I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx)
. IntelliJ suggested that I change it to SpectatorSwitcher::new
instead. SpectatorSwitcher is a non-static inner class of the class I'm working in. The suggested code compiles fine (using maven) but I get the following VerifyError on execution:
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
Test.lambda$runTest$8(LTest$Worker;)V @2: invokedynamic
Reason:
Type 'Test$Worker' (current frame, stack[1]) is not assignable to 'Test'
Current Frame:
bci: @2
flags: { }
locals: { 'Test$Worker' }
stack: { 'Test$Worker', 'Test$Worker' }
Bytecode:
0000000: 2a2a ba00 0b00 00b6 000c b1
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getMethod0(Class.java:2937)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Why is javac / maven not failing while compiling but still producing invalid byte code?
Edit: The problem appears to be far more complex than the simple call, this is the code needed to reproduce it:
import java.util.function.Function;
/**
* @author Yawkat
*/
public class Test {
public static void main(String[] args) { new Test().runTest(); }
private void runTest() {
Worker worker = new Worker();
run(() -> worker.print(field -> new SomeClass(field)));
run(() -> worker.print(SomeClass::new));
}
private void run(Runnable runnable) {
runnable.run();
}
private class SomeClass {
final Object field;
SomeClass(Object field) {
this.field = field;
}
}
private static class Worker {
void print(Function<Object, Object> i) {
System.out.println(i.apply(null));
}
}
}
Even after smacking my head into the bytecode for almost an hour, I've not been able to come to a reasonable conclusion as to why this is happening. Surprisingly, changing your method to this:
works fine. Also, getting rid of
run()
method invocation, and just callingworker.print()
:also works.
It seems like, using the constructor reference as in your case is not able to pass the enclosing instance of
Test
class to theSomeClass
constructor which is required. While the two cases here are able to passTest
instance to theSomeClass
constructor.But I couldn't come to the exact reason. The above reasoning might very well be wrong. But I've just come to that after getting to those working approach.
You might want to go through lambda translation, to understand the inner working. I'm still not very much clear about how lambdas and method references are translated.
I found a thread in lambda mailing list about similar issue. Also, this SO post is also related.
The following
runtTest()
method:Is compiled to following bytecode:
I can see only the second
run()
method invocation doesn't passLSO
argument, while others do pass it. You can run the command -javap -c -s -verbose Test
, to see Bootstrap methods for#0
,#1
, etc. I guess we can definitely say that this is a bug. Perhaps you can file one.