FileDocCategorySizeDatePackage
MethodNameInvoker.javaAPI DocAndroid 5.1 API3583Thu Mar 12 22:22:10 GMT 2015android.hardware.camera2.dispatch

MethodNameInvoker

public class MethodNameInvoker extends Object
Invoke a method on a dispatchable by its name (without knowing the {@code Method} ahead of time).
param
destination dispatch type, methods will be looked up in the class of {@code T}

Fields Summary
private final Dispatchable
mTarget
private final Class
mTargetClass
private final ConcurrentHashMap
mMethods
Constructors Summary
public MethodNameInvoker(Dispatchable target, Class targetClass)
Create a new method name invoker.

param
target destination dispatch type, invokes will be redirected to this dispatcher
param
targetClass destination dispatch class, the invoked methods will be from this class


                                        
         
        mTargetClass = targetClass;
        mTarget = target;
    
Methods Summary
public Kinvoke(java.lang.String methodName, java.lang.Object params)
Invoke a method by its name.

If more than one method exists in {@code targetClass}, the first method with the right number of arguments will be used, and later calls will all use that method.

param
methodName The name of the method, which will be matched 1:1 to the destination method
param
params Variadic parameter list.
return
The same kind of value that would normally be returned by calling {@code methodName} statically.
throws
IllegalArgumentException if {@code methodName} does not exist on the target class
throws
Throwable will rethrow anything that the target method would normally throw

        checkNotNull(methodName, "methodName must not be null");

        Method targetMethod = mMethods.get(methodName);
        if (targetMethod == null) {
            for (Method method : mTargetClass.getMethods()) {
                // TODO future: match types of params if possible
                if (method.getName().equals(methodName) &&
                        (params.length == method.getParameterTypes().length) ) {
                    targetMethod = method;
                    mMethods.put(methodName, targetMethod);
                    break;
                }
            }

            if (targetMethod == null) {
                throw new IllegalArgumentException(
                        "Method " + methodName + " does not exist on class " + mTargetClass);
            }
        }

        try {
            return (K) mTarget.dispatch(targetMethod, params);
        } catch (Throwable e) {
            UncheckedThrow.throwAnyException(e);
            // unreachable
            return null;
        }