[英]Method Reflect - Call sequence of methods
I am trying to learn Method Reflect so I can apply in my Java application. 我正在尝试学习方法反射,以便可以在Java应用程序中应用。
I created two POJO classes. 我创建了两个POJO类。
Wishes.java 愿望.java
public class Wishes {
private String greeting;
public String getGreeting() {
this.greeting="Good Afternoon!";
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
Day.java Day.java
public class Day {
private Wishes wishes;
public Wishes getWishes() {
return wishes;
}
public void setWishes(Wishes wishes) {
this.wishes = wishes;
}
}
This is what I do in my main method. 这就是我在主要方法中所做的。 DemoApp.java DemoApp.java
public class DemoApp {
public static void main(String[] args) {
try {
Class cls=Wishes.class;
Method method1=cls.getDeclaredMethod("getGreeting");
String result1=(String) method1.invoke(cls.newInstance());
System.out.println(result1);
Class clazz=Day.class;
Method method=clazz.getDeclaredMethod("getWishes().getGreeting");
String result=(String) method.invoke(clazz.newInstance());
System.out.println(result);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
I run the application. 我运行该应用程序。 For the first one I am getting exact output as it's straight forward. 对于第一个,我直接获得了准确的输出。 But for the second I am getting exception. 但是第二次我要例外了。 Here is the console output and stacktrace. 这是控制台输出和stacktrace。
Good Afternoon!
java.lang.NoSuchMethodException: com.myapp.demo.Day.getWishes().getGreeting()
at java.lang.Class.getDeclaredMethod(Class.java:2004)
at com.myapp.demo.DemoApp.main(DemoApp.java:17)
How to call the getGreeting
method from getWishes
from using Day
class with Method reflect? 如何将Day
类与Method反射一起使用, getWishes
从getWishes
调用getGreeting
方法? Is it possible? 可能吗? Otherwise what is the best way to do that with method reflect? 否则,使用方法反映的最佳方法是什么?
In my application, the method name I am getting is from one XML file. 在我的应用程序中,我得到的方法名称来自一个XML文件。 So it may contain single method or sequence of method calls like the above. 因此,它可能像上面一样包含单个方法或方法调用序列。
first of all in Day class you should initiate wishes 首先,在一天课中,您应该发起愿望
private Wishes wishes = new Wishes();
second you need to this: 第二,您需要这样做:
Method method=clazz.getDeclaredMethod("getWishes");
Object result= method.invoke(clazz.newInstance());
Method method2=result.getClass().getDeclaredMethod("getGreeting");
String result2=(String) method2.invoke(cls.newInstance());
System.out.println(result2);
The method Class#getDeclaredMethod
takes the name of a method and the types of its parameters. 方法Class#getDeclaredMethod
采用方法的名称及其参数的类型。 You are handing the string getWishes().getGreeting
what is not a valid method name. 您正在将字符串getWishes().getGreeting
传递给无效的方法名称。 You want to use 你想用
Method method = clazz.getDeclaredMethod("getWishes");
what should work in order to get the instance of Wishes
from your Day
instance. 为了从您的Day
实例中获取Wishes
实例,应该怎么做。 For the received instance, you can then call the getGreeting
method reflectively. 对于收到的实例,您可以然后反射地调用getGreeting
方法。 Method chaining as you suggest it does not work with reflection. 您建议的方法链接不适用于反射。 There are however libraries easing the reflection API as for example for bean access of chained properties. 但是,有些库简化了反射API,例如用于对链式属性进行Bean访问。 For your learning purposes, you however need to chain the reflective calls manually. 但是,出于学习目的,您需要手动链接反射调用。
Reflective calls are not stacked. 反射呼叫不堆叠。 So the way you are calling the method getGreeting
doesn't work. 因此,您调用方法getGreeting
的方式不起作用。
You can try this way instead: 您可以这样尝试:
Class cls=Wishes.class;
Method method1=cls.getDeclaredMethod("getGreeting");
String result1=(String) method1.invoke(cls.newInstance());
System.out.println(result1);
Class clazz=Day.class;
Object ob = clazz.newInstance();
Method method2=clazz.getDeclaredMethod("setWishes", cls);
method2.invoke(ob, cls.newInstance());
Method method=clazz.getDeclaredMethod("getWishes");
Object day =(Object) method.invoke(ob);
System.out.println(((Wishes)day).getGreeting());
Note: This snippet can further be refactored to suit your requirements 注意:可以根据您的要求进一步重构此代码段
There is no such method "getWishes().getGreeting" on the Day class. 在Day类上没有这样的方法“ getWishes()。getGreeting”。 what you have to do is. 您要做的是。
On sequences you have to execute one by one. 在序列上,您必须一个接一个地执行。
By the way, I think it is worth having a look at JXPath library as an alternative. 顺便说一句,我认为值得一看的是JXPath库作为替代方案。 you can give a complex object and do a xpath search. 您可以给一个复杂的对象并进行xpath搜索。
Reflection calls don't stack - there is no method with the name "getWishes().getGreeting()" in class Day. 反射调用不会堆叠-Day类中没有名称为“ getWishes()。getGreeting()”的方法。
You need to first call "Day.getWishes()" and then call "getGreeting()" on the returned object. 您需要先调用“ Day.getWishes()”,然后在返回的对象上调用“ getGreeting()”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.