Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/reflect/Reflection/GetCallerClassWithDepth.java
38838 views
/*1* Copyright (c) 2013, 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 802579926* @summary sun.reflect.Reflection.getCallerClass(int)27* @run main GetCallerClassWithDepth28*/2930public class GetCallerClassWithDepth {31public static void main(String[] args) throws Exception {32Class<?> c = Test.test();33assertEquals(c, GetCallerClassWithDepth.class);34Class<?> caller = Test.caller();35assertEquals(caller, GetCallerClassWithDepth.class);36Test.selfTest();3738try {39sun.reflect.Reflection.getCallerClass(-1);40throw new RuntimeException("getCallerClass(-1) should fail");41} catch (Error e) {42System.out.println("Expected: " + e.getMessage());43}44}4546public Class<?> getCallerClass() {47// 0: Reflection 1: getCallerClass 2: Test.test 3: main48return sun.reflect.Reflection.getCallerClass(3);49}5051static void assertEquals(Class<?> c, Class<?> expected) {52if (c != expected) {53throw new RuntimeException("Incorrect caller: " + c);54}55}5657static class Test {58// Returns the caller of this method59public static Class<?> test() {60return new GetCallerClassWithDepth().getCallerClass();61}6263// Returns the caller of this method64public static Class<?> caller() {65// 0: Reflection 1: Test.caller 2: main66return sun.reflect.Reflection.getCallerClass(2);67}68public static void selfTest() {69// 0: Reflection 1: Test.selfTest70Class<?> c = sun.reflect.Reflection.getCallerClass(1);71assertEquals(c, Test.class);72Inner1.deep();73}7475static class Inner1 {76static void deep() {77deeper();78}79static void deeper() {80Inner2.deepest();81}82static class Inner2 {83static void deepest() {84// 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest85Class<?> c = sun.reflect.Reflection.getCallerClass(4);86assertEquals(c, Test.class);87}88}89}90}91}929394