Java Method Call invokeGenericMethodOneArg(final Object obj, final String methodName, final Object arg)

Here you can find the source of invokeGenericMethodOneArg(final Object obj, final String methodName, final Object arg)

Description

Dirty method to call a declared method with a generic parameter type.

License

Open Source License

Parameter

Parameter Description
obj a parameter
methodName a parameter
arg Argument or invoke the method with.

Return

null in case of errors (can not be distinguished).

Declaration

public static Object invokeGenericMethodOneArg(final Object obj, final String methodName, final Object arg) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    /**//w w w . ja  v a  2  s .  c om
     * Dirty method to call a declared method with a generic parameter type. Does try+catch for method invocation and should not throw anything for the normal case. Purpose for this is generic factory registration, having methods with type Object alongside methods with more specialized types.
     * @param obj
     * @param methodName
     * @param arg Argument or invoke the method with.
     * @return null in case of errors (can not be distinguished).
     */
    public static Object invokeGenericMethodOneArg(final Object obj, final String methodName, final Object arg) {
        // TODO: Isn't there a one-line-call for this ??
        final Class<?> objClass = obj.getClass();
        final Class<?> argClass = arg.getClass();
        // Collect methods that might work.
        Method methodFound = null;
        boolean denyObject = false;
        for (final Method method : objClass.getDeclaredMethods()) {
            if (method.getName().equals(methodName)) {
                final Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1) {
                    // Prevent using Object as argument if there exists a method with a specialized argument.
                    if (parameterTypes[0] != Object.class && !parameterTypes[0].isAssignableFrom(argClass)) {
                        denyObject = true;
                    }
                    // Override the found method if none found yet and assignment is possible, or if it has a specialized argument of an already found one.
                    if ((methodFound == null && parameterTypes[0].isAssignableFrom(argClass) || methodFound != null
                            && methodFound.getParameterTypes()[0].isAssignableFrom(parameterTypes[0]))) {
                        methodFound = method;
                    }
                }
            }
        }
        if (denyObject && methodFound.getParameterTypes()[0] == Object.class) {
            // TODO: Throw something !?
            return null;
        } else if (methodFound != null && methodFound.getParameterTypes()[0].isAssignableFrom(argClass)) {
            try {
                final Object res = methodFound.invoke(obj, arg);
                return res;
            } catch (Throwable t) {
                // TODO: Throw something !?
                return null;
            }
        } else {
            // TODO: Throw something !?
            return null;
        }
    }
}

Related

  1. invokedMethod(java.lang.Object closure, java.lang.Class targetClass, java.lang.String targetMethod)
  2. invokeExactField(Object object, String fieldName)
  3. invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
  4. invokeFunction(Object iFunction, String funcName, String param)
  5. invokeFunctions(Object iFunction, String funcName)
  6. invokeGet(Object o, String fieldName)
  7. invokeGetClasspathMethodIfItExists(ClassLoader cl)
  8. invokeGetMethodWithSameName(Object object, Method method)
  9. invokeHeritedMethod(Object target, String method, Class klass)