instantiate an object and invoking a method via a

2019-07-29 10:18发布

问题:

I have a .class file and know nothing about the .class file, including class name, fields or methods. What I need to do is finding out everything about the class and instantiate an object of the class.

Remember that we know nothing about inside the .class file, after calling getDeclareMethod() ,getDeclareFields() and getDeclareConstructor(), I know that it contains a filed of String str and a void method with one String parameter which try to assign a name to the variable str and a pure constructor without any parameters.

So the question is how to instantiate an object and invoke the method to assign a String to str? I know how to do it when the declarations of class or method are in the same file, but this one is an unknown .class file, I turn to you guys for help.

回答1:

First off, you need to convert the .class file into a Class object. You can do this with URLClassLoader.

Let's assume you have a File classFile and a String className.

try {
    URLClassLoader classLoader = new URLClassLoader(new URL[]{classFile.toURI().toURL()});
    Class<?> clazz = classLoader.loadClass(className);
} catch (Exception e) {
    // something went wrong..
    e.printStackTrace();
}

Now you have your class file stored in a Class object, but how can objects of its type be creating and have methods invoked from? Java reflection, of the java.lang.reflect package, is useful in this situation.

Let's assume you have your Class<?> object stored in a variable named clazz, a String methodName of the method name, and a String toSet of the method argument.

try {
    Object instance = clazz.newInstance();
    Method method = clazz.getDeclaredMethod(methodName, String.class);
    method.setAccessible(true);
    method.invoke(instance, toSet);
} catch (Exception e) {
    // something went wrong..
    e.printStackTrace();
}

Note that if the method name is unknown, you will have to create a way of identifying it, which can be done either via reflection, but is more stably done by checking the bytecode of methods by using a bytecode injection/modification/analysis library such as ASM or BCEL. Although BCEL hasn't been updated since 2006, it is my personal opinion that it is easier to learn how to use; however, ASM is far faster and is up-to-date.



回答2:

You can convert that .class file to java file i.e. decompile it using JAD and then know the source of the java file.

You can take of this resource and also find many more on google.



回答3:

If you have the .class file, you can write your own ClassLoader, and use defineClass to create a Class instance. From there, you can get the name, and any constructors. If it has a null constructor, you simply do newInstance on the Class instance, otherwise you'll need to hunt down a constructor and pass in the appropriate arguments to the constructors newInstance method.



回答4:

Just go with this tutorial u'll get all your answer..

Using Java Reflection

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.