Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Java 8 121 but not on Java 11

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Java 8 121 but not on Java 11

我知道在 Java 8 Build 75 https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8006684 上进行了修复,我无法用 75 之前的版本中断的代码重现同样的问题。但是这段代码抛出异常在 Java 8-121 上,但在 Java 上没有 11. 这个问题是在 JDK 的哪个版本中解决的?

此代码在 75 之前的版本上产生问题的示例,但在之后的版本上没有。

public static void main(String[] args) {
    xxx();
}
static void xxx() {
    Functional1 f  = () -> {
        Object o = new Object() { };
        return new A();
    };
}
static class A { }
static interface Functional1 { A func(); }

但是我的代码在 Java 8-121 上抛出异常,但在 Java 11 上没有。

该代码只是能够表示它但仍然以单一方式表示的真实代码的示例。

class Element{
    private final String data;
    public Element(final String data) {
        this.data = data;
    }    
    public String getData() {
        return data;
    }    
}

class Html{
    protected Element doSomething(final String data){
        return new Element(data);
    }
}

class A{
    protected final Html html = new Html();
}
class B extends A{}
class C extends B{}
class D extends C{}
class E extends D{}

public final class JavaBug extends E{
    private final List<SupplierElementWithMapper>data = Arrays.asList(new SupplierElementWithMapper(""));
    public static void main(String[] args)throws Exception{
        System.out.println("JavaVersion: "+System.getProperty("java.version"));
        final JavaBug clazz = new JavaBug();        
    }
    
    private final class SupplierElementWithMapper{
        private final Supplier<Element> supplier;
        private final Function<Element,String> mapper;
        private UnaryOperator<String> unaryOperator = UnaryOperator.identity();

        public SupplierElementWithMapper(final String selector) {
            this(()->html.doSomething(selector),Element::getData);
        }
        public SupplierElementWithMapper(final Supplier<Element> supplier,final Function<Element, String> mapper) {
            this.supplier = supplier;
            this.mapper = mapper;
        }

        private SupplierElementWithMapper addUnaryOperator(final UnaryOperator<String>unaryOperator){
            this.unaryOperator = unaryOperator;
            return this;
        }    
    }    
}

控制台

JavaVersion: 1.8.0_121
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    projectname/issues/JavaBug$SupplierElementWithMapper.lambda$new(Ljava/lang/String;Lprojectname/issues/JavaBug;)Lprojectname/issues/Element; @1: getfield
  Reason:
    Type 'java/lang/String' (current frame, stack[0]) is not assignable to 'projectname/issues/JavaBug$SupplierElementWithMapper'
  Current Frame:
    bci: @1
    flags: { }
    locals: { 'java/lang/String', 'projectname/issues/JavaBug' }
    stack: { 'java/lang/String' }
  Bytecode:
    0x0000000: 2ab4 0005 b400 102a b600 11b0          

    at projectname.issues.JavaBug.<init>(JavaBug.java:34)
    at projectname.issues.JavaBug.main(JavaBug.java:37)
C:\Users\JavIut\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1

问题出在供应商这一行。

this(()->html.doSomething(selector),Element::getData);

正在呼叫一个受保护的成员,我已经尝试过但没有成功。

this(()->{return html.doSomething(selector);},Element::getData);
this(()->JavaBug.super.html.doSomething(selector),Element::getData);
this(()->getHtml().doSomething(selector),Element::getData);/*ADDING IN THE CODE THE GETTER*/

但这行得通。

this(()->JavaBug.this.html.doSomething(selector),Element::getData);

如何找到此修复程序的版本?我需要通知我的团队。

在 IntelliJ idea 提示中指出 源代码与字节码不匹配

121 同样的问题https://discuss.newrelic.com/t/verifyerror-bad-type-on-operand-stack/50700

我认为解决方案可能是这样。

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8178444

FULL PRODUCT VERSION :
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
javac 1.8.0_121


ADDITIONAL OS VERSION INFORMATION :
OSX 10.12.4 (16E195)
16.5.0 Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64


A DESCRIPTION OF THE PROBLEM :
See code snippet below.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided code.

您的问题是 JDK-8184989:

的一个实例

The fix for JDK-8129740 is incomplete. It fails on cases when outer class is a subclass and the entities of it's [sic] superclass are referred to in lambda expression.

此错误已通过“修复 Version/s: 10”解决,我确实可以使用 JDK-9.0.4 重现您的问题,但不能使用 JDK-10.0.2 .

您已经通过评论反向链接了所引用的 JDK-8129740 is the bug report linked in