Path: blob/master/test/jdk/java/lang/Class/getEnclosingClass/EnclosingClassTest.java
41153 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4992173 499217026* @library /test/lib27* @modules jdk.compiler28* @run testng/othervm EnclosingClassTest29* @summary Check getEnclosingClass and other methods30* @author Peter von der Ah\u00e931*/3233import java.io.BufferedReader;34import java.io.FileReader;35import java.io.FileWriter;36import java.io.IOException;37import java.io.PrintWriter;38import java.lang.reflect.Field;39import java.lang.reflect.InvocationTargetException;40import java.nio.file.Files;41import java.nio.file.Path;4243import common.TestMe;44import jdk.test.lib.compiler.CompilerUtils;45import jdk.test.lib.util.FileUtils;46import org.testng.Assert;47import org.testng.annotations.BeforeClass;48import org.testng.annotations.Test;4950/*51* We have five kinds of classes:52* a) Top level classes53* b) Nested classes (static member classes)54* c) Inner classes (non-static member classes)55* d) Local classes (named classes declared within a method)56* e) Anonymous classes57*58* Each one can be within a package or not.59* Kinds b-e can/must be within kinds a-e.60* This gives us a three dimensional space:61* 1. dimension: b-e62* 2. dimension: a-e63* 3. dimension: packages64*65* We make a two dimensional matrix of (b-e)x(a-e) and change the66* package configuration on that:67*68* b c d e69* a x x x x70* b x x x x71* c o x x x where o means "not legal"72* d o x x x73* e o x x x74*/7576public class EnclosingClassTest {77private static final String SRC_DIR = System.getProperty("test.src");78private static final Path ENCLOSING_CLASS_SRC = Path.of(SRC_DIR, "EnclosingClass.java");79private static final String GEN_SRC_DIR = "gensrc";8081@BeforeClass82public void createEnclosingClasses() throws IOException {83Path pkg1Dir = Path.of(GEN_SRC_DIR, "pkg1");84Path pkg2Dir = Path.of(GEN_SRC_DIR, "pkg1", "pkg2");85Path pkg1File = pkg1Dir.resolve("EnclosingClass.java");86Path pkg2File = pkg2Dir.resolve("EnclosingClass.java");8788if (!Files.notExists(pkg1Dir)) {89FileUtils.deleteFileTreeWithRetry(pkg1Dir);90}91Files.createDirectories(pkg2Dir);92createAndWriteEnclosingClasses(ENCLOSING_CLASS_SRC, pkg1File, "pkg1");93createAndWriteEnclosingClasses(ENCLOSING_CLASS_SRC, pkg2File, "pkg1.pkg2");9495Assert.assertTrue(CompilerUtils.compile(ENCLOSING_CLASS_SRC, Path.of(System.getProperty("test.classes")),96"--source-path", SRC_DIR));97Assert.assertTrue(CompilerUtils.compile(pkg1File, Path.of(System.getProperty("test.classes")),98"-classpath", System.getProperty("test.class.path")));99Assert.assertTrue(CompilerUtils.compile(pkg2File, Path.of(System.getProperty("test.classes")),100"-classpath", System.getProperty("test.class.path")));101}102103@Test104public void testEnclosingClasses() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,105InvocationTargetException, InstantiationException {106test(Class.forName("EnclosingClass").getDeclaredConstructor().newInstance());107}108109@Test110public void testEnclosingClassesInPackage() throws ClassNotFoundException, NoSuchMethodException,111IllegalAccessException, InvocationTargetException, InstantiationException {112test(Class.forName("pkg1.EnclosingClass").getDeclaredConstructor().newInstance());113}114115@Test116public void testEnclosingClassesInNestedPackage() throws ClassNotFoundException, NoSuchMethodException,117IllegalAccessException, InvocationTargetException, InstantiationException {118test(Class.forName("pkg1.pkg2.EnclosingClass").getDeclaredConstructor().newInstance());119}120121private void createAndWriteEnclosingClasses(Path source, Path target, String packageName) throws IOException {122String className = packageName + ".EnclosingClass";123try (BufferedReader br = new BufferedReader(new FileReader(source.toFile()));124PrintWriter bw = new PrintWriter(new FileWriter(target.toFile()))) {125String line;126while ((line = br.readLine()) != null) {127if (line.contains("canonical=\"EnclosingClass")) {128line = line.replaceAll("canonical=\"EnclosingClass", "canonical=\"" + className);129} else if (line.contains("\"class EnclosingClass")) {130line = line.replaceAll("\"class EnclosingClass", "\"class " + className);131} else if (line.contains("//package")) {132line = line.replaceAll("//package", "package " + packageName + ";");133}134bw.println(line);135}136}137}138139private void info(Class<?> c, Class<?> encClass, String desc) {140if (!"".equals(desc)) {141System.out.println(desc + ":");142}143System.out.println(c);144System.out.println("\tis enclosed by:\t\t" + encClass);145System.out.println("\thas simple name:\t`" +146c.getSimpleName() + "'");147System.out.println("\thas canonical name:\t`" +148c.getCanonicalName() + "'");149}150151private void match(String actual, String expected) {152Assert.assertTrue((actual == null && expected == null) || actual.equals(expected));153System.out.println("\t`" +154actual + "' matches expected `" +155expected + "'");156}157158private void check(Class<?> c, Class<?> enc,159String encName, String encNameExpected,160String simpleName, String simpleNameExpected,161String canonicalName, String canonicalNameExpected) {162match(encName, encNameExpected);163match(simpleName, simpleNameExpected);164match(canonicalName, canonicalNameExpected);165}166167private void testClass(Class<?> c, TestMe annotation, Field f) {168if (Void.class.equals(c))169return;170Class<?> encClass = c.getEnclosingClass();171c.getEnclosingMethod(); // make sure it does not crash172c.getEnclosingConstructor(); // make sure it does not crash173info(c, encClass, annotation.desc());174check(c, encClass,175""+encClass, annotation.encl(),176c.getSimpleName(), annotation.simple(),177c.getCanonicalName(),178annotation.hasCanonical() ? annotation.canonical() : null);179if (void.class.equals(c))180return;181Class<?> array = java.lang.reflect.Array.newInstance(c, 0).getClass();182check(array, array.getEnclosingClass(),183"", "",184array.getSimpleName(), annotation.simple()+"[]",185array.getCanonicalName(),186annotation.hasCanonical() ? annotation.canonical()+"[]" : null);187}188189private void test(Object tests) {190for (Field f : tests.getClass().getFields()) {191TestMe annotation = f.getAnnotation(TestMe.class);192if (annotation != null) {193try {194testClass((Class<?>)f.get(tests), annotation, f);195} catch (AssertionError ex) {196System.err.println("Error in " +197tests.getClass().getName() +198"." + f.getName());199throw ex;200} catch (IllegalAccessException ex) {201ex.printStackTrace();202throw new RuntimeException(ex);203}204}205}206}207}208209210211