如何反射打印方法体? [英] How do I print the method body reflectively?

查看:107
本文介绍了如何反射打印方法体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有

private static void getMethods(Class<? extends Object> clazz) {
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method aMethod : declaredMethods) {
      aMethod.setAccessible(true);

      // Print the declaration
      System.out.print(Modifier.toString(aMethod.getModifiers()) + " "
          + aMethod.getReturnType().getSimpleName() + " " + aMethod.getName());

      // Get Parameter Types
      getParameters(aMethod);

      //Empty Body
      System.out.println("{}\n");
    }
  }

反射性地打印大部分信息但创建一个空体。如何添加Java的反射特性来打印方法体?

Which prints most information reflectively but creates an empty body. How do I add to the reflective nature of Java to print the method body?

推荐答案


如何做我添加了Java的反射性质来打印方法体?

How do I add to the reflective nature of Java to print the method body?

我很害怕,我很害怕。

首先,原始源代码很可能对正在运行的程序不可用。通常,开发人员不在二进制JAR中包含源代码。 (即使他们这样做,也不能保证它们将成为真正的来源。)

For a start, the original source code is most likely to be unavailable to a running program. As a rule, developers do not include source code in the binary JARs. (And even if they do, there is not guarantee that they will be the "real" sources.)

您通常可以通过翻译FQN来获取该类的字节码将类转换为字节码文件并使用类classloader将文件作为资源流加载。但是不能保证你用这种方式获得的字节码与加载的字节码相同。 (由于各种原因,一些类加载器会乱码字节码。)

You can usually obtain the bytecodes for the class by translating the FQN for the class into the bytecode file and using the classes classloader to load the file as a resource stream. But it is not guaranteed that the bytecodes you get this way will be the same as what were loaded. (Some classloaders mess around with the bytecodes for various reasons.)

假设可以获取真正的字节码,最后一步将是或者将它们显示为原始,或者将它们反汇编或反编译为更具可读性的东西。您应该能够使用 javap 进行反汇编,但反编译需要使用第三方产品。当然,反编译的代码看起来与原始代码有很大的不同,如果字节码被混淆了,那么源代码几乎是不可读的。

Assuming that you can get the real bytecodes, the last step will be to either display them raw, or disassemble or decompile them to something more readable. You should be able to use javap to do a disassembly, but decompilation would entail using a third party product. Of course, the decompiled code will look significantly different to the original code, and if the bytecodes have been obfuscated they source code will be pretty much unreadable.

这篇关于如何反射打印方法体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆