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:net.abhinavsarkar.spelhelper.SpelHelper.java

private static List<Method> filterMethods(final Class<?> clazz) {
    List<Method> allowedMethods = new ArrayList<Method>();
    for (Method method : clazz.getMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
                && !method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length > 0) {
            allowedMethods.add(method);//from  w w  w  . j  a  v a  2s. co m
        }
    }
    return allowedMethods;
}

From source file:net.abhinavsarkar.spelhelper.SpelHelper.java

private static List<Method> filterFunctions(final Class<?> clazz) {
    List<Method> allowedMethods = new ArrayList<Method>();
    for (Method method : clazz.getMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
                && !method.getReturnType().equals(Void.TYPE)) {
            allowedMethods.add(method);/*from  www .  ja v a 2 s.  c o  m*/
        }
    }
    return allowedMethods;
}

From source file:net.sf.ehcache.config.BeanHandler.java

/**
 * Finds a creator method.//w  ww  .  j  a  v a  2  s .co m
 */
private static Method findCreateMethod(Class objClass, String name) {
    final String methodName = makeMethodName("create", name);
    final Method[] methods = objClass.getMethods();
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        if (!method.getName().equals(methodName)) {
            continue;
        }
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }
        if (method.getParameterTypes().length != 0) {
            continue;
        }
        if (method.getReturnType().isPrimitive() || method.getReturnType().isArray()) {
            continue;
        }
        return method;
    }

    return null;
}

From source file:RssAtomGenerationTest.java

public static <T> T convertObject(final T destination, final Object source, FeedType type, Object... context) {
    Map<Class, Object> contentMap = new HashMap<Class, Object>();
    for (Object o : context) {
        contentMap.put(o.getClass(), o);
    }/*from  ww  w  . ja v  a2s .co m*/
    final Method[] methods = source.getClass().getMethods();
    for (Method method : methods) {
        final boolean refPresent = method.isAnnotationPresent(SyndicationRefs.class);
        if (refPresent || (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type))) {
            SyndicationElement annotation = null;
            if (refPresent) {
                final SyndicationElement[] value = method.getAnnotation(SyndicationRefs.class).value();
                for (SyndicationElement element : value) {
                    if (element.type().equals(type)) {
                        annotation = element;
                        break;
                    }
                }
                if (annotation == null) {
                    continue;
                }
            } else {
                annotation = method.getAnnotation(SyndicationElement.class);
            }
            //final SyndicationElement annotation =
            final String name = annotation.name();
            try {
                final Object initValue = method.invoke(source);
                Object value = null;
                if (!(annotation.converter().isAssignableFrom(NoopConverter.class))) {
                    final Converter converter = annotation.converter().newInstance();
                    value = converter.convert(initValue);
                }
                if (!(annotation.transformer().isAssignableFrom(NoopTransformer.class))) {
                    contentMap.put(initValue.getClass(), initValue);
                    final Class<?> transformer = annotation.transformer();
                    final Method[] transformerMethods = transformer.getMethods();
                    for (Method transformerMethod : transformerMethods) {
                        if (transformerMethod.isAnnotationPresent(ContextTransformable.class)) {
                            final Class<?>[] parameterTypes = transformerMethod.getParameterTypes();
                            List<Object> parameters = new ArrayList<Object>();
                            for (Class clazz : parameterTypes) {
                                if (contentMap.containsKey(clazz)) {
                                    parameters.add(contentMap.get(clazz));
                                } else {
                                    boolean found = false;
                                    for (Object obj : contentMap.values()) {
                                        if (clazz.isInstance(obj)) {
                                            parameters.add(obj);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found) {
                                        parameters.add(null);
                                    }
                                }
                            }
                            if (Modifier.isStatic(transformerMethod.getModifiers())) {
                                value = transformerMethod.invoke(null, parameters.toArray());
                            } else {
                                value = transformerMethod.invoke(transformer.newInstance(),
                                        parameters.toArray());
                            }
                            break;
                        }
                    }

                }
                BeanUtils.setProperty(destination, name, value);
            } catch (Exception e) {
                log.error("test", e);
                e.printStackTrace();
            }

        }
    }
    return destination;
}

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

/**
 * Gets the attributes of a type.//www.  ja v a  2  s.  c o  m
 * 
 * @param type
 * @return
 */
public static Map<String, Class> getAttributes(Class type) {
    Map<String, Class> attributes = new HashMap<String, Class>();
    Method[] methods = type.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().startsWith("get") && !"getClass".equals(methods[i].getName())
                && methods[i].getName().length() > 3 && methods[i].getParameterTypes().length == 0) {
            String attributeName = StringUtils.uncapitalize(methods[i].getName().substring(3));
            Class returnType = methods[i].getReturnType();
            if (CollectionBuilderFactory.isCollectionType(returnType)) {
                attributeName = attributeName + "[0]" + getExtraMultiDimensionalArraySuffix(returnType);
                returnType = CollectionBuilderFactory.getElementType(returnType);
            }
            attributes.put(attributeName, returnType);
        }
    }
    return attributes;
}

From source file:org.synyx.hades.util.ClassUtils.java

/**
 * Returns the given base class' method if the given method (declared in the
 * interface) was also declared at the base class. Returns the given method
 * if the given base class does not declare the method given. Takes generics
 * into account./*from  ww  w .  j a v  a2  s . c o m*/
 * 
 * @param method
 * @param baseClass
 * @param daoInterface
 * @return
 */
public static Method getBaseClassMethodFor(Method method, Class<?> baseClass, Class<?> daoInterface) {

    for (Method daoClassMethod : baseClass.getMethods()) {

        // Wrong name
        if (!method.getName().equals(daoClassMethod.getName())) {
            continue;
        }

        // Wrong number of arguments
        if (!(method.getParameterTypes().length == daoClassMethod.getParameterTypes().length)) {
            continue;
        }

        // Check whether all parameters match
        if (!parametersMatch(method, daoClassMethod, daoInterface)) {
            continue;
        }

        return daoClassMethod;
    }

    return method;
}

From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java

static <T> Map<String, Supplier<?>> getMethodSuppliers(Class<T> configClass,
        DynamicPropertyFactory propertyFactory, AbstractConfiguration configuration) {
    final Map<String, Supplier<?>> properties = Maps.newHashMap();

    for (Method method : configClass.getMethods()) {
        Configuration c = method.getAnnotation(Configuration.class);
        if (c == null)
            continue;
        String defaultValue = null;
        DefaultValue dv = method.getAnnotation(DefaultValue.class);
        if (dv != null)
            defaultValue = dv.value();/* w w w . ja va2s .  com*/

        String name = getPropertyName(method, c);

        if (method.getReturnType().isAssignableFrom(Supplier.class)) {
            Type returnType = method.getGenericReturnType();

            if (returnType instanceof ParameterizedType) {
                ParameterizedType type = (ParameterizedType) returnType;
                Class<?> actualType = (Class<?>) type.getActualTypeArguments()[0];

                properties.put(method.getName(),
                        method.getAnnotation(Dynamic.class) != null
                                ? Suppliers.ofInstance(
                                        getDynamicSupplier(actualType, name, defaultValue, propertyFactory))
                                : Suppliers.ofInstance(
                                        getStaticSupplier(actualType, name, defaultValue, configuration)));
            } else {
                throw new RuntimeException("We'll get to this later");
            }
        } else {
            properties.put(method.getName(),
                    method.getAnnotation(Dynamic.class) != null
                            ? getDynamicSupplier(method.getReturnType(), name, defaultValue, propertyFactory)
                            : getStaticSupplier(method.getReturnType(), name, defaultValue, configuration));
        }
    }

    return properties;
}

From source file:edu.stanford.epad.common.plugins.impl.ClassFinderTestUtils.java

public static List<Method> findMethodsWithAnnotation(Class<?> testClass,
        Class<? extends Annotation> annotationClass) {
    List<Method> retVal = new ArrayList<Method>();
    for (Method method : testClass.getMethods()) {
        if (method.isAnnotationPresent(PluginHandler.class)) {
            retVal.add(method);//from   www  .j  av  a2 s.c  o m
            logger.info("Found ePad plugin-class: " + testClass.getName());
        }
    }
    return retVal;
}

From source file:ReflectUtils.java

public static Method getFirstMethod(Class theClass, String name) {
    Method[] methods = theClass.getMethods();

    for (int n = 0; n < methods.length; ++n)
        if (name.equals(methods[n].getName()))
            return methods[n];

    return null;//  w w w. j  av a  2 s  .com
}

From source file:org.zywx.wbpalmstar.plugin.uexiconlist.utils.IconListUtils.java

public static boolean isMethodExist(String className, String methodName) {
    boolean isExist = false;
    try {//from  ww w.  ja  va 2s . c  om
        Class<?> mClass = Class.forName(className);
        Method[] methodList = mClass.getMethods();
        for (Method method : methodList) {
            if (methodName.equals(method.getName())) {
                isExist = true;
                break;
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return isExist;
}