Example usage for org.apache.commons.lang3.reflect MethodUtils getMethodsWithAnnotation

List of usage examples for org.apache.commons.lang3.reflect MethodUtils getMethodsWithAnnotation

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect MethodUtils getMethodsWithAnnotation.

Prototype

public static Method[] getMethodsWithAnnotation(final Class<?> cls,
        final Class<? extends Annotation> annotationCls) 

Source Link

Document

Gets all methods of the given class that are annotated with the given annotation.

Usage

From source file:lite.flow.util.ActivityInspector.java

protected static EntryPoint[] getEntryAnnotatedMethods(Class<?> clazz) {
    Method[] methods = MethodUtils.getMethodsWithAnnotation(clazz, Entry.class);

    EntryPoint[] entryPoints = new EntryPoint[methods.length];
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        Entry annotation = method.getAnnotation(Entry.class);
        EntryPoint entryPoint = new EntryPoint(method, annotation.inNames(), annotation.outName());
        entryPoints[i] = entryPoint;/*w  w w.j a v  a  2 s . co  m*/
    }

    return entryPoints;
}

From source file:org.grouplens.grapht.reflect.internal.ClassInstantiator.java

@Override
public Object instantiate() throws ConstructionException {
    // find constructor and build up necessary constructor arguments

    Constructor<?> ctor = getConstructor();
    LogContext globalLogContext = LogContext.create();
    Object instance = null;/*  w  ww  . ja v  a2 s  . c o  m*/
    Method[] methods;

    try {
        // create the instance that we are injecting
        try {
            globalLogContext.put("org.grouplens.grapht.class", ctor.getClass().toString());
            Object[] ctorArgs = new Object[ctor.getParameterTypes().length];
            for (Desire d : desires) {
                LogContext ipContext = LogContext.create();
                if (d.getInjectionPoint() instanceof ConstructorParameterInjectionPoint) {
                    // this desire is a constructor argument so create it now
                    Instantiator provider = providers.get(d);
                    ConstructorParameterInjectionPoint cd = (ConstructorParameterInjectionPoint) d
                            .getInjectionPoint();
                    logger.trace("Injection point satisfactions in progress {}", cd);
                    try {
                        ipContext.put("org.grouplens.grapht.injectionPoint", cd.toString());
                    } finally {
                        ipContext.finish();
                    }
                    ctorArgs[cd.getParameterIndex()] = checkNull(cd, provider.instantiate());
                }
            }
            logger.trace("Invoking constructor {} with arguments {}", ctor, ctorArgs);
            ctor.setAccessible(true);
            instance = ctor.newInstance(ctorArgs);
        } catch (InvocationTargetException e) {
            throw new ConstructionException(ctor, "Constructor " + ctor + " failed", e);
        } catch (InstantiationException e) {
            throw new ConstructionException(ctor, "Could not instantiate " + type, e);
        } catch (IllegalAccessException e) {
            throw new ConstructionException(ctor, "Access violation on " + ctor, e);
        }

        // satisfy dependencies in the order of the list, which was
        // prepared to comply with JSR 330
        Map<Method, InjectionArgs> settersAndArguments = new HashMap<Method, InjectionArgs>();
        for (Desire d : desires) {
            LogContext ipContext = LogContext.create();
            try {
                final InjectionStrategy injectionStrategy = InjectionStrategy
                        .forInjectionPoint(d.getInjectionPoint());
                ipContext.put("org.grouplens.grapht.injectionPoint", d.getInjectionPoint().toString());
                injectionStrategy.inject(d.getInjectionPoint(), instance, providers.get(d),
                        settersAndArguments);
            } finally {
                ipContext.finish();
            }
        }
    } finally {
        globalLogContext.finish();
    }
    if (manager != null) {
        manager.registerComponent(instance);
    }

    methods = MethodUtils.getMethodsWithAnnotation(type, PostConstruct.class);
    for (Method method : methods) {
        method.setAccessible(true);
        try {
            method.invoke(instance);
        } catch (InvocationTargetException e) {
            throw new ConstructionException("Exception throw by " + method, e);
        } catch (IllegalAccessException e) {
            throw new ConstructionException("Access violation invoking " + method, e);
        }
    }

    // the instance has been fully configured
    return instance;
}