Search in sources :

Example 56 with Annotation

use of java.lang.annotation.Annotation in project morphia by mongodb.

the class MappedField method discoverConstructor.

private Constructor discoverConstructor() {
    Class<?> type = null;
    // get the first annotation with a concreteClass that isn't Object.class
    for (final Annotation an : foundAnnotations.values()) {
        try {
            final Method m = an.getClass().getMethod("concreteClass");
            m.setAccessible(true);
            final Object o = m.invoke(an);
            //noinspection EqualsBetweenInconvertibleTypes
            if (o != null && !(o.equals(Object.class))) {
                type = (Class) o;
                break;
            }
        } catch (NoSuchMethodException e) {
        // do nothing
        } catch (IllegalArgumentException e) {
            if (LOG.isWarningEnabled()) {
                LOG.warning("There should not be an argument", e);
            }
        } catch (Exception e) {
            if (LOG.isWarningEnabled()) {
                LOG.warning("", e);
            }
        }
    }
    if (type != null) {
        try {
            constructor = type.getDeclaredConstructor();
            constructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            if (!hasAnnotation(ConstructorArgs.class)) {
                if (LOG.isWarningEnabled()) {
                    LOG.warning("No usable constructor for " + type.getName(), e);
                }
            }
        }
    } else {
        // see if we can create instances of the type used for declaration
        type = getType();
        // short circuit to avoid wasting time throwing an exception trying to get a constructor we know doesnt exist
        if (type == List.class || type == Map.class) {
            return null;
        }
        if (type != null) {
            try {
                constructor = type.getDeclaredConstructor();
                constructor.setAccessible(true);
            } catch (NoSuchMethodException e) {
            // never mind.
            } catch (SecurityException e) {
            // never mind.
            }
        }
    }
    return constructor;
}
Also used : DBObject(com.mongodb.DBObject) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 57 with Annotation

use of java.lang.annotation.Annotation in project netty-socketio by mrniko.

the class ScannerEngine method scan.

public void scan(Namespace namespace, Object object, Class<?> clazz) throws IllegalArgumentException {
    Method[] methods = clazz.getDeclaredMethods();
    if (!clazz.isAssignableFrom(object.getClass())) {
        for (Method method : methods) {
            for (AnnotationScanner annotationScanner : annotations) {
                Annotation ann = method.getAnnotation(annotationScanner.getScanAnnotation());
                if (ann != null) {
                    annotationScanner.validate(method, clazz);
                    Method m = findSimilarMethod(object.getClass(), method);
                    if (m != null) {
                        annotationScanner.addListener(namespace, object, m, ann);
                    } else {
                        log.warn("Method similar to " + method.getName() + " can't be found in " + object.getClass());
                    }
                }
            }
        }
    } else {
        for (Method method : methods) {
            for (AnnotationScanner annotationScanner : annotations) {
                Annotation ann = method.getAnnotation(annotationScanner.getScanAnnotation());
                if (ann != null) {
                    annotationScanner.validate(method, clazz);
                    makeAccessible(method);
                    annotationScanner.addListener(namespace, object, method, ann);
                }
            }
        }
        if (clazz.getSuperclass() != null) {
            scan(namespace, object, clazz.getSuperclass());
        } else if (clazz.isInterface()) {
            for (Class<?> superIfc : clazz.getInterfaces()) {
                scan(namespace, object, superIfc);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 58 with Annotation

use of java.lang.annotation.Annotation in project netty-socketio by mrniko.

the class SpringAnnotationScanner method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    final AtomicBoolean add = new AtomicBoolean();
    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            add.set(true);
        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            for (Class<? extends Annotation> annotationClass : annotations) {
                if (method.isAnnotationPresent(annotationClass)) {
                    return true;
                }
            }
            return false;
        }
    });
    if (add.get()) {
        originalBeanClass = bean.getClass();
    }
    return bean;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) Method(java.lang.reflect.Method) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback) Annotation(java.lang.annotation.Annotation)

Example 59 with Annotation

use of java.lang.annotation.Annotation in project ninja by ninjaframework.

the class JaxyRoutes method findControllerMethods.

/**
     * Searches for Methods that have either a Path Annotation or a HTTP-Method Annotation
     */
@SuppressWarnings("unchecked")
private Set<Method> findControllerMethods() {
    Set<Method> methods = Sets.newLinkedHashSet();
    methods.addAll(reflections.getMethodsAnnotatedWith(Path.class));
    Reflections annotationReflections = new Reflections("", new TypeAnnotationsScanner(), new SubTypesScanner());
    for (Class<?> httpMethod : annotationReflections.getTypesAnnotatedWith(HttpMethod.class)) {
        if (httpMethod.isAnnotation()) {
            methods.addAll(reflections.getMethodsAnnotatedWith((Class<? extends Annotation>) httpMethod));
        }
    }
    return methods;
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Reflections(org.reflections.Reflections)

Example 60 with Annotation

use of java.lang.annotation.Annotation in project neo4j by neo4j.

the class PluginPointFactoryImpl method createFrom.

public PluginPoint createFrom(ServerPlugin plugin, Method method, Class<?> discovery) {
    ResultConverter result = ResultConverter.get(method.getGenericReturnType());
    Type[] types = method.getGenericParameterTypes();
    Annotation[][] annotations = method.getParameterAnnotations();
    SourceExtractor sourceExtractor = null;
    DataExtractor[] extractors = new DataExtractor[types.length];
    for (int i = 0; i < types.length; i++) {
        Description description = null;
        Parameter param = null;
        Source source = null;
        for (Annotation annotation : annotations[i]) {
            if (annotation instanceof Description) {
                description = (Description) annotation;
            } else if (annotation instanceof Parameter) {
                param = (Parameter) annotation;
            } else if (annotation instanceof Source) {
                source = (Source) annotation;
            }
        }
        if (param != null && source != null) {
            throw new IllegalStateException(String.format("Method parameter %d of %s cannot be retrieved as both Parameter and Source", Integer.valueOf(i), method));
        } else if (source != null) {
            if (types[i] != discovery) {
                throw new IllegalStateException("Source parameter type (" + types[i] + ") must equal the discovery type (" + discovery.getName() + ").");
            }
            if (sourceExtractor != null) {
                throw new IllegalStateException("Server Extension methods may have at most one Source parameter.");
            }
            extractors[i] = sourceExtractor = new SourceExtractor(source, description);
        } else if (param != null) {
            extractors[i] = parameterExtractor(types[i], param, description);
        } else {
            throw new IllegalStateException("Parameters of Server Extension methods must be annotated as either Source or Parameter.");
        }
    }
    return new PluginMethod(nameOf(method), discovery, plugin, result, method, extractors, method.getAnnotation(Description.class));
}
Also used : Annotation(java.lang.annotation.Annotation) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RelationshipType(org.neo4j.graphdb.RelationshipType)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13