尝试添加行以创建Map时出现Javassist CannotCompileException [英] Javassist CannotCompileException when trying to add a line to create a Map

查看:364
本文介绍了尝试添加行以创建Map时出现Javassist CannotCompileException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

um尝试检测执行以下任务的方法.任务-创建地图并将值插入地图添加System.out.println行不会导致任何异常.但是,当我添加行以创建Map时,由于 missing ; ,它会抛出 cannotCompileException .当我打印最后的字符串时,它似乎不会丢失任何字符串.我在这里做错了什么.

um trying to instrument a method to do the following task. Task - Create a Map and insert values to the map Adding System.out.println lines wouldn't cause any exception. But when i add the line to create the Map, it throws a cannotCompileException due to a missing ;. When i print the final string it doesn't seem to miss any. What am i doing wrong here.

public void createInsertAt(CtMethod method, int lineNo, Map<String,String> parameterMap)
            throws CannotCompileException {
        StringBuilder atBuilder = new StringBuilder();
        atBuilder.append("System.out.println(\"" + method.getName() + " is running\");");
        atBuilder.append("java.util.Map<String,String> arbitraryMap = new java.util.HashMap<String,String>();");

        for (Map.Entry<String,String> entry : parameterMap.entrySet()) {
        }
        System.out.println(atBuilder.toString());
        method.insertAt(1, atBuilder.toString());
    }

通过打印字符串生成器的输出获得的字符串是

String obtained by printing the output of string builder is,

System.out.println("prepareStatement是正在运行"); java.util.Map任意地图=新java.util.HashMap();

System.out.println("prepareStatement is running");java.util.Map arbitraryMap = new java.util.HashMap();

收到的异常是

javassist.CannotCompileException: [source error] ; is missing
    at javassist.CtBehavior.insertAt(CtBehavior.java:1207)
    at javassist.CtBehavior.insertAt(CtBehavior.java:1134)
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.createInsertAt(InstrumentationClassTransformer.java:126)
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.instrumentMethod(InstrumentationClassTransformer.java:100)
    at org.wso2.das.javaagent.instrumentation.InstrumentationClassTransformer.transform(InstrumentationClassTransformer.java:37)
    at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:424)
    at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
    at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
    at org.wso2.das.javaagent.instrumentation.Agent.premain(Agent.java:39)
    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 sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:382)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:397)
Caused by: compile error: ; is missing
    at javassist.compiler.Parser.parseDeclarationOrExpression(Parser.java:594)
    at javassist.compiler.Parser.parseStatement(Parser.java:277)
    at javassist.compiler.Javac.compileStmnt(Javac.java:567)
    at javassist.CtBehavior.insertAt(CtBehavior.java:1186)
    ... 15 more

(有什么方法可以调试此类问题.)请帮忙.....

(Is there any way to debug these kind of issues.) Some help please.....

推荐答案

Javassist的编译器不支持泛型.删除它们或将其注释掉:

Javassist's compiler doesn't support generics. Either remove or comment them out:

.append("java.util.Map arbitraryMap = new java.util.HashMap();")

.append("java.util.Map/*<String,String>*/ arbitraryMap = new java.util.HashMap/*<String,String>*/();")

后者只是对自己有用的注释,当然,对于Javassist来说并没有特殊的意义.

The latter is useful as comment for yourself only, of course, it has no special meaning for Javassist.

这篇关于尝试添加行以创建Map时出现Javassist CannotCompileException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆