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

public static List<Method> getSyndicationElementsOfType(Class clazz, FeedType type) {
    List<Method> methodList = new ArrayList<Method>();
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type)) {
            methodList.add(method);//from   w w w . ja v a2s.  com
        }
    }
    return methodList;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

public static Map<String, Map<Class<?>, Method>> findUnaryMethods(Class<?> clazz) {
    Map<String, Map<Class<?>, Method>> result = new TreeMap<>();

    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        int modified = method.getModifiers();
        if (Modifier.isPublic(modified) && method.getParameterTypes().length == 1) {
            Map<Class<?>, Method> methodMap = result.get(method.getName());
            if (methodMap == null) {
                methodMap = new HashMap<>();
                result.put(method.getName(), methodMap);
            }/*from   w  w w . ja va2  s . c  o  m*/
            methodMap.put(method.getParameterTypes()[0], method);
        }
    }

    return result;
}

From source file:org.openflamingo.uploader.el.ELService.java

public static Method findMethod(String className, String methodName) throws SystemException {
    Method method = null;/*from  w w w .j a va  2  s .  c  o m*/
    try {
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        for (Method m : clazz.getMethods()) {
            if (m.getName().equals(methodName)) {
                method = m;
                break;
            }
        }
        if (method == null) {
            // throw new SystemException(ErrorCode.E0111, className, methodName);
        }
        if ((method.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC
                | Modifier.STATIC)) {
            // throw new SystemException(ErrorCode.E0112, className, methodName);
        }
    } catch (ClassNotFoundException ex) {
        // throw new SystemException(ErrorCode.E0113, className);
    }
    return method;
}

From source file:net.sourceforge.fenixedu.webServices.jersey.api.FenixJerseyAPIConfig.java

private static void searchForAPIFenixScope(Class<?> apiClass) {
    if (apiClass != null) {
        Path pathAnnotation = apiClass.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            for (Method method : apiClass.getMethods()) {
                Path methodPathAnnotation = method.getAnnotation(Path.class);
                FenixAPIScope apiScopeAnnotation = method.getAnnotation(FenixAPIScope.class);
                if (apiScopeAnnotation != null) {
                    if (methodPathAnnotation != null) {
                        String path = ends(pathAnnotation.value());
                        String methodPath = ends(methodPathAnnotation.value());
                        String absolutePath = Joiner.on("/").join(path, methodPath);
                        String scopeName = apiScopeAnnotation.value();
                        scopePathsMap.put(scopeName, absolutePath);
                        LOGGER.debug("add {} to scope {}", absolutePath, scopeName);
                    } else {
                        LOGGER.debug("No path for method {}", method.getName());
                    }/*from  ww w. j a  va2 s .c o m*/
                } else {
                    FenixAPIPublic publicAPIAnnotation = method.getAnnotation(FenixAPIPublic.class);
                    if (publicAPIAnnotation != null) {
                        if (methodPathAnnotation != null) {
                            String path = ends(pathAnnotation.value());
                            String methodPath = ends(methodPathAnnotation.value());
                            String absolutePath = Joiner.on("/").join(path, methodPath);
                            publicScopes.add(absolutePath);
                            LOGGER.debug("add public endpoint {}", absolutePath);
                        }
                    }
                }
            }
        } else {
            LOGGER.debug("No api class");
        }
    }
}

From source file:net.servicefixture.util.ReflectionUtils.java

/**
 * Returns the first public method that matches method name.
 *//*from   ww  w  .  j a  va  2s. co  m*/
public static Method findMethodByName(Class type, String methodName) {
    Method[] methods = type.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equals(methodName) && Modifier.isPublic(method.getModifiers())) {
            return method;
        }
    }
    return null;
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

public static Map<String, Property> getProperties(Class<? extends ValidationObject> class1) {
    Map<String, Property> ret = new HashMap<>(class1.getMethods().length);
    for (Method method : class1.getMethods()) {
        final String fieldName = ValidationUtils.getFieldNameFromGetterMethodName(method.getName());
        if (!ValidationUtils.isUsefulField(fieldName, null)) {
            continue;
        }//  ww w.j a  v  a  2 s.c  om
        Method getter = ValidationUtils.figureGetter(fieldName, class1);
        if (getter.isAnnotationPresent(Ignore.class)) {
            continue;
        }
        Method setter = ValidationUtils.figureSetter(fieldName, class1);
        ret.put(fieldName, new Property(fieldName, getter, setter, class1));
    }
    return ret;

}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

private static Method figureSetter(final String name, final Class<?> clazz) {
    final String setter = figureSetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(setter)) {
            return method;
        }/* ww  w .jav a2  s  . co  m*/
    }
    throw new RuntimeException("Could not find method " + setter + " on class " + clazz.getName());
}

From source file:ar.com.zauber.commons.spring.web.CommandURLParameterGenerator.java

/**
 * @see #getURLParameter(Class, Set)//from  w w w.  j ava 2 s. c o  m
 */
public static String getURLParameter(final Class<?> cmdClass, final Map<Class<?>, Object> values) {
    final StringBuilder sb = new StringBuilder("?");
    final Pattern getter = Pattern.compile("^get(.*)$");
    boolean first = true;

    // look for getters
    for (final Method method : cmdClass.getMethods()) {
        final Matcher matcher = getter.matcher(method.getName());
        if (matcher.lookingAt() && matcher.groupCount() == 1 && method.getParameterTypes().length == 0
                && values.containsKey(method.getReturnType())) {
            try {
                cmdClass.getMethod("set" + matcher.group(1), new Class[] { method.getReturnType() });
                if (!first) {
                    sb.append("&");
                } else {
                    first = false;
                }

                sb.append(URLEncoder.encode(matcher.group(1).toLowerCase(), CHARSET));
                sb.append("=");
                sb.append(URLEncoder.encode(values.get(method.getReturnType()).toString(), CHARSET));
            } catch (Exception e) {
                // skip
            }
        }
    }

    return sb.toString();
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

public static Method figureGetter(final String name, final Class<?> clazz) {
    final String getter = figureGetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(getter)) {
            return method;
        }/*  ww w  .j  a  va 2 s . com*/
    }
    final String isGetter = figureIsGetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(isGetter)) {
            return method;
        }
    }
    throw new RuntimeException("Could not find method " + getter + " on class " + clazz.getName());
}

From source file:com.relicum.ipsum.Reflection.ReflectionUtil.java

/**
 * Gets a {@link Method} in a given {@link Class} object.
 *
 * @param clazz Class object/*w w  w  .j  av a  2 s.co  m*/
 * @param name  Method name
 * @return The method, or null if none exists
 */
public static final Method getMethod(Class<?> clazz, String name) {
    Validate.notNull(clazz, "clazz cannot be null!");
    Validate.notNull(name, "name cannot be null!");

    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(name))
            return method;
    }

    return null;
}