-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
11, 17
The following small test program will a false result for `StackWalker.getCallerClass()` if running with `-XX:+ShowHiddenFrames` (i.e. `class InjectedInvoker/0x0000000800062440` instead of `class GetCallerClass`):
```
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.Set;
public class GetCallerClass {
private static Method gcc;
static {
try {
gcc = GetCallerClass.class.getDeclaredMethod("getCallerClass");
} catch (Exception ex) {
System.out.println(ex);
}
}
private static final StackWalker WALKER = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE), 3);
public static void getCallerClass() {
System.out.println(WALKER.getCallerClass());
}
public static void main(String[] args) throws Throwable {
MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);
MethodHandle mh =
MethodHandles.
lookup().
findVirtual(Method.class, "invoke", mt).
bindTo(gcc);
mh.invoke(null, null);
}
}
```
```
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.Set;
public class GetCallerClass {
private static Method gcc;
static {
try {
gcc = GetCallerClass.class.getDeclaredMethod("getCallerClass");
} catch (Exception ex) {
System.out.println(ex);
}
}
private static final StackWalker WALKER = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE), 3);
public static void getCallerClass() {
System.out.println(WALKER.getCallerClass());
}
public static void main(String[] args) throws Throwable {
MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);
MethodHandle mh =
MethodHandles.
lookup().
findVirtual(Method.class, "invoke", mt).
bindTo(gcc);
mh.invoke(null, null);
}
}
```