在JAVA中使用反射实例化对象并通过.class文件调用方法? [英] instantiate an object and invoking a method via a .class file using reflection in JAVA?

查看:80
本文介绍了在JAVA中使用反射实例化对象并通过.class文件调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.class文件,而对.class文件一无所知,包括类名,字段或方法.我需要做的是找出有关该类的所有内容并实例化该类的对象.

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.

请记住,我们对.class文件一无所知,在调用getDeclareMethod(),getDeclareFields()和getDeclareConstructor()之后,我知道它包含一个String str文件和一个带有一个String参数的void方法,该方法试图为变量str和纯构造函数分配一个名称,不带任何参数.

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.

所以问题是如何实例化一个对象并调用该方法将一个String分配给str?当类或方法的声明位于同一文件中时,我知道该怎么做,但这是一个未知的.class文件,我请大家帮忙.

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.

推荐答案

首先,您需要将.class文件转换为 Class 对象.您可以使用 URLClassLoader 来做到这一点.

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

假设您有一个 File classFile 和一个 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();
}

现在您已经将类文件存储在 Class 对象中,但是如何创建该类型的对象并从中调用方法呢? java.lang.reflect Java反射>包,在这种情况下很有用.

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.

假设您将 Class<?> 对象存储在名为 clazz 的变量,方法名称的 String methodName 变量中,以及方法参数的 String toSet .

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

请注意,如果方法名称未知,则必须创建一种识别方法,可以通过反射来实现,但是通过使用字节码注入/修改/分析库,例如 ASM BCEL .尽管BCEL自2006年以来就没有进行过更新,但是我个人认为更容易学习使用.但是,ASM更快并且是最新的.

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.

这篇关于在JAVA中使用反射实例化对象并通过.class文件调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆