Path: blob/master/test/jdk/java/lang/Class/getEnclosingMethod/EnclosingMethodTests.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 496234126* @summary Check getEnclosingMethod method27* @author Joseph D. Darcy28*/2930import java.lang.reflect.Method;31import java.lang.annotation.*;3233public class EnclosingMethodTests {34static Class<?> anonymousClass;3536static {37Cloneable c = new Cloneable() {}; // anonymous cloneable38anonymousClass = c.getClass();39}4041EnclosingMethodTests() {}4243@MethodDescriptor("java.lang.Class EnclosingMethodTests.getLocalClass(Object o)")44Class getLocalClass(Object o) {45class Local {};46Local l = new Local();47return l.getClass();48}4950static int examine(Class enclosedClass, String methodSig) {51Method m = enclosedClass.getEnclosingMethod();52if (m == null && methodSig == null)53return 0;5455if (m != null &&56m.getAnnotation(MethodDescriptor.class).value().equals(methodSig))57return 0; // everything is okay58else {59System.err.println("\nUnexpected method value; expected:\t" + methodSig +60"\ngot:\t" + m);61return 1;62}63}6465@MethodDescriptor("public static void EnclosingMethodTests.main(java.lang.String[])")66public static void main(String argv[]) {67int failures = 0;68class StaticLocal {};6970failures += examine(StaticLocal.class,71"public static void EnclosingMethodTests.main(java.lang.String[])");7273failures += examine( (new EnclosingMethodTests()).getLocalClass(null),74"java.lang.Class EnclosingMethodTests.getLocalClass(Object o)");7576failures += examine(EnclosingMethodTests.class, null);7778failures += examine(anonymousClass, null);7980if (failures > 0)81throw new RuntimeException("Test failed.");82}83}8485@Retention(RetentionPolicy.RUNTIME)86@interface MethodDescriptor {87String value();88}899091