使用JavaAssist创建方法时抛出java.lang.VerifyError

时间:2016-10-02 11:06:05

标签: java code-generation javassist verifyerror

我正在尝试使用JavaAssist版本3.12.1.GA来执行使用Java 8实现接口的Pojo的运行时代码生成。我在尝试创建返回类型为Object的方法时遇到了错误。

错误:

Caused by: java.lang.VerifyError: (class: person, method: getColumnByIndex signature: (I)Ljava/lang/Object;) Wrong return type in function

添加getColumnByIndex方法时会抛出

以下是完整的示例类:

public class Example {

    public interface Domain {
        public int getIdentifier();
        public Object getColumnByIndex(int i);
    }

    public static void main(final String[] args) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {

        final ClassPool pool = ClassPool.getDefault();
        final CtClass cc = pool.makeClass("Person");
        cc.addInterface(resolveCtClass(Domain.class));

        final CtField idField = new CtField(CtClass.intType, "id", cc);
        final CtMethod idGetter = CtNewMethod.getter("getId", idField);
        final CtMethod idSetter = CtNewMethod.setter("setId", idField);
        cc.addField(idField);
        cc.addMethod(idGetter);
        cc.addMethod(idSetter);

        final CtField firstNameField = new CtField(resolveCtClass(String.class), "firstName", cc);
        final CtMethod firstNameGetter = CtNewMethod.getter("getFirstName", firstNameField);
        final CtMethod firstNameSetter = CtNewMethod.setter("setFirstName", firstNameField);
        cc.addField(firstNameField);
        cc.addMethod(firstNameSetter);
        cc.addMethod(firstNameGetter);

        final CtMethod getIdentifier = CtNewMethod.make("public int getIdentifier () { return id; }", cc);
        cc.addMethod(getIdentifier);            

        final CtMethod getColumnByIndex = CtNewMethod.make(
                    "public Object getColumnByIndex(int i) {"
                    +   "switch (i) {"
                    +       "case 0:"
                    +           "return id;"
                    +       "case 1:"
                    +           "return firstName;"
                    +       "default: "
                    +           "throw new IllegalArgumentException(\"Tried getting column index i, but this column index does not exist\");"
                    +   "}"
                    + "}", cc);
        cc.addMethod(getColumnByIndex);
        final Class<?> dynamicClass = cc.toClass();

        final Domain domainImpl = (Domain) dynamicClass.newInstance();
        System.out.println(domainImpl.getIdentifier());
        System.out.println(domainImpl.getColumnByIndex(0));
    }

    private static CtClass resolveCtClass(final Class<?> clazz) throws NotFoundException {
        final ClassPool pool = ClassPool.getDefault();
        return pool.get(clazz.getName());
    }

如何解决java.lang.VeryifyError?

1 个答案:

答案 0 :(得分:1)

显然,id类型的int字段不会自动加入Integer,因此您可以手动执行此操作:

switch (i) {
  case 0: return Integer.valueOf(id);
  case 1: return firstName;
  default: throw new IllegalArgumentException("...");
}

或者您将所有内容转换为Integer,包括Domain.getIdentifier()的返回类型。 Java中的原始类型不是Object!存在包装类型以伪造JVM中的单个根层次结构,并且编译器在适当时静默地插入诸如Integer.valueOf(int)Integer.intValue()的调用,但有时抽象会泄漏给用户。

由于您编写的代码似乎是一个数据库访问的东西,我更喜欢第二个选项,以便所有内容都可以为空 - 否则代表一个尚未插入的记录,您必须依赖一些金丝雀价值,如零,-1或其他。

可运行的gist