Java链式反射
我有这个链式方法调用 parseInt(A.get().getC().getD().toString()) 我需要用反思来做这件事。我知道我可以使用Class.forName(String类)然后调用方法,但我如何保存方法结果以便调用该链呢 课程:Java链式反射,java,reflection,Java,Reflection,我有这个链式方法调用 parseInt(A.get().getC().getD().toString()) 我需要用反思来做这件事。我知道我可以使用Class.forName(String类)然后调用方法,但我如何保存方法结果以便调用该链呢 课程: public class A { public static B get() { return new B(); } } public class B { public C getC() { return new C();}
public class A
{
public static B get() { return new B(); }
}
public class B
{
public C getC() { return new C();}
}
public class C
{
public C getD() { return new D();}
}
假设我们有这样的类:
public class A {
public B getB() { return new B(); }
public static B getBStatic() { return new B(); }
}
public class B { public C getC() { return new C();}}
public class C { public String getD() { return "done"}}
例1:
public static void main(String[] args) throws Exception {
Class<A> clazz = A.class;
Constructor<A> constructor = clazz.getConstructor();
A instance = constructor.newInstance();
Method getMethod = clazz.getDeclaredMethod("getB");
Object b = getMethod.invoke(instance);
Method getCMethod = b.getClass().getDeclaredMethod("getC");
Object c = getCMethod.invoke(b);
Method getDMethod = c.getClass().getDeclaredMethod("getD");
String d = (String) getDMethod.invoke(c);
System.out.println(d); // done
}
publicstaticvoidmain(字符串[]args)引发异常{
clazz类=A类;
Constructor=clazz.getConstructor();
实例=构造函数。newInstance();
方法getMethod=clazz.getDeclaredMethod(“getB”);
对象b=getMethod.invoke(实例);
方法getCMethod=b.getClass().getDeclaredMethod(“getC”);
对象c=getCMethod.invoke(b);
方法getDMethod=c.getClass().getDeclaredMethod(“getD”);
String d=(String)getDMethod.invoke(c);
System.out.println(d);//完成
}
例2:
public static void main(String[] args) throws Exception {
reflection(new A(), "getB", "getC", "getD"); // invoke non static methods
reflection(new A(), "getBStatic", "getC", "getD"); // invoke static and nonstatic methods
reflection(A.getBStatic(), "getC", "getD"); // provide object from static method
reflection(invokeStaticMethod(A.class, "getBStatic"), "getC", "getD"); // invoke static method without instance
}
public static Object invokeStaticMethod(Class<?> clazz, String methodName) throws Exception {
return clazz.getMethod(methodName).invoke(clazz);
}
public static void reflection(Object instance, String... methods) throws Exception {
Object item = instance;
for (String methodName : methods) {
item = item.getClass().getDeclaredMethod(methodName).invoke(item);
}
System.out.println(item); // done
}
publicstaticvoidmain(字符串[]args)引发异常{
反射(新的A(),“getB”,“getC”,“getD”);//调用非静态方法
反射(新的A(),“getBStatic”,“getC”,“getD”);//调用静态和非静态方法
反射(A.getBStatic(),“getC”,“getD”);//从静态方法提供对象
反射(invokeStaticMethod(A.class,“getBStatic”),“getC”,“getD”);//不使用实例调用静态方法
}
公共静态对象invokeStaticMethod(类clazz,字符串methodName)引发异常{
返回clazz.getMethod(methodName).invoke(clazz);
}
公共静态void反射(对象实例、字符串…方法)引发异常{
对象项=实例;
for(字符串方法名:方法){
item=item.getClass().getDeclaredMethod(methodName).invoke(item);
}
System.out.println(项);//完成
}
更喜欢使用该方法的返回类型来查找下一个方法,因为它实际上可能返回一个子类,而该子类没有具有该名称的“已声明”方法。