Open tangzhenhuang opened 5 years ago
The type parameter T is java.lang.Object at the byte code level. So you must insert explicit type cast before getMockData().
But when I changed the code to:
"{" +
"if (!isRecord()) {" +
" return ($r)getMockData(\"%s\", $args, $type);" +
"} "
"}";
It throws the following exception, doesn't $r work?
Caused by: javassist.CannotCompileException: [source error] no such class: $r
at javassist.CtBehavior.insertBefore(CtBehavior.java:774)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at com.seewo.honeycomb.ttm.agent.bytecode.EMethod.insertBefore(EMethod.java:27)
... 48 more
Caused by: compile error: no such class: $r
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:468)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:412)
at javassist.compiler.MemberResolver.lookupClassByName(MemberResolver.java:315)
at javassist.compiler.MemberResolver.resolveClassName(MemberResolver.java:502)
at javassist.compiler.TypeChecker.resolveClassName(TypeChecker.java:132)
at javassist.compiler.TypeChecker.atCastExpr(TypeChecker.java:546)
at javassist.compiler.JvstTypeChecker.atCastExpr(JvstTypeChecker.java:104)
at javassist.compiler.ast.CastExpr.accept(CastExpr.java:55)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.compileExpr(CodeGen.java:229)
at javassist.compiler.CodeGen.atReturnStmnt2(CodeGen.java:615)
at javassist.compiler.JvstCodeGen.atReturnStmnt(JvstCodeGen.java:425)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:363)
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.atIfStmnt(CodeGen.java:398)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:355)
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.Javac.compileStmnt(Javac.java:569)
at javassist.CtBehavior.insertBefore(CtBehavior.java:754)
... 50 more
The exception says the type of $r was not found. I'm not sure what is the type of $r but you should check it is in the class path.
I think this is because "$r" doesn't have the effect of escaping, because even if I add Class.forName($type.getName()) before this, it doesn't help. According to the documentation, $r doesn't work. It should have the effect of escaping.
OK, you're right. $r was not available in the code passed to insertBefore
.
I've fixed this bug. Could you try javassist.jar
available from HEAD of the master branch.
I have tried it, this bug no longer exists, thanks.
Recently, I am doing a mock function on methods, one of which is to modify the bytecode before the method starts. I inserted the following code with the insertBefore method:
Among them, the definition of the getMockData method is:
But when it started, it throws the following exceptions:
When I delete the "insertBefore" expression, it was normal again. So I believe there are some problems with this statement("insertBefore" with return expression).
Thanks.