Example usage for org.apache.commons.lang.reflect MethodUtils getMatchingAccessibleMethod

List of usage examples for org.apache.commons.lang.reflect MethodUtils getMatchingAccessibleMethod

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect MethodUtils getMatchingAccessibleMethod.

Prototype

public static Method getMatchingAccessibleMethod(Class cls, String methodName, Class[] parameterTypes) 

Source Link

Document

Find an accessible method that matches the given name and has compatible parameters.

Usage

From source file:org.apache.hadoop.hbase.client.coprocessor.Batch.java

/**
 * Creates a new {@link Batch.Call} instance that invokes a method
 * with the given parameters and returns the result.
 *
 * <p>/*from   ww w  .j  av a2  s . c  om*/
 * Note that currently the method is naively looked up using the method name
 * and class types of the passed arguments, which means that
 * <em>none of the arguments can be <code>null</code></em>.
 * For more flexibility, see
 * {@link Batch#forMethod(java.lang.reflect.Method, Object...)}.
 * </p>
 *
 * @param protocol the protocol class being called
 * @param method the method name
 * @param args zero or more arguments to be passed to the method
 * (individual args cannot be <code>null</code>!)
 * @param <T> the class type of the protocol implementation being invoked
 * @param <R> the return type for the method call
 * @return a {@code Callable} instance that will invoke the given method
 * and return the results
 * @throws NoSuchMethodException if the method named, with the given argument
 *     types, cannot be found in the protocol class
 * @see Batch#forMethod(java.lang.reflect.Method, Object...)
 * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call, org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)
 */
public static <T extends CoprocessorProtocol, R> Call<T, R> forMethod(final Class<T> protocol,
        final String method, final Object... args) throws NoSuchMethodException {
    Class[] types = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
        if (args[i] == null) {
            throw new NullPointerException("Method argument cannot be null");
        }
        types[i] = args[i].getClass();
    }

    Method m = MethodUtils.getMatchingAccessibleMethod(protocol, method, types);
    if (m == null) {
        throw new NoSuchMethodException("No matching method found for '" + method + "'");
    }

    m.setAccessible(true);
    return forMethod(m, args);
}

From source file:org.gradle.internal.reflect.JavaPropertyReflectionUtil.java

/**
 * Locates the property with the given name as a writable property. Searches only public properties.
 *
 * Returns null if no such property exists.
 *//*from ww w . j a v a  2s . c o  m*/
public static PropertyMutator writeablePropertyIfExists(Class<?> target, String property,
        @Nullable Class<?> valueType) throws NoSuchPropertyException {
    String setterName = toMethodName("set", property);
    Method method = MethodUtils.getMatchingAccessibleMethod(target, setterName, new Class<?>[] { valueType });
    if (method != null) {
        return new MethodBackedPropertyMutator(property, method);
    }
    return null;
}

From source file:org.openengsb.core.services.internal.RequestHandlerImpl.java

private Method findMethod(Object service, String methodName, Class<?>[] argTypes) {
    Method method;/*  w  ww .  j av  a 2s  .c o m*/

    Class<?> serviceClass = retrieveRealServiceClass(service);
    if (serviceClass.isInstance(CustomMarshallerRealTypeAccess.class)) {
        serviceClass = ((CustomMarshallerRealTypeAccess) service).getRealUnproxiedType();
    }
    method = MethodUtils.getMatchingAccessibleMethod(serviceClass, methodName, argTypes);
    if (method == null) {
        throw new IllegalArgumentException(String.format("could not find method matching arguments \"%s(%s)\"",
                methodName, ArrayUtils.toString(argTypes)));
    }

    return method;
}

From source file:org.openengsb.core.workflow.drools.MethodFinderTest.java

@Test
public void testFindMethodWithSubclass_shouldReturnSameMethod() throws Exception {
    Method matchingAccessibleMethod = MethodUtils.getMatchingAccessibleMethod(WorkflowService.class,
            "processEvent", new Class[] { TestEvent.class });
    assertThat(matchingAccessibleMethod, is(WorkflowService.class.getMethod("processEvent", Event.class)));
}

From source file:org.sglj.service.rmi.client.AbstractNotificationHandler.java

@Override
public void handleNotification(T service, String methodName, Object... args) {
    Class<?>[] paramTypes = new Class<?>[args == null ? 0 : args.length];
    for (int i = 0; i < paramTypes.length; ++i)
        paramTypes[i] = args[i].getClass();

    try {//from  w w  w .j  av  a2 s  .  c o m
        Method m = MethodUtils.getMatchingAccessibleMethod(service.getClass(), methodName, paramTypes);
        if (!canExecute(service, m)) {
            throw new ServiceReflectionCallException("Missing method \"" + m
                    + "\" in the notification service: " + service.getClass().getName());
        }
        m.invoke(service, args);
    } catch (InvocationTargetException e) {
        // if notification fails, let the application crash
        throw new NotificationException(e);
    } catch (Exception e) {
        throw new ServiceReflectionCallException(e);
    }
}

From source file:org.sglj.service.rmi.server.NotificationServiceStub.java

protected void callNotificationMethod(T[] recipients, String methodName, Object... args)
        throws ServiceReflectionCallException {

    Class<?>[] paramTypes = new Class<?>[args == null ? 0 : args.length];
    if (args != null) {
        for (int i = 0; i < paramTypes.length; ++i) {
            paramTypes[i] = args[i].getClass();
        }/*from   ww w  .j a  va2  s.co m*/
    }

    Method method;
    try {
        method = MethodUtils.getMatchingAccessibleMethod(this.getClass(), methodName, paramTypes);
    } catch (SecurityException e) {
        e.printStackTrace();
        throw new ServiceReflectionCallException(e);
    }
    if (!this.remoteMethods.contains(method)) {
        throw new ServiceReflectionCallException("No such method");
    }

    this.sender.sendNotification(recipients, new RemoteCallRequest(getId(), methodName, args));
}