Java 如何在运行时调用方法的方法?

Java 如何在运行时调用方法的方法?,java,Java,我知道使用反射可以在运行时调用方法。我有一个要求obj.getMethod1().getMethod2().getMethod3(),需要在运行时调用。只有在运行时才能知道方法名称。在运行时,方法的数量也会有所不同。有时它可以是obj.getMethod1().getMethod2() 目前我正在通过数组处理,如下所示 obj=initialObj; for(i=0;i<arrayValue.length;i++){ tempobj=obj.getClass.getMetho

我知道使用反射可以在运行时调用方法。我有一个要求
obj.getMethod1().getMethod2().getMethod3()
,需要在运行时调用。只有在运行时才能知道方法名称。在运行时,方法的数量也会有所不同。有时它可以是obj.getMethod1().getMethod2()

目前我正在通过数组处理,如下所示

 obj=initialObj;
 for(i=0;i<arrayValue.length;i++){
     tempobj=obj.getClass.getMethod(arrayValue[i])// arrayValue contains method name
     obj=tempobj;
 }
obj=initialObj;

对于(i=0;i假设您的方法没有参数,可以执行以下操作:

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;
}

这是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;
    }
}
客户端代码可能如下所示:

Object res = invokeMethods(obj, "getMethod1", "getMethod2", "getMethod2");

您的示例代码无法工作,因为您没有调用
invoke(obj)
在该方法上。此外,第三行缺少
。我的实现与上面相同。我不想迭代我关心的方法。迭代会降低性能。还有其他方法吗。@SoundariyaaJakanadhan您是否确实遇到过性能问题,或者您只是在猜测?E即使存在性能问题,反射也可能是原因,而不是循环。你有一个未知大小的方法数组,否则你将如何调用它们?
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;
    }
}
public static Object invokeMethods(Object obj, String... methodNames) throws Throwable {
    for (String methodName : methodNames)
        obj = ReflectionUtils.invokeMethod(obj, methodName, null);
    return obj;
}
Object res = invokeMethods(obj, "getMethod1", "getMethod2", "getMethod2");