1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8025799 |
|
27 * @summary Reflection.getCallerClass(int) |
|
28 * @modules java.base/jdk.internal.reflect |
|
29 * @run main GetCallerClassWithDepth |
|
30 */ |
|
31 |
|
32 import jdk.internal.reflect.Reflection; |
|
33 |
|
34 public class GetCallerClassWithDepth { |
|
35 public static void main(String[] args) throws Exception { |
|
36 Class<?> c = Test.test(); |
|
37 assertEquals(c, GetCallerClassWithDepth.class); |
|
38 Class<?> caller = Test.caller(); |
|
39 assertEquals(caller, GetCallerClassWithDepth.class); |
|
40 Test.selfTest(); |
|
41 |
|
42 try { |
|
43 Reflection.getCallerClass(-1); |
|
44 throw new RuntimeException("getCallerClass(-1) should fail"); |
|
45 } catch (Error e) { |
|
46 System.out.println("Expected: " + e.getMessage()); |
|
47 } |
|
48 } |
|
49 |
|
50 public Class<?> getCallerClass() { |
|
51 // 0: Reflection 1: getCallerClass 2: Test.test 3: main |
|
52 return Reflection.getCallerClass(3); |
|
53 } |
|
54 |
|
55 static void assertEquals(Class<?> c, Class<?> expected) { |
|
56 if (c != expected) { |
|
57 throw new RuntimeException("Incorrect caller: " + c); |
|
58 } |
|
59 } |
|
60 |
|
61 static class Test { |
|
62 // Returns the caller of this method |
|
63 public static Class<?> test() { |
|
64 return new GetCallerClassWithDepth().getCallerClass(); |
|
65 } |
|
66 |
|
67 // Returns the caller of this method |
|
68 public static Class<?> caller() { |
|
69 // 0: Reflection 1: Test.caller 2: main |
|
70 return Reflection.getCallerClass(2); |
|
71 } |
|
72 public static void selfTest() { |
|
73 // 0: Reflection 1: Test.selfTest |
|
74 Class<?> c = Reflection.getCallerClass(1); |
|
75 assertEquals(c, Test.class); |
|
76 Inner1.deep(); |
|
77 } |
|
78 |
|
79 static class Inner1 { |
|
80 static void deep() { |
|
81 deeper(); |
|
82 } |
|
83 static void deeper() { |
|
84 Inner2.deepest(); |
|
85 } |
|
86 static class Inner2 { |
|
87 static void deepest() { |
|
88 // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest |
|
89 Class<?> c = Reflection.getCallerClass(4); |
|
90 assertEquals(c, Test.class); |
|
91 } |
|
92 } |
|
93 } |
|
94 } |
|
95 } |
|