【问题标题】:How to read data of CtMethod如何读取 CtMethod 的数据
【发布时间】: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


    【解决方案1】:

    如果您尝试阅读该方法的原始来源,javassist 并没有真正做到这一点。你需要一个 java 字节码反编译器。谷歌这个词。 jad 不错。

    【讨论】:

    • 好的,首先谢谢我决定使用Java Decompiler IntelliJ Plugin 因为它更新了呵呵:D
    • 确实如此。忘了那个:)
    猜你喜欢
    • 2023-03-22
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2012-03-09
    • 2011-01-26
    相关资源
    最近更新 更多