使用 Java 反射创建 eval() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2621251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Java reflection to create eval() method
提问by heldopslippers
I have a question about reflection I am trying to have some kind of eval() method. So i can call for example:
我有一个关于反射的问题,我正在尝试使用某种 eval() 方法。所以我可以调用例如:
eval("test('woohoo')");
Now I understand the there is no eval method in java but there is reflection. I made the following code:
现在我明白java中没有eval方法但是有反射。我做了以下代码:
String s = "test";
Class cl = Class.forName("Main");
Method method = cl.getMethod(s, String.class);
method.invoke(null, "woohoo");
This works perfectly (of course there is a try, catch block around this code). It runs the test method. However I want to call multiple methods who all have different parameters.
这完美地工作(当然,围绕此代码有一个 try, catch 块)。它运行测试方法。但是我想调用多个具有不同参数的方法。
I don't know what parameters these are (so not only String.class). But how is this possible? how can I get the parameter types of a method ? I know of the following method:
我不知道这些是什么参数(所以不仅仅是 String.class)。但这怎么可能呢?如何获取方法的参数类型?我知道以下方法:
Class[] parameterTypes = method.getParameterTypes();
But that will return the parameterTypes of the method I just selected! with the following statement:
但这将返回我刚刚选择的方法的参数类型!有以下声明:
Method method = cl.getMethod(s, String.class);
Any help would be appreciated !
任何帮助,将不胜感激 !
回答by cletus
You will need to call Class.getMethods()and iterate through them looking for the correct function.
您将需要调用Class.getMethods()并遍历它们以寻找正确的函数。
For (Method method : clazz.getMethods()) {
if (method.getName().equals("...")) {
...
}
}
The reason for this is that there can be multiple methods with the same name and different parameter types (ie the method name is overloaded).
这样做的原因是可以有多个具有相同名称和不同参数类型的方法(即方法名称被重载)。
getMethods()returns all the public methods in the class, including those from superclasses. An alternative is Class.getDeclaredMethods(), which returns all methods in that class only.
getMethods()返回类中的所有公共方法,包括来自超类的方法。另一种方法是Class.getDeclaredMethods(),它只返回该类中的所有方法。
回答by Bozho
You can loop over all methods of a class using:
您可以使用以下方法遍历类的所有方法:
cls.getMethods(); // gets all public methods (from the whole class hierarchy)
or
或者
cls.getDeclaredMethods(); // get all methods declared by this class
.
.
for (Method method : cls.getMethods()) {
// make your checks and calls here
}
回答by Itay Maman
You can use getMethods()which returns an array of all methods of a class.
Inside the loop you can inspect the parameters of each method.
您可以使用getMethods()which 返回一个类的所有方法的数组。在循环内部,您可以检查每个方法的参数。
for(Method m : cl.getMethods()) {
Class<?>[] params = m.getParameterTypes();
...
}
Otherwise you can use getDelcaredMethods()which will allow you to "see" private methods (but not inherited methods). Note that if you want to invoke a private methods you must first apply setAccessible(boolean flag)on it:
否则,您可以使用getDelcaredMethods()which 将允许您“查看”私有方法(但不是继承的方法)。请注意,如果要调用私有方法,则必须首先对其应用setAccessible(boolean flag):
for(Method m : cl.getDelcaredMethods()) {
m.setAccessible(true);
Class<?>[] params = m.getParameterTypes();
...
}
回答by heldopslippers
Ok thanxs to all the people who answered my question here the final solution:
好的,感谢所有在这里回答我问题的人的最终解决方案:
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args){
String func = "test";
Object arguments[] = {"this is ", "really cool"};
try{
Class cl = Class.forName("Main");
for (Method method : cl.getMethods()){
if(method.getName().equals(func)){
method.invoke(null, arguments);
}
}
} catch (Exception ioe){
System.out.println(ioe);
}
}
public static void test(String s, String b){
System.out.println(s+b);
}
}

