构造函数调用的java.lang.VerifyError

时间:2015-09-29 18:38:32

标签: java jvm bytecode jvm-languages

我最近开始为自己的语言编写一个编译器,但是当我调用main方法时它开始抛出异常。它适用于我的其他测试类,但它不想使用这个。据我所知,在这个类和其他类之间调用方法的方式并没有什么不同。这是例外。

Exception in thread "main" java.lang.VerifyError: (class: FizzBuzz/FizzBuzz, method: <init> signature: ()V) Incompatible object argument for function call
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)

这里是javap输出的字节码

public final class FizzBuzz.FizzBuzz {
  public FizzBuzz.FizzBuzz();
    Code:
       0: new           #16                 // class Lang/Int
       3: dup
       4: lconst_0
       5: invokespecial #19                 // Method Lang/Int."<init>":(J)V
       8: astore_1
       9: aload_1
      10: new           #16                 // class Lang/Int
      13: dup
      14: ldc2_w        #20                 // long 100l
      17: invokespecial #19                 // Method Lang/Int."<init>":(J)V
      20: invokevirtual #25                 // Method Lang/Int._lessThan:(LLang/Number;)LLang/Boolean;
      23: getfield      #31                 // Field Lang/Boolean.value:Z
      26: ifeq          140
      29: ldc           #33                 // String
      31: astore_2
      32: aload_1
      33: new           #16                 // class Lang/Int
      36: dup
      37: ldc2_w        #34                 // long 3l
      40: invokespecial #19                 // Method Lang/Int."<init>":(J)V
      43: invokevirtual #39                 // Method Lang/Int._modulus:(LLang/Int;)LLang/Int;
      46: new           #16                 // class Lang/Int
      49: dup
      50: lconst_0
      51: invokespecial #19                 // Method Lang/Int."<init>":(J)V
      54: invokevirtual #43                 // Method Lang/Int._equals:(Ljava/lang/Object;)LLang/Boolean;
      57: getfield      #31                 // Field Lang/Boolean.value:Z
      60: ifeq          70
      63: aload_2
      64: ldc           #45                 // String Fizz
      66: invokevirtual #51                 // Method Lang/String._add:(LLang/String;)LLang/String;
      69: astore_2
      70: nop
      71: aload_1
      72: new           #16                 // class Lang/Int
      75: dup
      76: ldc2_w        #52                 // long 5l
      79: invokespecial #19                 // Method Lang/Int."<init>":(J)V
      82: invokevirtual #39                 // Method Lang/Int._modulus:(LLang/Int;)LLang/Int;
      85: new           #16                 // class Lang/Int
      88: dup
      89: lconst_0
      90: invokespecial #19                 // Method Lang/Int."<init>":(J)V
      93: invokevirtual #43                 // Method Lang/Int._equals:(Ljava/lang/Object;)LLang/Boolean;
      96: getfield      #31                 // Field Lang/Boolean.value:Z
      99: ifeq          109
     102: aload_2
     103: ldc           #55                 // String Buzz
     105: invokevirtual #51                 // Method Lang/String._add:(LLang/String;)LLang/String;
     108: astore_2
     109: nop
     110: aload_2
     111: invokevirtual #59                 // Method Lang/String.isEmpty:()LLang/Boolean;
     114: invokevirtual #62                 // Method Lang/Boolean._not:()LLang/Boolean;
     117: getfield      #31                 // Field Lang/Boolean.value:Z
     120: ifeq          127
     123: aload_2
     124: invokestatic  #68                 // Method Lang/System.println:(Ljava/lang/Object;)V
     127: nop
     128: aload_1
     129: invokestatic  #68                 // Method Lang/System.println:(Ljava/lang/Object;)V
     132: aload_1
     133: invokevirtual #72                 // Method Lang/Int._increment:()LLang/Int;
     136: astore_1
     137: goto          9
     140: nop
     141: aload_0
     142: invokespecial #10                 // Method java/lang/Object."<init>":()V
     145: return
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
        141       5     0  this   LFizzBuzz/FizzBuzz;
          8     138     1     i   LLang/Int;
         31     115     2   str   LLang/String;

  public static void main(java.lang.String[]);
    Code:
       0: aload_0
       1: invokestatic  #80                 // Method Lang/System.setArguments:([Ljava/lang/String;)V
       4: new           #4                  // class FizzBuzz/FizzBuzz
       7: invokespecial #81                 // Method "<init>":()V
      10: return
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      11     0  args   [Ljava/lang/String;
}

1 个答案:

答案 0 :(得分:4)

在您的代码中,您有序列

  29: ldc           #33             // String
  31: astore_2
…
  63: aload_2
  64: ldc           #45             // String Fizz
  66: invokevirtual #51             // Method Lang/String._add:(LLang/String;)LLang/String;

当然,仅仅因为您的语言具有不同的String类型,并不会导致JVM在遇到ldc指令时使用您的自定义类型。您必须先将java/lang/String指令创建的ldc实例转换为Lang/String实例。然后,您可以调用add方法。

如果您的自定义String是不可变的,并且您希望实现语言String的编译时常量,则可以使用指向invokedynamic常量的java/lang/String指令作为静态参数。然后,引导方法可以将其转换为您的字符串类型并返回常量方法句柄。由于bootstrap方法仅在第一次执行invokedynamic指令时调用一次,因此您将获得所需的常量行为。