Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Class/getEnclosingClass/EnclosingClassTest.java
38828 views
/*1* Copyright (c) 2004, 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*27* @run shell make_src.sh28* @run shell build.sh29* @run main/othervm -esa -ea EnclosingClassTest30*31* @summary Check getEnclosingClass and other methods32* @author Peter von der Ah\u00e933*/3435/*36* We have five kinds of classes:37* a) Top level classes38* b) Nested classes (static member classes)39* c) Inner classes (non-static member classes)40* d) Local classes (named classes declared within a method)41* e) Anonymous classes42*43* Each one can be within a package or not.44* Kinds b-e can/must be within kinds a-e.45* This gives us a three dimensional space:46* 1. dimension: b-e47* 2. dimension: a-e48* 3. dimension: packages49*50* We make a two dimensional matrix of (b-e)x(a-e) and change the51* package configuration on that:52*53* b c d e54* a x x x x55* b x x x x56* c o x x x where o means "not legal"57* d o x x x58* e o x x x59*/6061import java.util.List;62import java.util.LinkedList;63import java.lang.reflect.Field;64import common.TestMe;6566public class EnclosingClassTest {67static void info(Class<?> c, Class<?> encClass, String desc) {68if (!"".equals(desc))69System.out.println(desc + ":");70System.out.println(c);71System.out.println("\tis enclosed by:\t\t" + encClass);72System.out.println("\thas simple name:\t`" +73c.getSimpleName() + "'");74System.out.println("\thas canonical name:\t`" +75c.getCanonicalName() + "'");76}7778static void match(String actual, String expected) {79assert((actual == null && expected == null) || actual.equals(expected));80System.out.println("\t`" +81actual + "' matches expected `" +82expected + "'");83}8485static void check(Class<?> c, Class<?> enc,86String encName, String encNameExpected,87String simpleName, String simpleNameExpected,88String canonicalName, String canonicalNameExpected) {89match(encName, encNameExpected);90match(simpleName, simpleNameExpected);91match(canonicalName, canonicalNameExpected);92}9394static void testClass(Class<?> c, TestMe annotation, Field f) {95if (Void.class.equals(c))96return;97Class<?> encClass = c.getEnclosingClass();98c.getEnclosingMethod(); // make sure it does not crash99c.getEnclosingConstructor(); // make sure it does not crash100info(c, encClass, annotation.desc());101check(c, encClass,102""+encClass, annotation.encl(),103c.getSimpleName(), annotation.simple(),104c.getCanonicalName(),105annotation.hasCanonical() ? annotation.canonical() : null);106if (void.class.equals(c))107return;108Class<?> array = java.lang.reflect.Array.newInstance(c, 0).getClass();109check(array, array.getEnclosingClass(),110"", "",111array.getSimpleName(), annotation.simple()+"[]",112array.getCanonicalName(),113annotation.hasCanonical() ? annotation.canonical()+"[]" : null);114}115116static void test(Object tests) {117for (Field f : tests.getClass().getFields()) {118TestMe annotation = f.getAnnotation(TestMe.class);119if (annotation != null) {120try {121testClass((Class<?>)f.get(tests), annotation, f);122} catch (AssertionError ex) {123System.err.println("Error in " +124tests.getClass().getName() +125"." + f.getName());126throw ex;127} catch (IllegalAccessException ex) {128ex.printStackTrace();129throw new RuntimeException(ex);130}131}132}133}134public static void main(String[] args) {135test(new EnclosingClass());136test(new pkg1.EnclosingClass());137test(new pkg1.pkg2.EnclosingClass());138}139}140141142