Path: blob/master/test/jdk/java/lang/Class/getEnclosingConstructor/EnclosingConstructorTests.java
41153 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 4962341 683255726* @summary Check getEnclosingMethod method27* @author Joseph D. Darcy28*/2930import java.lang.reflect.Constructor;31import java.lang.annotation.*;3233public class EnclosingConstructorTests {34static Class<?> anonymousClass;35static Class<?> localClass;36static Class<?> anotherLocalClass;3738static {39Cloneable c = new Cloneable() {}; // anonymous cloneable40anonymousClass = c.getClass();41}4243@ConstructorDescriptor("EnclosingConstructorTests()")44EnclosingConstructorTests() {45class Local {};46Local l = new Local();47localClass = l.getClass();48}495051@ConstructorDescriptor("private EnclosingConstructorTests(int)")52private EnclosingConstructorTests(int i) {53class Local {};54Local l = new Local();55anotherLocalClass = l.getClass();56}575859static int examine(Class<?> enclosedClass, String constructorSig) {60Constructor<?> c = enclosedClass.getEnclosingConstructor();61if (c == null && constructorSig == null)62return 0;6364if (c != null &&65c.getAnnotation(ConstructorDescriptor.class).value().equals(constructorSig))66return 0; // everything is okay67else {68System.err.println("\nUnexpected constructor value; expected:\t" + constructorSig +69"\ngot:\t" + c);70return 1;71}72}737475public static void main(String argv[]) {76int failures = 0;77class StaticLocal {};78EnclosingConstructorTests ect = new EnclosingConstructorTests();79ect = new EnclosingConstructorTests(5);8081failures += examine(StaticLocal.class,82null);8384failures += examine(localClass,85"EnclosingConstructorTests()");8687failures += examine(anotherLocalClass,88"private EnclosingConstructorTests(int)");8990failures += examine(anonymousClass,91null);9293if (failures > 0)94throw new RuntimeException("Test failed.");95}96}9798@Retention(RetentionPolicy.RUNTIME)99@interface ConstructorDescriptor {100String value();101}102103104