United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-8016814 sun.reflect.Reflection.getCallerClass returns the wrong stack frame
JDK-8016814 : sun.reflect.Reflection.getCallerClass returns the wrong stack frame

Details
Type:
Bug
Submit Date:
2013-06-17
Status:
Closed
Updated Date:
2013-11-27
Project Name:
JDK
Resolved Date:
2013-06-25
Component:
core-libs
OS:
generic
Sub-Component:
java.lang:reflect
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
7u25
Fixed Versions:
7u40 (b31)

Related Reports
Backport:
Backport:
Relates:

Sub Tasks

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
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.
                                     
2013-06-18
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);
     }
 

                                     
2013-06-19
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
                                     
2013-06-19
SQE is OK with this fix
                                     
2013-06-20
URL:   http://hg.openjdk.java.net/jdk7u/jdk7u40-dev/jdk/rev/ee18d5c46468
User:  mchung
Date:  2013-06-25 23:13:38 +0000

                                     
2013-06-25
URL:   http://hg.openjdk.java.net/jdk7u/jdk7u40/jdk/rev/ee18d5c46468
User:  lana
Date:  2013-06-27 01:42:03 +0000

                                     
2013-06-27
Verified with 7u40/b31 by provided regression test.
                                     
2013-07-02



Hardware and Software, Engineered to Work Together