简体   繁体   English

构造函数必须在返回方法之前调用super()或this()

[英]Constructor must call super() or this() before return in method

I am getting this error: 我收到此错误:

Exception in thread "Thread-0" java.lang.VerifyError: Constructor must call super() or this() before return in method JGame.Util.KeyboardMap.<init>()V at offset 0
        at JGame.Room.Room.keyboardEventTests(Room.java:81)
        at JGame.Room.Room.run(Room.java:54)
        at java.lang.Thread.run(Thread.java:722)

When my application loads, it calls this method right away (KeyboardMap.map is an empty HashMap). 当我的应用程序加载时,它立即调用此方法(KeyboardMap.map是一个空的HashMap)。

Here is the Method (Line 54 calls this method this.keyboardEventTests(); ): 这是方法(第54行调用此方法this.keyboardEventTests(); ):

protected void keyboardEventTests(){
    for(Map.Entry ap : KeyboardMap.map.entrySet()){ // Line 81
        Mapping mp = (Mapping)ap.getValue();
        if(mp.doing){
            mp.run();
        }
    }
}

And here is the KeyboardMap class. 这是KeyboardMap类。

package JGame.Util;

import java.util.HashMap;
import java.util.Map;

public class KeyboardMap{

    public static Map<String, Mapping> map = new HashMap<>();

    public static void set(String key, Boolean value, Runnable run){
        Mapping mp = new Mapping();
        mp.doing = value;
        mp.run = run;
        KeyboardMap.map.put(key, mp);
    }

    public static Mapping get(String key){
        return KeyboardMap.map.get(key);
    }
}

Why am I getting that error, and how can I get rid of it? 为什么我会收到这个错误,我怎么能摆脱它呢?

Why am I getting that error, and how can I get rid of it? 为什么我会收到这个错误,我怎么能摆脱它呢?

The big clue is that this is a VerifyError , not a compilation error. 最大的线索是,这是一个VerifyError ,而不是编译错误。 What this means is that the JVM has found a bytecode file in which one of the constructors is not chaining properly. 这意味着JVM找到了一个字节码文件,其中一个构造函数没有正确链接。 These are (in effect) a malformed bytecodes. 这些(实际上)是格式错误的字节码。

How can that happen? 怎么会发生这种情况?

  • Well it CAN'T happen in a Java class that is (just) compiled in the normal way. 好吧,它不会发生在以正常方式(仅)编译的Java类中。 The compiler will automatically insert an implicit super() call into any constructor that doesn't explicitly chain. 编译器将自动将隐式super()调用插入到未明确链接的任何构造函数中。

  • If this is Java code, then either: 如果这是Java代码,那么:

    • the class was compiled using broken compiler (unlikely!), or 该类是使用损坏的编译器编译的(不太可能!),或

    • something has tweaked the bytecodes after compilation. 有些东西在编译后调整了字节码。

  • If it was some other language, then the first suspect would be the "other language to bytecode" compilation process. 如果它是其他语言,那么第一个嫌疑人将是“字节码的其他语言”编译过程。

I think you are getting this problem because your unit tests is using a mocking framework, and the mocking framework is using "byte code engineering" to inject something into the classes under test. 我认为你遇到了这个问题,因为你的单元测试使用了一个模拟框架,而模拟框架正在使用“字节代码工程”将一些内容注入到被测试的类中。 The code that is doing this has "made a mistake" and the result is bytecodes that won't compile. 执行此操作的代码“出错了”,结果是无法编译的字节码。


This was apparently fixed by a rebuild, but that doesn't contradict this explanation. 这显然是通过重建来解决的,但这与此解释并不矛盾。 A rebuild could clear out broken instrumentation code injected by the mocking framework. 重建可以清除由模拟框架注入的破坏的检测代码。 And next time around, the framework could "get it right". 下一次,框架可以“正确”。

Just override the default contructor by adding 只需通过添加覆盖默认的构造函数

public KeyboardMap() {
}

to the KeyboardMap class. 到KeyboardMap类。 It will work. 它会工作。

I had the same problem, and it was caused by a very strange thing: In NetBeans, i use editor fold to fold long codes: 我有同样的问题,它是由一个非常奇怪的事情引起的:在NetBeans中,我使用编辑器折叠来折叠长代码:

// <editor-fold desc="SOME DESCRIPTION">
  ...
  ...
// </editor-fold>

And it appeared that in one of such my folds i had forgotten to write the beginning of fold, like this: 似乎在我的一个折叠中,我忘了写折叠的开头,像这样:

  ...
  ...
// </editor-fold>

Correcting this solved my problem . 纠正这个解决了我的问题 Strange, because this is just a comment and it's for NetBeans 奇怪,因为这只是一个评论,它适用于NetBeans

在我的例子中,在buildTypes下关闭build.gradle中的minify有帮助。

minifyEnabled false

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

相关问题 java.lang.VerifyError:构造函数必须在返回之前调用super()或this() - java.lang.VerifyError: Constructor must call super() or this() before return 构造函数必须调用super()或this() - Constructor must call super() or this() 构造函数调用必须是 super() 构造函数中的第一条语句 - Constructor call must be the first statement in a constructor in super() 对super()的调用必须是构造函数体中的第一个语句 - call to super() must be the first statement in constructor body 何时必须使用对超级构造函数的显式调用? - When must an explicit call to super constructor be used? 对 super 的调用必须是构造函数中的第一条语句,但它是 - Call to super must be first statement in the constructor, but it is java.lang.VerifyError :(类:ea / Individual,方法: <init> 签名:(I)V)构造函数必须调用super()或this() - java.lang.VerifyError: (class: ea/Individual, method: <init> signature: (I)V) Constructor must call super() or this() 使用super()时,构造函数调用必须是构造函数中的第一条语句; - Constructor Call must be the first statement in a constructor while using super(); 方法必须在 Netbeans 中调用 super() 错误 - method must call super() error in Netbeans Zebra.java:3:错误:对super的调用必须是构造函数中的第一条语句 - Zebra.java:3: error: call to super must be first statement in constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM