异常原因java.lang.VerifyError:操作数堆栈上的错误类型 [英] Reason for the exception java.lang.VerifyError: Bad type on operand stack

查看:4842
本文介绍了异常原因java.lang.VerifyError:操作数堆栈上的错误类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单的java代码在操作数堆栈上发送
java.lang.VerifyError:Bad类型异常

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);
    }
}

如果我删除代码

parentData = 1

B 的构造函数中,异常将不会到来。

from B's constructor, the exception won't come.

任何人都可以说明原因吗?

Can any one tell the reason for this?

推荐答案

问题出现是因为你的lambda表达式没有引用这个或者的成员,但外部 的成员。你写过课 B 喜欢

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);
    }
}

编译器拒绝了它,毫无疑问是访问 innerData 意味着访问

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

关于外部实例的观点它是一个常量,当内部实例尚未完全构造时,它甚至可用。因此接受代码是正确的,但不幸的是编译器生成的代码试图通过内部类实例的隐式字段访问外部实例,因此lambda表达式需要内部类的实例并尝试使用未完全构造的内部class实例产生错误。

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);
    }
}

如果有这么小的变化,lambda表达式指的是外部实例没有访问内部实例,也没有出现错误。

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

这篇关于异常原因java.lang.VerifyError:操作数堆栈上的错误类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆