The below simple java code sends the java.lang.VerifyError: Bad type on operand stack exception
public class TestJavaCodes {
int parentData = 0;
public void init() {
A ob = new B();
}
public static void main(String[] args) {
TestJavaCodes testJavaCodes = new TestJavaCodes();
testJavaCodes.init();
}
public static class A {
public A(MyLambdaFunc lambdaFunc) {
}
}
public class B extends A {
public B() {
super((data1, type) -> {
parentData = 1;
});
}
}
@FunctionalInterface
public static interface MyLambdaFunc {
public void onData(String data, int type);
}
}
If I remove the code
parentData = 1
from B
's constructor, the exception won't come.
Can any one tell the reason for this?
Seems that such code should not compile at all. I minimized your code:
It's compiled without problems by javac 1.8.0.25, 1.8.0.40 and 1.9b57. Every compiled version produces the same output when launching:
This code is not compiled by ECJ compiler. It reports a compilation error:
So it looks like a bug in javac compiler: it should return a compilation error instead (like ECJ).
I did not find similar bug in OpenJDK bug tracker, so submitted a new bug report via webform. If Java folks are reading this, the internal review ID assigned is JI-9021379.
Update: The bug report is accepted (JDK-8129740)
The problem arises because your lambda expression does not reference
this
or a member ofthis
but a member of the outerthis
. Had you written classB
likethe compiler rejected it without any doubts as accessing
innerData
implies accessingthis
.The point about the outer instance is that it is a constant which is even available when the inner instance has not been fully constructed yet. So it’s correct to accept the code but unfortunately the compiler generates code which attempts to access the outer instance via an implicit field of the inner class instance, thus the lambda expression requires an instance of the inner class and attempting to use the not fully constructed inner class instance produces the error.
It can be easily demonstrated that the code can be compiled correctly:
with that small change, the lambda expression refers to the outer instance without accessing the inner instance and no error arises.