[英]Method Reflect - Call sequence of methods
我正在尝试学习方法反射,以便可以在Java应用程序中应用。
我创建了两个POJO类。
愿望.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
public class Day {
private Wishes wishes;
public Wishes getWishes() {
return wishes;
}
public void setWishes(Wishes wishes) {
this.wishes = wishes;
}
}
这就是我在主要方法中所做的。 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();
}
}
}
我运行该应用程序。 对于第一个,我直接获得了准确的输出。 但是第二次我要例外了。 这是控制台输出和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)
如何将Day
类与Method反射一起使用, getWishes
从getWishes
调用getGreeting
方法? 可能吗? 否则,使用方法反映的最佳方法是什么?
在我的应用程序中,我得到的方法名称来自一个XML文件。 因此,它可能像上面一样包含单个方法或方法调用序列。
首先,在一天课中,您应该发起愿望
private Wishes wishes = new Wishes();
第二,您需要这样做:
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);
方法Class#getDeclaredMethod
采用方法的名称及其参数的类型。 您正在将字符串getWishes().getGreeting
传递给无效的方法名称。 你想用
Method method = clazz.getDeclaredMethod("getWishes");
为了从您的Day
实例中获取Wishes
实例,应该怎么做。 对于收到的实例,您可以然后反射地调用getGreeting
方法。 您建议的方法链接不适用于反射。 但是,有些库简化了反射API,例如用于对链式属性进行Bean访问。 但是,出于学习目的,您需要手动链接反射调用。
反射呼叫不堆叠。 因此,您调用方法getGreeting
的方式不起作用。
您可以这样尝试:
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());
注意:可以根据您的要求进一步重构此代码段
在Day类上没有这样的方法“ getWishes()。getGreeting”。 您要做的是。
在序列上,您必须一个接一个地执行。
顺便说一句,我认为值得一看的是JXPath库作为替代方案。 您可以给一个复杂的对象并进行xpath搜索。
反射调用不会堆叠-Day类中没有名称为“ getWishes()。getGreeting()”的方法。
您需要先调用“ Day.getWishes()”,然后在返回的对象上调用“ getGreeting()”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.