Javassist如何调用超类

时间:2015-10-22 04:45:42

标签: java javassist

我有一个NFTypeSerializer类,我使用javassist创建NFTypeSerializer的子类

    CtClass superCc = pool.get(NFTypeSerializer.class.getName());
    CtClass cc = pool.makeClass("HotelSerializer", superCc);

我想重写一个在NFTypeSerializer中定义的方法doSerialize()

    cc.addMethod(CtNewMethod.make("protected void doSerialize(Hotel value, NFSerializationRecord rec) {\n" +
            "        serializePrimitive(rec, \"name\", value.getName());\n" +
            "        serializePrimitive(rec, \"price\", value.getPrice());\n" +
            "    }", cc));

但错误发生

  

线程“main”中的异常javassist.CannotCompileException:[source   错误]   serializePrimitive(com.netflix.zeno.serializer.NFSerializationRecord,java.lang.String中,双)   在HotelSerializer找不到   javassist.CtNewMethod.make(CtNewMethod.java:79)at   javassist.CtNewMethod.make(CtNewMethod.java:45)at   test.demo.javassist.ComponentBuilder.buildHotelSerializer(ComponentBuilder.java:60)     在   test.demo.javassist.ComponentBuilder.main(ComponentBuilder.java:88)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)   引起:编译错误:   serializePrimitive(com.netflix.zeno.serializer.NFSerializationRecord,java.lang.String中,双)   在HotelSerializer找不到   javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:723)     at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:688)     在   javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)     在javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)at   javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)at   javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)at   javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)at   javassist.compiler.CodeGen.atStmnt(CodeGen.java:351)at   javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)at   javassist.compiler.CodeGen.atMethodBody(CodeGen.java:292)at at   javassist.compiler.CodeGen.atMethodDecl(CodeGen.java:274)at at   javassist.compiler.ast.MethodDecl.accept(MethodDecl.java:44)at   javassist.compiler.Javac.compileMethod(Javac.java:169)at   javassist.compiler.Javac.compile(Javac.java:95)at   javassist.CtNewMethod.make(CtNewMethod.java:74)... 8更多

方法serializePrimitive()是在超类中定义的,但是我不能在CtNewMethod中调用它?

1 个答案:

答案 0 :(得分:1)

请检查为serializePrimitive()声明的构造函数。您可能传递了错误类型的值或错误的序列。

提供的参数 -

serializePrimitive(NFSerializationRecord rec, String fieldName, double value)

预期参数

serializePrimitive(NFSerializationRecord rec, String fieldName, Object value)