Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the code of a method from its Class

Tags:

java

class

I am working with this awesome little piece of code, and I need some help figuring it out.

Basically it has a big byte array, which is used to load a new class:

    Class class = class1.loadClass(name, byteArray);
    class.getMethod("run", new Class[0]).invoke(null, new Object[0]);

This is loadClass:

    public Class<?> loadClass(String paramString, byte[] paramArrayOfByte) throws ClassNotFoundException
        return defineClass(paramString, paramArrayOfByte, 0, paramArrayOfByte.length);

And then it calls the 'run' in this class. Now, besides this being an awesome way to hide your classes, how do I get a look into this class? I have the actual class in 'class', but I don't get further than the attributes and their values and the function names. I want to know what 'run' actually does. Is there any way to somehow print the content of this method or whatever? Or maybe I can transform the byte array containing the class in something readable?

Thanks!

like image 259
JvanDalfsen Avatar asked Dec 03 '25 02:12

JvanDalfsen


1 Answers

What if you write this byteArray in a .class file and open it with a Java Decompiler tool (like jd-gui) ?

like image 93
Gaël J Avatar answered Dec 05 '25 16:12

Gaël J