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:Main.java

private static void setOnClickListener(final Object object, final View view, final Method method) {
    if (null == view)
        return;// w  w w  .  j  a  v  a  2 s. c  o  m
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Class<?>[] classes = method.getParameterTypes();
                if (null == classes || 0 == classes.length) {
                    method.invoke(object);
                } else {
                    method.invoke(object, view);
                }
            } catch (Exception e) {
                throw new RuntimeException(
                        "setOnClickListener fail,method is " + method.getName() + " \n" + e.getMessage());
            }
        }
    });
}

From source file:io.openmessaging.rocketmq.utils.BeanUtils.java

public static Class<?> getMethodClass(Class<?> clazz, String methodName) {
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (method.getName().equalsIgnoreCase(methodName)) {
            return method.getParameterTypes()[0];
        }//from w w  w  .j a v a2s  .  c  om
    }
    return null;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static LinkedHashSet<Method> getDeclaredMethods(Class<?> theClazz) {
    LinkedHashSet<Method> retVal = new LinkedHashSet<Method>();
    for (Method next : theClazz.getDeclaredMethods()) {
        try {/* w  w w . j av  a2 s .  c  o  m*/
            Method method = theClazz.getMethod(next.getName(), next.getParameterTypes());
            retVal.add(method);
        } catch (NoSuchMethodException e) {
            retVal.add(next);
        } catch (SecurityException e) {
            retVal.add(next);
        }
    }
    return retVal;
}

From source file:com.jaspersoft.jasperserver.util.QueryUtil.java

/**
 * Execute DataBaseMetaData methods using reflection
 * @param dmd//from   w  w w.j a  v a2s. co m
 * @param methodName
 * @param parameters
 * @return
 * @throws ClassNotFoundException
 */
public static Method findMethod(DatabaseMetaData dmd, String methodName, Object[] parameters)
        throws ClassNotFoundException {
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter findMethod .. Start Time" + System.currentTimeMillis());
        }
        Class cl = Class.forName("java.sql.DatabaseMetaData");
        Method[] methods = cl.getDeclaredMethods();
        // Trying to avoid collision of methods with varying parameters and avoid having to do parameter class types

        int paramCount = 0;
        if (null != parameters) {
            paramCount = parameters.length;
        }

        for (Method m : methods) {
            if (m.getName().equals(methodName)) {
                if (Modifier.isPublic(m.getModifiers()) && m.getParameterTypes().length == paramCount) {
                    return m;
                }
            }
        } //for
        return null;
    } finally {
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit findMethod .. Total Time Spent: " + elapsedTime);
        }
    }

}

From source file:Main.java

/**
 * get the method start with 'get' or 'is'.
 *///ww  w .  j  ava 2s.  c om
public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<>();
        for (Method method : bean.getClass().getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
                    && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) {
                String name = method.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method);
                } else if (name.length() > 2 && name.startsWith("is")) {
                    cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method);
                }
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:Main.java

/**
 * Checks is the given method from java.lang.Object
 * @param method - method to check/*from   w  w w . ja  va 2s  .co m*/
 * @return true if method from java.lang.Object
 */
public static boolean isJavaLangObjectMethod(Method method) {
    Method methods[] = Object.class.getDeclaredMethods();
    for (Method objMethod : methods) {
        if (objMethod.getName().equals(method.getName())) {
            Class<?> methodParameterTypes[] = method.getParameterTypes();
            Class<?> objectMethodParameterTypes[] = objMethod.getParameterTypes();
            if (objectMethodParameterTypes.length == methodParameterTypes.length) {
                boolean matched = true;
                for (int i = 0; i < methodParameterTypes.length; i++) {
                    Class<?> class1 = methodParameterTypes[i];
                    Class<?> class2 = objectMethodParameterTypes[i];
                    if (!class1.equals(class2)) {
                        matched = false;
                        break;
                    }
                }
                if (matched) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static Method getMethod(Object o, String methodName, String returnType, Class<?>... parameters) {
    for (Method method : o.getClass().getDeclaredMethods()) {
        if (!method.getName().equals(methodName) || !method.getReturnType().getName().equals(returnType))
            continue;

        Class<?>[] pars = method.getParameterTypes();
        if (parameters.length != pars.length)
            continue;
        boolean found = true;
        for (int i = 0; i < parameters.length; i++) {
            if (pars[i] != parameters[i]) {
                found = false;//from  w w  w. ja  v a  2s .c o  m
                break;
            }
        }

        if (found) {
            return method;
        }
    }

    return null;
}

From source file:Main.java

public static Method getMethod(Object o, String methodName, String returnType, String... parameters) {
    for (Method method : o.getClass().getDeclaredMethods()) {
        if (!method.getName().equals(methodName) || !method.getReturnType().getName().equals(returnType))
            continue;

        Class<?>[] pars = method.getParameterTypes();
        if (parameters.length != pars.length)
            continue;
        boolean found = true;
        for (int i = 0; i < parameters.length; i++) {
            if (!pars[i].getName().equals(parameters[i])) {
                found = false;/*w  w w  . java 2 s .c  o  m*/
                break;
            }
        }

        if (found) {
            return method;
        }
    }

    return null;
}

From source file:Main.java

/**
 * find all getter and is method and return the value by map.
 *///from  w w  w  .  j  a  va2 s.  c o  m
public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<>();
    for (Method method : bean.getClass().getMethods()) {
        String name = method.getName();
        if (((name.length() > 3 && name.startsWith("get")) || (name.length() > 2 && name.startsWith("is")))
                && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0
                && method.getDeclaringClass() != Object.class) {
            int i = name.startsWith("get") ? 3 : 2;
            String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
            try {
                map.put(key, method.invoke(bean, new Object[0]));
            } catch (Exception e) {
            }
        }
    }
    return map;
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Method findMethod(Object object, String methodName, int arity) {
    for (Method method : object.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arity
                && !method.isVarArgs()) {
            return method;
        }/*from ww  w.j a va 2s. co  m*/
    }
    return null;
}