到目前为止,我在WebApplication中尝试了一些Bytecode操作。
现在,我需要在代码中的某些特定位置插入一些字节代码。我用switch
语句和method.inserAt();
进行了尝试,
但不会显示我想要的结果。
这是我的操纵代码
private void changeMethod(CtMethod method) throws NotFoundException, CannotCompileException {
if (method.hasAnnotation(Loggable.class)) {
int linenumber = 0;
int i = 0;
switch(i) {
case 0: linenumber = 50;
break;
case 1: linenumber = 71;
break;
case 2: linenumber = 91;
break;
}
method.insertAt(linenumber, "\n" +
" Thread thread1 = new Thread(new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
"\n" +
"\n" +
" threadLogger.info(\"Testlog\");\n" +
"\n" +
" try {\n" +
" threadLogger.logCall(Webservice.this.getClass().getMethod(\"startThread1\"), \"Thread1\");\n" +
" } catch (Exception e) {\n" +
" e.printStackTrace();\n" +
" }\n" +
"\n" +
"\n" +
" }\n" +
" });\n" +
" thread1.start();");
i++;
}
}
最佳答案
在这里实现:
int i = 0;
switch(i) { ...
i
始终为零。也许您想以这种价值做某事?关于java - 如何使用method.inserAt();正确地,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31614185/