JDK-8016814 : sun.reflect.Reflection.getCallerClass returns the wrong stack frame
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 7u25
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2013-06-17
  • Updated: 2013-11-27
  • Resolved: 2013-06-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7u40 b31Fixed
Related Reports
Relates :  
Description
Problem Description
-------------------
sun.reflect.Reflection.getCallerClass(int) has changed its behaviour such
that a different Class is now returned for a given integer. It looks like
getCallerClass is now calling another method on Reflection, which has changed
the call stack. Attached is a test class that exhibits the problem.

For jdk7u21, its output is:

sun.reflect.Reflection
sunreflect.ReflectionCaller$Inner2
sunreflect.ReflectionCaller$Inner1
sunreflect.ReflectionCaller

But for 7u25, its output is:
sun.reflect.Reflection
sun.reflect.Reflection
sunreflect.ReflectionCaller$Inner2
sunreflect.ReflectionCaller$Inner1 
Comments
Verified with 7u40/b31 by provided regression test.
02-07-2013

SQE is OK with this fix
20-06-2013

7u40-critical-request justification: This bug is a regression introduced in 7u25. Although sun.reflect.Reflection is a private API, there are existing applications depending on it (see JDK-8014925). Propose to fix this bug together with JDK-8014925 and the fix is reviewed and approved [1]. Low risk. Webrev at: http://cr.openjdk.java.net/~mchung/jdk7u/8014925/webrev.01/ Reviewed-by: jrose, alanb, chegar [1] http://mail.openjdk.java.net/pipermail/jdk7u-dev/2013-June/006791.html
19-06-2013

1-line fix: diff --git a/src/share/classes/sun/reflect/Reflection.java b/src/share/classes/sun/reflect/Reflection.java --- a/src/share/classes/sun/reflect/Reflection.java +++ b/src/share/classes/sun/reflect/Reflection.java @@ -65,7 +65,7 @@ @Deprecated @CallerSensitive public static Class getCallerClass(int depth) { - return getCallerClass0(depth); + return getCallerClass0(depth+1); }
19-06-2013

In 7u25, a new native sun.reflect.Reflection.getCallerClass() method was added andsun.reflect.Reflection.getCallerClass(int) had to be modified as a Java method (can't have two native method implementation with the same method name) that adds a new frame to the stack when it is called. The regression test didn't catch this bug because the test case verifying the returned class have the same caller class at the given depth and depth+1.
18-06-2013