Search code examples
javadynamicjavap

Getting runtime class information from the web? Something like javap but at runtime on class loaded in memory


I am working with Websphere and complicated classloading issues. I want to be able to download or print information that would normally get printed by javap (the methods, etc).

I may also need to get the raw binary class data, to perform a binary diff.

How would you do this?


Solution

  • You could write a Servlet or JMX MBean that exposes the class to the your client.

    Servlet:

    String resourceParameter = ...;
    OutputStream out = ...:
    InputStream input = Thread.currentThread().getContextClassLoader()
       .getResourceAsStream(resourceParameter)
    write(input, out);
    

    Client:

    GET http://host/DiagnosticServlet?resource=your/ClassName.class
    

    The resource parameter has to be your class file your.ClassName -> your/ClassName.class. You can then save the file and use javap.

    (I think the MBean has to encode your class file into a string (e.g. Base 64) as byte[] is not supported. But I'm not sure about that. The rest would be the same.)

    If this will be deployed in production some form of authentication should be configured.