Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

In this page you can find the example usage for java.lang Class getMethods.

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:com.adaptris.util.SimpleBeanUtil.java

private static Method getSetterMethod(Class c, String methodName) {
    Method result = null;//from www. ja v  a 2 s  .c  om
    Method[] methods = c.getMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (name.equalsIgnoreCase(methodName)) {
            Class[] params = m.getParameterTypes();
            if (params.length == 1 && PRIMITIVES.contains(params[0])) {
                result = m;
                break;
            }
        }
    }
    return result;
}

From source file:ReflectionUtils.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//w w w. ja  va 2 s.  c o m
 * @param name the name of the method
 * @param paramTypes the parameter types of the method
 * @return the Method object, or <code>null</code> if none found
 */
public static Method findMethod(Class clazz, String name, Class[] paramTypes) {

    Class searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

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];
        }//  w  ww.  ja  v a  2s. c  om
    }
    return null;
}

From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java

public static Method findSameMethod(Method method, Class classToTest) {
    for (Method m : classToTest.getMethods()) {
        if (m.getName().equals(method.getName())
                && Arrays.equals(m.getParameterTypes(), method.getParameterTypes())) {
            method = m;// www.  j av  a 2  s .  co m
            break;
        }
    }
    return method;
}

From source file:com.gorillalogic.fonemonkey.PropertyUtil.java

private static Method getMethod(Class<?> klass, String name, String name2) throws Exception {
    Method[] methods = klass.getMethods();

    for (int i = 0; i < methods.length; ++i) {
        if (methods[i].getParameterTypes().length == 0) {
            if (methods[i].getName().equals(name) || methods[i].getName().equals(name2))
                return methods[i];
        }/*ww w .  ja  v  a  2 s.  co  m*/
    }

    return null;
}

From source file:com.fengduo.bee.commons.util.ObjectUtils.java

/**
 * ????/*from w  ww . j a v a  2 s. c o  m*/
 * 
 * @param annotation
 * @param object
 */
public static void annotationToObject(Object annotation, Object object) {
    if (annotation != null) {
        Class<?> annotationClass = annotation.getClass();
        Class<?> objectClass = object.getClass();
        for (Method m : objectClass.getMethods()) {
            if (StringUtils.startsWith(m.getName(), "set")) {
                try {
                    String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
                    Object obj = annotationClass.getMethod(s).invoke(annotation);
                    if (obj != null && !"".equals(obj.toString())) {
                        if (object == null) {
                            object = objectClass.newInstance();
                        }
                        m.invoke(object, obj);
                    }
                } catch (Exception e) {
                    // 
                }
            }
        }
    }
}

From source file:com.yahoo.bard.webservice.web.filters.QueryParameterNormalizationFilter.java

/**
 * Extract a set of methods from a given class.
 *
 * @param clazz Class to extract methods from
 * @return  Stream of methods on class//from w  w w . ja v  a2 s  . c  o  m
 */
private static Stream<Method> extractMethods(Class clazz) {
    try {
        Method[] methods = clazz.getMethods();
        if (methods.length > 0) {
            return Stream.of(methods);
        }
    } catch (Exception | Error e) {
        LOG.warn("Problems loading class at startup: {}", clazz, e);
    }
    return Stream.empty();
}

From source file:hudson.util.ReflectionUtils.java

/**
 * Finds a public method of the given name, regardless of its parameter definitions,
 *///from   w  w w  .j a  v a 2 s  .  co  m
public static Method getPublicMethodNamed(Class c, String methodName) {
    for (Method m : c.getMethods())
        if (m.getName().equals(methodName))
            return m;
    return null;
}

From source file:com.yahoo.sql4d.sql4ddriver.Util.java

public static List<Method> getAllSetters(Class<?> clazz) {
    Method[] allMethods = clazz.getMethods();
    List<Method> setters = new ArrayList<>();
    for (Method method : allMethods) {
        if (method.getName().startsWith("set")) {
            setters.add(method);/*from   w w  w .j  a  va 2 s.c  o m*/
        }
    }
    return setters;
}

From source file:com.yahoo.sql4d.sql4ddriver.Util.java

public static List<Method> getAllGetters(Class<?> clazz) {
    Method[] allMethods = clazz.getMethods();
    List<Method> getters = new ArrayList<>();
    for (Method method : allMethods) {
        if (method.getName().startsWith("get") && !method.getName().equals("getClass")) {
            getters.add(method);/*  w ww  . j av  a2s.c  om*/
        }
    }
    return getters;
}