views:

63

answers:

3

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?

+2  A: 

Java doesn't have that information at runtime. The only thing you could print would be bytecode.

Chris Lercher
+1  A: 

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. 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).

Yishai
A: 

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

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. (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. 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. 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.

Stephen C