Reason for the exception java.lang.VerifyError: Bad type on operand stack

前端 未结 2 1966
灰色年华
灰色年华 2020-12-09 09:32

The below simple java code sends the java.lang.VerifyError: Bad type on operand stack exception

public class TestJavaCodes {

    int pare         


        
相关标签:
2条回答
  • 2020-12-09 10:07

    The problem arises because your lambda expression does not reference this or a member of this but a member of the outer this. Had you written class B like

    public class B extends A {
        int innerData;
        public B() {
            super((data1, type) -> innerData = 1);
        }
    }
    

    the compiler rejected it without any doubts as accessing innerData implies accessing this.

    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:

    public class B extends A {
        public B() {
            this(TestJavaCodes.this);
        }
        private B(TestJavaCodes outer) {
            super((data1, type) -> outer.parentData = 1);
        }
    }
    

    with that small change, the lambda expression refers to the outer instance without accessing the inner instance and no error arises.

    0 讨论(0)
  • 2020-12-09 10:18

    Seems that such code should not compile at all. I minimized your code:

    public class CompilerBug {
        int var = 0;
    
        public static void main(String[] args) {
            new CompilerBug().new Inner();
        }
    
        public class Inner {
            public Inner(Runnable r) {}
    
            public Inner() {
                this(() -> {
                    var = 1;
                });
            }
        }
    }
    

    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:

    Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
    Exception Details:
      Location:
        CompilerBug$Inner.<init>(LCompilerBug;)V @3: invokedynamic
      Reason:
        Type uninitializedThis (current frame, stack[2]) is not assignable to 'CompilerBug$Inner'
      Current Frame:
        bci: @3
        flags: { flagThisUninit }
        locals: { uninitializedThis, 'CompilerBug' }
        stack: { uninitializedThis, 'CompilerBug', uninitializedThis }
      Bytecode:
        0000000: 2a2b 2aba 0003 0000 b700 04b1
    
            at CompilerBug.main(CompilerBug.java:5)
    

    This code is not compiled by ECJ compiler. It reports a compilation error:

    ----------
    1. ERROR in C:\projects\Test\src\CompilerBug.java (at line 12)
        this(() -> {
             ^^^^^
    Cannot refer to 'this' nor 'super' while explicitly invoking a constructor
    ----------
    1 problem (1 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)

    0 讨论(0)
提交回复
热议问题