尝试添加一行以创建Map

时间:2015-10-22 11:27:15

标签: java instrumentation javassist

嗯试图检测方法来执行以下任务。       任务 - 创建地图并将值插入地图 添加System.out.println行不会导致任何异常。但是当我添加该行来创建Map时,由于缺少 ; ,它会抛出 cannotCompileException 。当我打印最后一个字符串时,似乎没有错过任何字符串。我在这做错了什么。

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

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

  

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

(有没有办法调试这类问题。)请帮忙.....

1 个答案:

答案 0 :(得分:2)

Javassist的编译器不支持泛型。删除或评论它们:

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

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

后者仅对自己有用,当然,它对Javassist没有特殊意义。