[英]How to use method.inserAt(); correctly
I try some Bytecode manipulation in my WebApplication and so far, so good. 到目前为止,我在WebApplication中尝试了一些Bytecode操作。 Now I need to inject some byte code on some specific places in my code.
现在,我需要在代码中的某些特定位置插入一些字节代码。 I tried this with a
switch
statement and method.inserAt();
我尝试使用
switch
语句和method.inserAt();
, but it won't show the results I want. ,但不会显示我想要的结果。
Here's my code for the manipulation 这是我的操纵代码
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++;
}
}
Realise that here: 在这里实现:
int i = 0;
switch(i) { ...
i
is always going to be zero. i
将永远为零。 Maybe you want to do something with that value? 也许您想以这种价值做某事?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.