diff --git a/makefiles/mapfiles/libjava/mapfile-vers b/makefiles/mapfiles/libjava/mapfile-vers index 29ce7eb26df276fce594783c874506b32049e8b1..7b4d5472b1494b44174ab6453e0164b2d1336a51 100644 --- a/makefiles/mapfiles/libjava/mapfile-vers +++ b/makefiles/mapfiles/libjava/mapfile-vers @@ -267,7 +267,8 @@ SUNWprivate_1.1 { Java_sun_misc_GC_maxObjectInspectionAge; Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0; Java_sun_reflect_NativeMethodAccessorImpl_invoke0; - Java_sun_reflect_Reflection_getCallerClass; + Java_sun_reflect_Reflection_getCallerClass__; + Java_sun_reflect_Reflection_getCallerClass__I; Java_sun_reflect_Reflection_getClassAccessFlags; Java_sun_misc_Version_getJdkVersionInfo; Java_sun_misc_Version_getJdkSpecialVersion; diff --git a/makefiles/mapfiles/libjava/reorder-sparc b/makefiles/mapfiles/libjava/reorder-sparc index b64537e3edd93653dc27c91b6b71e5d06803b20c..1657305257612dc717691e3f2fbd1d38ded036c8 100644 --- a/makefiles/mapfiles/libjava/reorder-sparc +++ b/makefiles/mapfiles/libjava/reorder-sparc @@ -27,7 +27,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_lang_System_setIn0; -text: .text%Java_sun_reflect_Reflection_getCallerClass; +text: .text%Java_sun_reflect_Reflection_getCallerClass__; +text: .text%Java_sun_reflect_Reflection_getCallerClass__I; text: .text%Java_java_lang_Class_forName0; text: .text%Java_java_lang_Object_getClass; text: .text%Java_sun_reflect_Reflection_getClassAccessFlags; diff --git a/makefiles/mapfiles/libjava/reorder-sparcv9 b/makefiles/mapfiles/libjava/reorder-sparcv9 index 8e6d249a440c437101977e78cc7fca148fbfbfad..fe80fca8b596292992b96bed2d5cafc05da40b93 100644 --- a/makefiles/mapfiles/libjava/reorder-sparcv9 +++ b/makefiles/mapfiles/libjava/reorder-sparcv9 @@ -26,7 +26,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_lang_System_setIn0; -text: .text%Java_sun_reflect_Reflection_getCallerClass; +text: .text%Java_sun_reflect_Reflection_getCallerClass__; +text: .text%Java_sun_reflect_Reflection_getCallerClass__I; text: .text%Java_java_lang_Class_forName0; text: .text%Java_java_lang_String_intern; text: .text%Java_java_lang_Float_floatToIntBits; diff --git a/makefiles/mapfiles/libjava/reorder-x86 b/makefiles/mapfiles/libjava/reorder-x86 index ff4836f4612f6a6d64b1910d009590e38a70b7d0..839ee50caa8e7e0c5990cf1bcb9c36ea2a0c89f1 100644 --- a/makefiles/mapfiles/libjava/reorder-x86 +++ b/makefiles/mapfiles/libjava/reorder-x86 @@ -28,7 +28,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_lang_System_setIn0; -text: .text%Java_sun_reflect_Reflection_getCallerClass; +text: .text%Java_sun_reflect_Reflection_getCallerClass__; +text: .text%Java_sun_reflect_Reflection_getCallerClass__I; text: .text%Java_java_lang_Class_forName0; text: .text%Java_java_lang_String_intern; text: .text%Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0; diff --git a/src/share/classes/sun/reflect/Reflection.java b/src/share/classes/sun/reflect/Reflection.java index a57b89b2b1d2ee479f5a82a70b665cf1c7a29873..63baa42abaec1aaa5feaeeaccde4daaf3465c3c5 100644 --- a/src/share/classes/sun/reflect/Reflection.java +++ b/src/share/classes/sun/reflect/Reflection.java @@ -57,6 +57,14 @@ public class Reflection { @CallerSensitive public static native Class getCallerClass(); + /** + * @deprecated This method will be removed in JDK 9. + * This method is a private JDK API and retained temporarily for + * existing code to run until a replacement API is defined. + */ + @Deprecated + public static native Class getCallerClass(int depth); + /** Retrieves the access flags written to the class file. For inner classes these flags may differ from those returned by Class.getModifiers(), which searches the InnerClasses diff --git a/src/share/javavm/export/jvm.h b/src/share/javavm/export/jvm.h index caec0b9306f1604cf85d8fc0cdb35497d4e01243..46bf75ae57c3cd7e27e32c73f828821cd62b9f9c 100644 --- a/src/share/javavm/export/jvm.h +++ b/src/share/javavm/export/jvm.h @@ -351,7 +351,7 @@ JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim); * java.lang.Class and java.lang.ClassLoader */ -#define JVM_DEPTH -1 +#define JVM_CALLER_DEPTH -1 /* * Returns the immediate caller class of the native method invoking diff --git a/src/share/native/sun/reflect/Reflection.c b/src/share/native/sun/reflect/Reflection.c index 46cfa570e1c1cb1b96609adcd49367eb6fa7a314..8da4101b8b175681b1506b2751782d3ecf677f2a 100644 --- a/src/share/native/sun/reflect/Reflection.c +++ b/src/share/native/sun/reflect/Reflection.c @@ -26,10 +26,16 @@ #include "jvm.h" #include "sun_reflect_Reflection.h" -JNIEXPORT jclass JNICALL Java_sun_reflect_Reflection_getCallerClass +JNIEXPORT jclass JNICALL Java_sun_reflect_Reflection_getCallerClass__ (JNIEnv *env, jclass unused) { - return JVM_GetCallerClass(env, JVM_DEPTH); // JVM_DEPTH is only the expected value + return JVM_GetCallerClass(env, JVM_CALLER_DEPTH); +} + +JNIEXPORT jclass JNICALL Java_sun_reflect_Reflection_getCallerClass__I +(JNIEnv *env, jclass unused, jint depth) +{ + return JVM_GetCallerClass(env, depth); } JNIEXPORT jint JNICALL Java_sun_reflect_Reflection_getClassAccessFlags diff --git a/test/sun/reflect/Reflection/GetCallerClassWithDepth.java b/test/sun/reflect/Reflection/GetCallerClassWithDepth.java new file mode 100644 index 0000000000000000000000000000000000000000..33eefd54875d74114ada3dccd3cfb55fd9611f6d --- /dev/null +++ b/test/sun/reflect/Reflection/GetCallerClassWithDepth.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8025799 + * @summary sun.reflect.Reflection.getCallerClass(int) + * @run main GetCallerClassWithDepth + */ + +public class GetCallerClassWithDepth { + public static void main(String[] args) throws Exception { + Class c = Test.test(); + assertEquals(c, GetCallerClassWithDepth.class); + Class caller = Test.caller(); + assertEquals(caller, GetCallerClassWithDepth.class); + Test.selfTest(); + + try { + sun.reflect.Reflection.getCallerClass(-1); + throw new RuntimeException("getCallerClass(-1) should fail"); + } catch (Error e) { + System.out.println("Expected: " + e.getMessage()); + } + } + + public Class getCallerClass() { + // 0: Reflection 1: getCallerClass 2: Test.test 3: main + return sun.reflect.Reflection.getCallerClass(3); + } + + static void assertEquals(Class c, Class expected) { + if (c != expected) { + throw new RuntimeException("Incorrect caller: " + c); + } + } + + static class Test { + // Returns the caller of this method + public static Class test() { + return new GetCallerClassWithDepth().getCallerClass(); + } + + // Returns the caller of this method + public static Class caller() { + // 0: Reflection 1: Test.caller 2: main + return sun.reflect.Reflection.getCallerClass(2); + } + public static void selfTest() { + // 0: Reflection 1: Test.selfTest + Class c = sun.reflect.Reflection.getCallerClass(1); + assertEquals(c, Test.class); + Inner1.deep(); + } + + static class Inner1 { + static void deep() { + deeper(); + } + static void deeper() { + Inner2.deepest(); + } + static class Inner2 { + static void deepest() { + // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest + Class c = sun.reflect.Reflection.getCallerClass(4); + assertEquals(c, Test.class); + } + } + } + } +}