提交 43e7cb30 编写于 作者: M mchung

8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided

Reviewed-by: alanb, forax, dholmes, twisti
上级 885bf602
...@@ -267,7 +267,8 @@ SUNWprivate_1.1 { ...@@ -267,7 +267,8 @@ SUNWprivate_1.1 {
Java_sun_misc_GC_maxObjectInspectionAge; Java_sun_misc_GC_maxObjectInspectionAge;
Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0; Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0;
Java_sun_reflect_NativeMethodAccessorImpl_invoke0; 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_reflect_Reflection_getClassAccessFlags;
Java_sun_misc_Version_getJdkVersionInfo; Java_sun_misc_Version_getJdkVersionInfo;
Java_sun_misc_Version_getJdkSpecialVersion; Java_sun_misc_Version_getJdkSpecialVersion;
......
...@@ -27,7 +27,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; ...@@ -27,7 +27,8 @@ text: .text%Java_java_io_FileInputStream_initIDs;
text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs;
text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs;
text: .text%Java_java_lang_System_setIn0; 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_Class_forName0;
text: .text%Java_java_lang_Object_getClass; text: .text%Java_java_lang_Object_getClass;
text: .text%Java_sun_reflect_Reflection_getClassAccessFlags; text: .text%Java_sun_reflect_Reflection_getClassAccessFlags;
......
...@@ -26,7 +26,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; ...@@ -26,7 +26,8 @@ text: .text%Java_java_io_FileInputStream_initIDs;
text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs;
text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs;
text: .text%Java_java_lang_System_setIn0; 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_Class_forName0;
text: .text%Java_java_lang_String_intern; text: .text%Java_java_lang_String_intern;
text: .text%Java_java_lang_Float_floatToIntBits; text: .text%Java_java_lang_Float_floatToIntBits;
......
...@@ -28,7 +28,8 @@ text: .text%Java_java_io_FileInputStream_initIDs; ...@@ -28,7 +28,8 @@ text: .text%Java_java_io_FileInputStream_initIDs;
text: .text%Java_java_io_FileDescriptor_initIDs; text: .text%Java_java_io_FileDescriptor_initIDs;
text: .text%Java_java_io_FileOutputStream_initIDs; text: .text%Java_java_io_FileOutputStream_initIDs;
text: .text%Java_java_lang_System_setIn0; 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_Class_forName0;
text: .text%Java_java_lang_String_intern; text: .text%Java_java_lang_String_intern;
text: .text%Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0; text: .text%Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0;
......
...@@ -57,6 +57,14 @@ public class Reflection { ...@@ -57,6 +57,14 @@ public class Reflection {
@CallerSensitive @CallerSensitive
public static native Class<?> getCallerClass(); 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 /** Retrieves the access flags written to the class file. For
inner classes these flags may differ from those returned by inner classes these flags may differ from those returned by
Class.getModifiers(), which searches the InnerClasses Class.getModifiers(), which searches the InnerClasses
......
...@@ -351,7 +351,7 @@ JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim); ...@@ -351,7 +351,7 @@ JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim);
* java.lang.Class and java.lang.ClassLoader * 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 * Returns the immediate caller class of the native method invoking
......
...@@ -26,10 +26,16 @@ ...@@ -26,10 +26,16 @@
#include "jvm.h" #include "jvm.h"
#include "sun_reflect_Reflection.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) (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 JNIEXPORT jint JNICALL Java_sun_reflect_Reflection_getClassAccessFlags
......
/*
* 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);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册