[英]How to call method of method at runtime?
I know using reflection we can call method at run time.我知道使用反射我们可以在运行时调用方法。 I have one requirement obj.getMethod1().getMethod2().getMethod3()
to be called at run time.我有一个要求obj.getMethod1().getMethod2().getMethod3()
在运行时被调用。 Method Name will be know only during run time.方法名称只会在运行时知道。 Number of methods also differ during run time.运行时方法的数量也不同。 sometimes it can be obj.getMethod1().getMethod2()
.有时它可以是obj.getMethod1().getMethod2()
。
Currently I am handling through array as below目前我正在通过数组处理如下
obj=initialObj;
for(i=0;i<arrayValue.length;i++){
tempobj=obj.getClass.getMethod(arrayValue[i])// arrayValue contains method name
obj=tempobj;
}
Is there any other better way of doing this?有没有其他更好的方法来做到这一点?
Assuming your methods don't have parameters, you can do this:假设您的方法没有参数,您可以这样做:
public static Object callChainedMethods(Object obj, String[] methodNames) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> type = obj.getClass();
Object objectOnWhichToCallMethod = obj;
for (String methodName : methodNames) {
Method m = type.getMethod(methodName);
objectOnWhichToCallMethod = m.invoke(objectOnWhichToCallMethod);
type = objectOnWhichToCallMethod.getClass();
}
return objectOnWhichToCallMethod;
}
If you don't need to return the final return value:如果不需要返回最终的返回值:
public static void callChainedMethods(Object obj, String[] methodNames) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> type = obj.getClass();
Object objectOnWhichToCallMethod = obj;
for (String methodName : methodNames) {
Method m = type.getMethod(methodName);
objectOnWhichToCallMethod = m.invoke(objectOnWhichToCallMethod);
type = objectOnWhichToCallMethod.getClass();
}
}
For example:例如:
String[] methods = {"toString", "getClass", "getClass"};
System.out.println(callChainedMethods((Integer)10, methods));
// prints "class java.lang.Class"
// because it is calling ((Integer)10).toString().getClass().getClass()
This is util class, that contains common methods:这是 util 类,包含常用方法:
public final class ReflectionUtils {
public static <T> T invokeMethod(Object obj, String name, Class<?>[] types, Object... values) throws Throwable {
try {
Method method = getMethod(obj.getClass(), name, types);
method.setAccessible(true);
return (T)method.invoke(obj, values);
} catch(InvocationTargetException e) {
throw e.getTargetException();
}
}
private static Method getMethod(Class<?> cls, String name, Class<?>... types) throws NoSuchMethodException {
Method method = null;
while (method == null && cls != null) {
try {
method = cls.getDeclaredMethod(name, types);
} catch(NoSuchMethodException ignored) {
cls = cls.getSuperclass();
}
}
if (method == null) {
throw new NoSuchMethodException();
}
return method;
}
}
This is how to call obj.getMethod1().getMethod2().getMethod3()
:这是调用obj.getMethod1().getMethod2().getMethod3()
:
public static Object invokeMethods(Object obj, String... methodNames) throws Throwable {
for (String methodName : methodNames)
obj = ReflectionUtils.invokeMethod(obj, methodName, null);
return obj;
}
And client code could look like this:客户端代码可能如下所示:
Object res = invokeMethods(obj, "getMethod1", "getMethod2", "getMethod2");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.