mchung | d2d2235 | 2013-10-17 19:47:47 -0700 | [diff] [blame] | 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 sun.reflect.Reflection.getCallerClass(int) |
| 28 | * @run main GetCallerClassWithDepth |
| 29 | */ |
| 30 | |
| 31 | public class GetCallerClassWithDepth { |
| 32 | public static void main(String[] args) throws Exception { |
| 33 | Class<?> c = Test.test(); |
| 34 | assertEquals(c, GetCallerClassWithDepth.class); |
| 35 | Class<?> caller = Test.caller(); |
| 36 | assertEquals(caller, GetCallerClassWithDepth.class); |
| 37 | Test.selfTest(); |
| 38 | |
| 39 | try { |
| 40 | sun.reflect.Reflection.getCallerClass(-1); |
| 41 | throw new RuntimeException("getCallerClass(-1) should fail"); |
| 42 | } catch (Error e) { |
| 43 | System.out.println("Expected: " + e.getMessage()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public Class<?> getCallerClass() { |
| 48 | // 0: Reflection 1: getCallerClass 2: Test.test 3: main |
| 49 | return sun.reflect.Reflection.getCallerClass(3); |
| 50 | } |
| 51 | |
| 52 | static void assertEquals(Class<?> c, Class<?> expected) { |
| 53 | if (c != expected) { |
| 54 | throw new RuntimeException("Incorrect caller: " + c); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static class Test { |
| 59 | // Returns the caller of this method |
| 60 | public static Class<?> test() { |
| 61 | return new GetCallerClassWithDepth().getCallerClass(); |
| 62 | } |
| 63 | |
| 64 | // Returns the caller of this method |
| 65 | public static Class<?> caller() { |
| 66 | // 0: Reflection 1: Test.caller 2: main |
| 67 | return sun.reflect.Reflection.getCallerClass(2); |
| 68 | } |
| 69 | public static void selfTest() { |
| 70 | // 0: Reflection 1: Test.selfTest |
| 71 | Class<?> c = sun.reflect.Reflection.getCallerClass(1); |
| 72 | assertEquals(c, Test.class); |
| 73 | Inner1.deep(); |
| 74 | } |
| 75 | |
| 76 | static class Inner1 { |
| 77 | static void deep() { |
| 78 | deeper(); |
| 79 | } |
| 80 | static void deeper() { |
| 81 | Inner2.deepest(); |
| 82 | } |
| 83 | static class Inner2 { |
| 84 | static void deepest() { |
| 85 | // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest |
| 86 | Class<?> c = sun.reflect.Reflection.getCallerClass(4); |
| 87 | assertEquals(c, Test.class); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |