Javassist 如何调用 super class 中定义的方法
Javassist how to invoke method defined in super class
我有一个 class NFTypeSerializer 并且我使用 javassist 创建了一个 NFTypeSerializer
的子class
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));
但出现错误
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
方法 serializePrimitive() 在 super class 中定义,但我无法在 CtNewMethod 中调用它?
请检查为 serializePrimitive() 声明的构造函数。您可能传递了错误类型的值或错误的顺序。
提供的参数-
serializePrimitive(NFSerializationRecord rec, String fieldName, double value)
预期参数-
serializePrimitive(NFSerializationRecord rec, String fieldName, Object value)
我有一个 class NFTypeSerializer 并且我使用 javassist 创建了一个 NFTypeSerializer
的子class 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));
但出现错误
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
方法 serializePrimitive() 在 super class 中定义,但我无法在 CtNewMethod 中调用它?
请检查为 serializePrimitive() 声明的构造函数。您可能传递了错误类型的值或错误的顺序。
提供的参数-
serializePrimitive(NFSerializationRecord rec, String fieldName, double value)
预期参数-
serializePrimitive(NFSerializationRecord rec, String fieldName, Object value)