Example usage for java.lang.reflect Method getParameterTypes

List of usage examples for java.lang.reflect Method getParameterTypes

Introduction

In this page you can find the example usage for java.lang.reflect Method getParameterTypes.

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:org.carewebframework.shell.property.PropertyUtil.java

/**
 * Returns the requested method from an object instance.
 * //w ww.  j  a  v a  2  s  .  c  o  m
 * @param methodName Name of the setter method.
 * @param instance Object instance to search.
 * @param valueClass The desired property return type (null if don't care).
 * @param setter If true, search for setter method signature. If false, getter method signature.
 * @return The requested method.
 * @throws NoSuchMethodException If method was not found.
 */
private static Method findMethod(String methodName, Object instance, Class<?> valueClass, boolean setter)
        throws NoSuchMethodException {
    if (methodName == null) {
        return null;
    }

    int paramCount = setter ? 1 : 0;

    for (Method method : instance.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == paramCount) {
            Class<?> targetClass = setter ? method.getParameterTypes()[0] : method.getReturnType();

            if (valueClass == null || TypeUtils.isAssignable(targetClass, valueClass)) {
                return method;
            }
        }
    }

    throw new NoSuchMethodException("Compatible method not found: " + methodName);

}

From source file:Main.java

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied name
 * and parameter types. Searches all superclasses up to <code>Object</code>.
 * <p>Returns <code>null</code> if no {@link Method} can be found.
 *
 * @param clazz      the class to introspect
 * @param name       the name of the method
 * @param paramTypes the parameter types of the method
 *                   (may be <code>null</code> to indicate any signature)
 * @return the Method object, or <code>null</code> if none found
 *///from w ww. j ava2 s .com
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName())
                    && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:ReflectUtils.java

/**
 * Convert the Method to a Java Code String
 * (arguments are replaced by the simple types)
 * @param method Method//  www.j  ava 2s  . c  om
 * @return
 */
public static String getJavaCallString(Method method) {
    StringBuilder call = new StringBuilder();
    call.append(method.getName());
    addParamsString(call, method.getParameterTypes());
    return call.toString();
}

From source file:Main.java

public static void callMethod(Object obj, String method, Object... params) {
    Method[] ms = obj.getClass().getMethods();
    for (Method m : ms) {
        if (!m.getName().equals(method))
            continue;
        // TODO check type args
        Class<?>[] types = m.getParameterTypes();
        if (params.length != types.length)
            continue;
        for (int i = 0; i < params.length; i++) {

        }/*from   w w w  . ja v  a2  s .  c  om*/
        // call
        try {
            m.invoke(obj, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    throw new RuntimeException(obj + "." + method + "(" + params.length + ")");
}

From source file:Main.java

static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Method finalMethod = null;/*from   w  w  w .j  av a  2s .  c  o  m*/
    while (objectClass != null && finalMethod == null) {
        for (Method method : objectClass.getDeclaredMethods()) {
            if (method.getName().equals(fieldName)) {
                Class<?>[] paramsType = method.getParameterTypes();
                if (paramsType.length == 0) {
                    finalMethod = method;
                    break;
                } else if (paramsType.length == 1) {
                    if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                        finalMethod = method;
                        break;
                    }
                }

            }
        }
        if (finalMethod == null) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (finalMethod == null) {
        throw new NoSuchFieldException(fieldName);
    }
    return finalMethod;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java

public static boolean isGetterMethod(Method method) {
    return ArrayUtils.isEmpty(method.getParameterTypes()) && isGetterMethod(method.getName());
}

From source file:ShowClass.java

/**
 * Print the modifiers, return type, name, parameter types and exception type
 * of a method or constructor. Note the use of the Member interface to allow
 * this method to work with both Method and Constructor objects
 */// ww w  .ja  va 2  s .c  om
public static void print_method_or_constructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(exceptions[i]));
    }
    System.out.println(";");
}

From source file:jp.go.nict.langrid.testresource.loader.NodeLoader_1_2.java

protected static void addProperties(Class<?> clazz, Set<String> properties, Set<String> tupleProperties) {
    for (Method m : clazz.getDeclaredMethods()) {
        if (m.getParameterTypes().length != 1)
            continue;
        String name = m.getName();
        if (!name.startsWith("set"))
            continue;
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String propName = name.substring(3, 4).toLowerCase() + name.substring(4);
        if (m.getParameterTypes()[0].isArray()) {
            tupleProperties.add(propName);
        } else {/*from  w  w  w .ja va  2s. c  o m*/
            properties.add(propName);
        }
    }
}

From source file:Main.java

/**
 * Given a method, which may come from an interface, and a target class used in the current reflective invocation, find the corresponding target method if there is one. E.g. the method may be <code>IFoo.bar()</code> and the target class may be <code>DefaultFoo</code>. In this case, the method may be <code>DefaultFoo.bar()</code>. This enables attributes on that method to be found.
 * <p>/*w  w  w  .  java  2s. co m*/
 * <b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod}, this method does <i>not</i> resolve Java 5 bridge methods automatically. Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod} if bridge method resolution is desirable (e.g. for obtaining metadata from the original method definition).
 * 
 * @param method
 *          the method to be invoked, which may come from an interface
 * @param targetClass
 *          the target class for the current invocation. May be <code>null</code> or may not even implement the method.
 * @return the specific target method, or the original method if the <code>targetClass</code> doesn't implement it or is <code>null</code>
 * @see org.springframework.aop.support.AopUtils#getMostSpecificMethod
 */
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && targetClass != null) {
        try {
            method = targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException ex) {
            // Perhaps the target class doesn't implement this method:
            // that's fine, just use the original method.
        }
    }
    return method;
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Validates that the method annotated with the specified annotation has a single
 * argument of the expected type.//from  ww w  .ja  v  a2  s .  c o  m
 */
public static void validateArgument(Method method, Class<? extends Annotation> annotationType,
        Class<?> expectedParameterType) {
    if (method.getParameterTypes().length != 1
            || !method.getParameterTypes()[0].equals(expectedParameterType)) {
        throw new BeanCreationException(
                String.format("Method %s with @%s MUST take a single argument of type %s", method,
                        annotationType.getName(), expectedParameterType.getName()));
    }
}