java 从分支内部调用错误的 <init> 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25428317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:01:42  来源:igfitidea点击:

Bad <init> method call from inside of a branch

javamavenpowermockjavassist

提问by Lauri

After upgrading JDK to java7u65several unit-tests utilizing Mockitoand PowerMockstarted to fail with following causes:

将 JDK 升级到java7u65后,使用MockitoPowerMock 的几个单元测试开始失败,原因如下:

15:15:23,807 INFO  - Tests in error: 
15:15:23,810 INFO  -   initializationError(com.your.ClassHere): Bad <init> method call from inside of a branch

回答by Lauri

UPDATE

更新

There are newer java7 versions that fix this issue. As is written in reply to Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7 #525

有更新的 java7 版本可以解决这个问题。正如在回复Powermock / Javassist 中所写的那样,使用 -target 7 #525 为 JDK 1.7.0u65 和 1.8.0u11 创建非法构造函数

upgrading from java 7u71 to 7u75 fixed the problem

从 java 7u71 升级到 7u75 解决了这个问题

Rootcause

根本原因

Upon investigation I heard cries for help from all over the internet from all tools and languages that built upon JDK.

经过调查,我听到互联网上所有基于 JDK 的工具和语言都在寻求帮助。

Turns out the cause is new java bytecode standard that gets checked by new verifier. But unfortunately javassistsometimes is asked by powermockto produce changes into bytecode that are no longer accepted by this new shiny veryfier.

原来原因是新的 Java 字节码标准被新的验证器检查。但不幸的是,javassist有时会被powermock要求生成字节码的更改,而这些更改不再被这个新的闪亮的veryfier 接受。

Workaround (for those who cannot go with newer java)

解决方法(对于那些不能使用较新的 Java 的人)

As a workaround in JRebel blogthey suggested using -noverifyflag upon starting JVM However i found from Java 7 Bytecode Verifier: Huge backward step for the JVMblog post alternative workaround that works on java7: -XX:-UseSplitVerifier

作为JRebel 博客中的一种解决方法,他们建议在启动 JVM 时使用-noverify标志但是我从Java 7 Bytecode Verifier 中发现:JVM博客文章的巨大后退步骤适用于 java7 的替代解决方法:-XX:-UseSplitVerifier

As my tests run in some unaccessible server and are executed as part of maven build, i needed to find a way to pass that argument along with my project files. First workable solution i discovered is to add this parameter to configurtaion of surefire plugin in pom.xml as follows:

由于我的测试在一些无法访问的服务器上运行并作为 maven 构建的一部分执行,我需要找到一种方法来将该参数与我的项目文件一起传递。我发现的第一个可行的解决方案是将此参数添加到 pom.xml 中的surefire插件的配置中,如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <argLine>-XX:-UseSplitVerifier</argLine>
            </configuration>
        </plugin>
        <plugin>
    </plugins>
</build>

I suppose that on java8 one could use similar method to call tests with -noverifykey, but haven't had the chance to confirm that.

我想在 java8 上可以使用类似的方法来调用带有-noverify键的测试,但还没有机会确认这一点。

Other related resources to keep eye on

其他需要关注的相关资源

Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7.Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7 #525

Powermock / Javassist 使用 -target 7为 JDK 1.7.0u65 和 1.8.0u11 创建非法构造函数。Powermock / Javassist 使用 -target 7 为 JDK 1.7.0u65 和 1.8.0u11 创建非法构造函数 #525

回答by Senaka

Had the same issue with java8 and resolve it by using -noverify

与 java8 有相同的问题并使用 -noverify 解决它

回答by Chethaka Uduwarage

I had the same issue as the project was built on jdk1.8.0_51 and I had jdk1.8.0_11 in my machine. The project runs successfully after upgrading to jdk1.8.0_51 in the machine.

我遇到了同样的问题,因为该项目是在 jdk1.8.0_51 上构建的,而我的机器中有 jdk1.8.0_11。 本机升级到jdk1.8.0_51后项目运行成功。