简体   繁体   English

如何反射性地打印方法主体?

[英]How do I print the method body reflectively?

Right now I have现在我有

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");
    }
  }

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?如何添加 Java 的反射特性以打印方法主体?

With considerable difficulty, I'm afraid.恐怕难度很大。

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.通常,开发人员不会在二进制 JAR 中包含源代码。 (And even if they do, there is not guarantee that they will be the "real" sources.) (即使他们这样做,也不能保证他们将是“真正的”来源。)

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.您通常可以通过将类的 FQN 转换为字节码文件并使用类类加载器将文件作为资源流加载来获取类的字节码。 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.) (一些类加载器出于各种原因处理字节码。)

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.您应该能够使用javap进行反汇编,但反编译需要使用第三方产品。 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.当然,反编译后的代码看起来与原始代码有很大不同,如果字节码被混淆,它们的源代码将几乎无法阅读。

Java doesn't have that information at runtime. Java 在运行时没有这些信息。 The only thing you could print would be bytecode.您唯一可以打印的是字节码。

The only way to do something like that is to write/use a decompiler.这样做的唯一方法是编写/使用反编译器。 That involves reading the byte-code directly, which is not a standard function of the ReflectionAPI.这涉及直接读取字节码,这不是 ReflectionAPI 的标准功能。 The source code as represented in the original source file is gone, and it may not be possible to completely reproduce it as it was from byte code.原始源文件中表示的源代码已经消失,并且可能无法像从字节代码那样完全重现它。 (For example, Generics would be lost, as well as use of an enhanced for loop wouldn't be distinguishable and I'm sure there are others). (例如,泛型会丢失,并且无法区分使用增强的 for 循环,我相信还有其他的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM