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:com.spstudio.common.log.ServiceExceptionLogAspect.java

/**
 * *//w  w  w  . j  av  a2  s .c o m
 * ???? service *
 *
 *
 * @param joinPoint 
 *
 * @return ??
 *
 * @throws Exception *
 */
public static String getServiceMthodDescription(JoinPoint joinPoint) throws Exception {

    String targetName = joinPoint.getTarget().getClass().getName();

    String methodName = joinPoint.getSignature().getName();

    Object[] arguments = joinPoint.getArgs();

    Class targetClass = Class.forName(targetName);

    Method[] methods = targetClass.getMethods();

    String description = ":" + targetName + " ;??:" + methodName
            + " ;???:";

    for (Method method : methods) {

        if (method.getName().equals(methodName)) {

            Class[] clazzs = method.getParameterTypes();

            if (clazzs.length == arguments.length) {

                description += method.getAnnotation(ServiceExceptionLog.class).description();

                break;

            }

        }

    }

    return description;

}

From source file:de.unisb.cs.st.javalanche.mutation.runtime.testDriver.junit.Junit4Util.java

public static Method getSuiteMethod(Class<?> forName) {
    Method[] methods = forName.getMethods();
    for (Method method : methods) {
        if (method.getName().equals("suite") && method.getParameterTypes().length == 0) {
            return method;
        }/*from   w  w w  .jav a 2 s  .  co  m*/
    }
    return null;
}

From source file:Main.java

/**
 * Returns an array of all methods in a class with the specified parameter types.
 *
 * The return type is optional, it will not be compared if it is {@code null}.
 * Use {@code void.class} if you want to search for methods returning nothing.
 *///from w  w  w  . j a  v  a  2 s.  c o m
public static Method[] findMethodsByExactParameters(Class<?> clazz, Class<?> returnType,
        Class<?>... parameterTypes) {
    List<Method> result = new LinkedList<Method>();
    for (Method method : clazz.getDeclaredMethods()) {
        if (returnType != null && returnType != method.getReturnType())
            continue;

        Class<?>[] methodParameterTypes = method.getParameterTypes();
        if (parameterTypes.length != methodParameterTypes.length)
            continue;

        boolean match = true;
        for (int i = 0; i < parameterTypes.length; i++) {
            if (parameterTypes[i] != methodParameterTypes[i]) {
                match = false;
                break;
            }
        }

        if (!match)
            continue;

        method.setAccessible(true);
        result.add(method);
    }
    return result.toArray(new Method[result.size()]);
}

From source file:Main.java

public static <T extends Object, Result> Result callMethodCast(T receiver, Class<Result> resultClass,
        String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException {
    if (receiver == null || methodName == null) {
        return null;
    }/*from  w ww  .j  a  va 2  s  .  c om*/
    Class<?> cls = receiver.getClass();
    Method toInvoke = null;
    do {
        Method[] methods = cls.getDeclaredMethods();
        methodLoop: for (Method method : methods) {
            if (!methodName.equals(method.getName())) {
                continue;
            }
            Class<?>[] paramTypes = method.getParameterTypes();
            if (args == null && paramTypes == null) {
                toInvoke = method;
                break;
            } else if (args == null || paramTypes == null || paramTypes.length != args.length) {
                continue;
            }

            for (int i = 0; i < args.length; ++i) {
                if (!paramTypes[i].isAssignableFrom(args[i].getClass())) {
                    continue methodLoop;
                }
            }
            toInvoke = method;
        }
    } while (toInvoke == null && (cls = cls.getSuperclass()) != null);
    Result t;
    if (toInvoke != null) {
        boolean accessible = toInvoke.isAccessible();
        toInvoke.setAccessible(true);
        if (resultClass != null) {
            t = resultClass.cast(toInvoke.invoke(receiver, args));
        } else {
            toInvoke.invoke(receiver, args);
            t = null;
        }
        toInvoke.setAccessible(accessible);
        return t;
    } else {
        throw new NoSuchMethodException("Method " + methodName + " not found");
    }
}

From source file:com.jkoolcloud.tnt4j.streams.matchers.StringMatcher.java

private static Method findMatchingMethodAndConvertArgs(Object methodName, String[] arguments,
        Object[] convertedArguments, Method[] methods) {
    for (Method method : methods) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arguments.length + 1) {
            boolean methodMatch = true;
            Class<?>[] parameterTypes = method.getParameterTypes();
            for (int i = 1; i < parameterTypes.length; i++) {
                Class<?> parameterType = parameterTypes[i];
                if (CharSequence.class.isAssignableFrom(parameterType)) {
                    convertedArguments[i - 1] = arguments[i - 1];
                } else {
                    try {
                        if (parameterType.isPrimitive()) {
                            parameterType = ClassUtils.primitiveToWrapper(parameterType);
                        }/* w w w.  j av a 2s  . c om*/
                        Method converterMethod = parameterType.getMethod("valueOf", String.class);
                        convertedArguments[i - 1] = converterMethod.invoke(null, arguments[i - 1]);
                    } catch (Exception e) {
                        methodMatch = false;
                        break;
                    }
                }
            }

            if (methodMatch) {
                return method;
            }
        }
    }
    return null;
}

From source file:Main.java

private static Set<Method> findMethodsCompatibleWith(boolean staticMethod, Set<Method> methods,
        String methodName, Class<?>[] argTypes) {
    final Set<Method> list = new HashSet<Method>(methods);
    for (final Iterator<Method> it = list.iterator(); it.hasNext();) {
        final Method method = it.next();
        if (!methodName.equals(method.getName())) {
            it.remove();//from  w  w w  . ja  va  2 s. c o  m
            continue;
        }
        if (argTypes.length != method.getParameterTypes().length) {
            it.remove();
            continue;
        }
        if (Modifier.isAbstract(method.getModifiers())
                || Modifier.isStatic(method.getModifiers()) != staticMethod) {
            it.remove();
            continue;
        }
        if (!isMethodArgCompatible(method, argTypes)) {
            it.remove();
            continue;
        }

    }
    return list;
}

From source file:Main.java

private static boolean isCandidate(Method m, String methodName, List<Object> availableParams,
        List<Object> paramsContainer) {
    if (m.getName().equals(methodName) == false) {
        return false;
    }/*ww w  .  j a v a  2s .c  om*/

    Class<?>[] paramTypes = m.getParameterTypes();
    if (paramTypes.length == 0) {
        return true;
    }

    paramsContainer.addAll(createParamsArray(m, availableParams));

    /*
     * we found match for every type
     */
    return paramsContainer.size() == paramTypes.length;
}

From source file:asia.gkc.vneedu.utils.FilterUtil.java

/**
 * /*from   ww w.  jav  a2s .  c o m*/
 * @param list -  
 * @param object - ?
 * @param <T> - 
 * @return ?
 */
public static <T> T exclude(List<String> list, T object) {
    Class<?> c = object.getClass();

    for (String fieldName : list) {
        Method[] methods = c.getDeclaredMethods();

        for (Method method : methods) {
            if (method.getName().equals(getMethodName(fieldName, "set"))) {
                Class<?> param_class = method.getParameterTypes()[0];
                try {
                    logger.info("Invoking: " + method.getName());
                    method.invoke(object, param_class.cast(null));
                    logger.info("Invoked: " + method.getName());
                } catch (IllegalAccessException e) {
                    logger.warn("IllegalAccessException");
                } catch (InvocationTargetException e) {
                    logger.warn("InvocationTargetException");
                }
            }
        }
    }

    return object;
}

From source file:com.bluecloud.ioc.classloader.ClassHandler.java

/**
 * <h3>Class</h3>/*  w ww.j  av  a 2  s. com*/
 * 
 * @param obj
 *            
 * @param methodName
 *            ??
 * @param args
 *            ?
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void invokeMethod(Object obj, String methodName, Object args)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<? extends Object> clazz = obj.getClass();
    try {
        if (args != null) {
            clazz.getMethod(methodName, args.getClass()).invoke(obj, args);
        }
    } catch (SecurityException e) {
        log.error(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
                method.invoke(obj, args);
            }
        }
    }
}

From source file:com.acuityph.commons.util.ClassUtils.java

/**
 * Checks if the given method matches the JavaBean property setter signature.
 *
 * @param method/*ww w .  j a  va 2 s.c  o m*/
 *        the method to check
 * @return {@code true}, if the method matchers the JavaBean property setter signature
 */
public static boolean isPropertySetter(final Method method) {
    Assert.notNull(method, "method is null!");
    final int mod = method.getModifiers();
    final boolean expectsOneParameter = method.getParameterTypes().length == 1;
    final boolean returnsVoid = void.class.equals(method.getReturnType());
    return !isPrivate(mod) && !isStatic(mod) && expectsOneParameter && returnsVoid
            && isPropertySetterName(method.getName());
}