From 43e7cb305f07b8dd24996e29db72cb08a6c27628 Mon Sep 17 00:00:00 2001 From: mchung Date: Thu, 17 Oct 2013 19:47:47 -0700 Subject: [PATCH] 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided Reviewed-by: alanb, forax, dholmes, twisti --- makefiles/mapfiles/libjava/mapfile-vers | 3 +- makefiles/mapfiles/libjava/reorder-sparc | 3 +- makefiles/mapfiles/libjava/reorder-sparcv9 | 3 +- makefiles/mapfiles/libjava/reorder-x86 | 3 +- src/share/classes/sun/reflect/Reflection.java | 8 ++ src/share/javavm/export/jvm.h | 2 +- src/share/native/sun/reflect/Reflection.c | 10 +- .../Reflection/GetCallerClassWithDepth.java | 92 +++++++++++++++++++ 8 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 test/sun/reflect/Reflection/GetCallerClassWithDepth.java diff --git a/makefiles/mapfiles/libjava/mapfile-vers b/makefiles/mapfiles/libjava/mapfile-vers index 29ce7eb26..7b4d5472b 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 b64537e3e..165730525 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 8e6d249a4..fe80fca8b 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 ff4836f46..839ee50ca 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 a57b89b2b..63baa42ab 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 caec0b930..46bf75ae5 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 46cfa570e..8da4101b8 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 000000000..33eefd548 --- /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); + } + } + } + } +} -- GitLab