【发布时间】:2015-07-28 07:08:06
【问题描述】:
问候我想读取我尝试使用 javassist 和 java 代理进行字节码操作更改的方法的数据。 原因是我的程序(一个 webApplication)不能工作(javassist.CannotCompileException: [source error] ) is missing what is this?) 目前没有人可以帮助我。 所以我不想在我的 Methode 里面有什么可能会产生错误......所以我想知道我是否可以读取和打印 CtMethod 的内容
我的代码
private byte[] transformClass(Class classToTransform, byte[] b, String className) {
if (className.startsWith("de/example")) {
ClassPool pool = ClassPool.getDefault();
CtClass cl = null;
try {
cl = pool.makeClass(new ByteArrayInputStream(b));
} catch (IOException e) {
e.printStackTrace();
}
try {
assert cl != null;
for (CtMethod ctMethod : cl.getMethods()) {
changeMethod(ctMethod);
System.out.println(ctMethod.getMethodInfo());
System.out.println(ctMethod.getMethodInfo2());
}
b = cl.toBytecode();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cl != null) {
cl.detach();
}
}
}
return b;
}
private void changeMethod(CtMethod method) throws NotFoundException, CannotCompileException {
if (method.hasAnnotation(Loggable.class)) {
System.out.println(method.getMethodInfo());
System.out.println(method.getMethodInfo2());
method.insertBefore(" startTime = 0;\n" +
" startTime = System.currentTimeMillis();\n" +
" final de.example.webservice.ws.TestFolder.Logging threadLogger = de.example.webservice.ws.TestFolder.Logging.getInstance();\n" +
" Thread thread1 = new Thread(new Runnable(){\n" +
" @Override\n" +
" public void run() {\n" +
" threadLogger.info(\"Testlog\");\n" +
" try {\n" +
" threadLogger.logCall(Webservice.this.getClass().getMethod(\"startThread0\"),\"Thread\");\n" +
" } catch (Exception e) {\n" +
" e.printStackTrace();\n" +
" }\n" +
" }\n" +
" });\n" +
" thread1.start();");
}
}
我只是谷歌的方法,可以从 .txt 文件等文件中读取数据,但这对我的问题没有用。
【问题讨论】:
标签: java javassist bytecode-manipulation javaagents