如何使用Javassist制作数组列表
2022-09-02 14:03:39
我想在检测期间将arrayList添加到方法中。我尝试了一下,如(Javassist在尝试添加一行来创建Map时不能CompileException)所述,但它会抛出一个不同的java.lang.VerifyError异常。
public void createInsertBefore(String scenarioName, String className, CtMethod method,
String insertBefore) throws CannotCompileException {
method.addLocalVariable("startTime", CtClass.longType);
StringBuilder bBuilder = new StringBuilder();
bBuilder.append("startTime = System.nanoTime();");
bBuilder.append("System.out.println(startTime);");
if((insertBefore!=null) && !insertBefore.isEmpty()){
bBuilder.append(insertBefore);
}
bBuilder.append("java.util.List metadata = new java.util.ArrayList();");
System.out.println(bBuilder.toString());
method.insertBefore(bBuilder.toString());
}
从 print 语句接收的输出为:
startTime = System.nanoTime();
System.out.println(startTime);
java.util.List metadata = new java.util.ArrayList();
但它会引发以下异常,
Exception in thread "main" java.lang.reflect.InvocationTargetException
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: java.lang.VerifyError
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:57)
... 6 more
FATAL ERROR in native method: processing of -javaagent failed
Aborted (core dumped)
情况与之前相同,但为什么会引发不同的异常。我做错了什么...请帮忙...
更新 1
行添加(我已经删除了一些打印行),插入之前,
startTime = System.nanoTime();
java.util.List metadata = new java.util.ArrayList();
插入
System.out.println("prepareStatement is running");
java.util.Map/*<String,String>*/ arbitraryMap = new java.util.HashMap/*String,String>*/();
arbitraryMap.put("query",$1);System.out.println(arbitraryMap);
插入后
System.out.println(System.nanoTime()-startTime);