简体   繁体   English

内部类&lambda的java.lang.VerifyError

[英]java.lang.VerifyError with inner class & lambda

The follow code compiles but causes a java.lang.VerifyError . 以下代码编译但导致java.lang.VerifyError The error occurs even if the run() method is not executed. 即使未执行run()方法,也会发生错误。

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;


public class TestCase {

    public static void main(String[] args) {
        new TestCase().run();
    }

    public void run() {
        class Inner {

        }
        Map<String, Inner> map = new HashMap<>();
        Function<String, Inner> function = (name) -> {
            Inner i = map.get(name);
            if (i == null) {
                i = new Inner();
                map.put(name, i);
            }
            return i;

        };
        function.apply("test");
    }
}

The Error: 错误:

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    TestCase.lambda$0(Ljava/util/Map;Ljava/lang/String;)LTestCase$1Inner; @20: invokespecial
  Reason:
    Type 'java/util/Map' (current frame, stack[2]) is not assignable to 'TestCase'
  Current Frame:
    bci: @20
    flags: { }
    locals: { 'java/util/Map', 'java/lang/String', 'TestCase$1Inner' }
    stack: { uninitialized 15, uninitialized 15, 'java/util/Map' }
  Bytecode:
    0000000: 2a2b b900 2d02 00c0 0032 4d2c c700 15bb
    0000010: 0032 592a b700 344d 2a2b 2cb9 0037 0300
    0000020: 572c b0                                
  Stackmap Table:
    append_frame(@33,Object[#50])

    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)

However, if I move the 'Inner' class to be an inner class of TestCase (instead of declared in a method), the error goes away. 但是,如果我将'Inner'类移动为TestCase的内部类(而不是在方法中声明),则错误消失。 Or, if I use an anonymous class to define the Function, the error goes away. 或者,如果我使用匿名类来定义函数,则错误消失。 It seems to be an issue with a class declared in the method and the use of a lamba. 对于在方法中声明的类和lamba的使用,这似乎是一个问题。

Is this a JVM bug? 这是一个JVM错误吗? Or am I missing something? 或者我错过了什么? I am using Oracle's Java 8. The error happens both on the command line and within Eclipse 4.4. 我正在使用Oracle的Java 8.错误发生在命令行和Eclipse 4.4中。

EDIT: I upgraded to the latest JDK: java version "1.8.0_11" Java(TM) SE Runtime Environment (build 1.8.0_11-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode) 编辑:我升级到最新的JDK:java版“1.8.0_11”Java(TM)SE运行时环境(版本1.8.0_11-b12)Java HotSpot(TM)64位服务器VM(版本25.11-b03,混合模式)

When compile via javac manually and run it works fine. 当通过javac手动编译并运行它工作正常。 If I run the class compiled by Eclipse, it doesn't. 如果我运行Eclipse编译的类,则不会。 So now I suspect that the Eclipse compiler has a bug. 所以现在我怀疑Eclipse编译器有一个bug。

verify error is thrown when your compiler generates a code that is not verifiable by verifier. 当编译器生成无法通过验证程序验证的代码时,将引发验证错误。 when the "verifier" detects that a class file, though well formed, contains some sort of internal inconsistency or security problem. 当“验证者”检测到类文件虽然格式良好,但包含某种内部不一致或安全问题时。 So it is clearly an issue with your eclipse compiler as you have suggested. 因此,正如您所建议的那样,这显然是您的eclipse编译器的问题。 it is unable to properly compile these constructs. 它无法正确编译这些结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM