Search code examples
javajavassist

Javassist how to invoke method defined in super class


I have a class NFTypeSerializer and I use javassist create a subclass of NFTypeSerializer

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

and I want to override a method doSerialize() which is defined in NFTypeSerializer

    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));

but the error occur

Exception in thread "main" javassist.CannotCompileException: [source error] serializePrimitive(com.netflix.zeno.serializer.NFSerializationRecord,java.lang.String,double) not found in HotelSerializer at javassist.CtNewMethod.make(CtNewMethod.java:79) at javassist.CtNewMethod.make(CtNewMethod.java:45) at test.demo.javassist.ComponentBuilder.buildHotelSerializer(ComponentBuilder.java:60) at test.demo.javassist.ComponentBuilder.main(ComponentBuilder.java:88) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: compile error: serializePrimitive(com.netflix.zeno.serializer.NFSerializationRecord,java.lang.String,double) not found in HotelSerializer at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:723) at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:688) at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157) at 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 javassist.compiler.CodeGen.atMethodDecl(CodeGen.java:274) 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 more

the method serializePrimitive() is defined in super class but I can't invoke it in CtNewMethod ?


Solution

  • Please check the constructors declared for serializePrimitive(). You might be passing the wrong types of values or in the wrong sequence.

    Supplied paramters-

    serializePrimitive(NFSerializationRecord rec, String fieldName, double value)

    Expected parameters-

    serializePrimitive(NFSerializationRecord rec, String fieldName, Object value)