8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
authormchung
Thu, 17 Oct 2013 19:47:47 -0700
changeset 21284 e6dfe841a88f
parent 20886 9630bb813a6c
child 21285 e740104a04f1
8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided Reviewed-by: alanb, forax, dholmes, twisti
jdk/makefiles/mapfiles/libjava/mapfile-vers
jdk/makefiles/mapfiles/libjava/reorder-sparc
jdk/makefiles/mapfiles/libjava/reorder-sparcv9
jdk/makefiles/mapfiles/libjava/reorder-x86
jdk/src/share/classes/sun/reflect/Reflection.java
jdk/src/share/javavm/export/jvm.h
jdk/src/share/native/sun/reflect/Reflection.c
jdk/test/sun/reflect/Reflection/GetCallerClassWithDepth.java
--- a/jdk/makefiles/mapfiles/libjava/mapfile-vers	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/makefiles/mapfiles/libjava/mapfile-vers	Thu Oct 17 19:47:47 2013 -0700
@@ -267,7 +267,8 @@
 		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;
--- a/jdk/makefiles/mapfiles/libjava/reorder-sparc	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/makefiles/mapfiles/libjava/reorder-sparc	Thu Oct 17 19:47:47 2013 -0700
@@ -27,7 +27,8 @@
 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;
--- a/jdk/makefiles/mapfiles/libjava/reorder-sparcv9	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/makefiles/mapfiles/libjava/reorder-sparcv9	Thu Oct 17 19:47:47 2013 -0700
@@ -26,7 +26,8 @@
 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;
--- a/jdk/makefiles/mapfiles/libjava/reorder-x86	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/makefiles/mapfiles/libjava/reorder-x86	Thu Oct 17 19:47:47 2013 -0700
@@ -28,7 +28,8 @@
 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;
--- a/jdk/src/share/classes/sun/reflect/Reflection.java	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/src/share/classes/sun/reflect/Reflection.java	Thu Oct 17 19:47:47 2013 -0700
@@ -57,6 +57,14 @@
     @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
--- a/jdk/src/share/javavm/export/jvm.h	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/src/share/javavm/export/jvm.h	Thu Oct 17 19:47:47 2013 -0700
@@ -351,7 +351,7 @@
  * 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
--- a/jdk/src/share/native/sun/reflect/Reflection.c	Fri Oct 18 08:57:52 2013 +0800
+++ b/jdk/src/share/native/sun/reflect/Reflection.c	Thu Oct 17 19:47:47 2013 -0700
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/reflect/Reflection/GetCallerClassWithDepth.java	Thu Oct 17 19:47:47 2013 -0700
@@ -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);
+                }
+            }
+        }
+    }
+}