Example usage for java.lang.annotation ElementType ANNOTATION_TYPE

List of usage examples for java.lang.annotation ElementType ANNOTATION_TYPE

Introduction

In this page you can find the example usage for java.lang.annotation ElementType ANNOTATION_TYPE.

Prototype

ElementType ANNOTATION_TYPE

To view the source code for java.lang.annotation ElementType ANNOTATION_TYPE.

Click Source Link

Document

Annotation type declaration

Usage

From source file:org.jgentleframework.utils.ReflectUtils.java

/**
 * Phng thc s kim tra xem <code>annotation</code> ch nh c
 * {@link Target} tng ng hp l vi i tng <code>object</code> hay
 * khng (i tng <code>object</code> c th l bt k thc th ch nh
 * no cho php annotate annotation nh/*  ww w.jav  a2 s .c  om*/
 * <code>Class, Interface, Annotation, Enum, Method, Field, Constructor, ...</code>
 * )
 * 
 * @param annotation
 *            i tng <code>annotation</code> cn kim tra.
 * @param obj
 *            i tng <code>object</code> cn kim tra
 * @return tr v? <b>true</b> nu c, nu khng tr v? <b>false</b>.
 */
public static boolean isValidTarget(Annotation annotation, Object obj) {

    if (annotation.annotationType().isAnnotationPresent(Target.class)) {
        Target target = annotation.annotationType().getAnnotation(Target.class);
        if (obj.getClass().isAnnotation()) {
            if (!Arrays.asList(target.value()).contains(ElementType.TYPE)
                    || !Arrays.asList(target.value()).contains(ElementType.ANNOTATION_TYPE)) {
                return false;
            }
        } else if (ReflectUtils.isCast(Constructor.class, obj)) {
            if (!Arrays.asList(target.value()).contains(ElementType.CONSTRUCTOR)) {
                return false;
            }
        } else if (ReflectUtils.isField(obj)) {
            if (!Arrays.asList(target.value()).contains(ElementType.FIELD)) {
                return false;
            }
        } else if (ReflectUtils.isMethod(obj)) {
            if (!Arrays.asList(target.value()).contains(ElementType.METHOD)) {
                return false;
            }
        } else if (ReflectUtils.isClass(obj) || ReflectUtils.isInterface(obj)) {
            if (!Arrays.asList(target.value()).contains(ElementType.TYPE)) {
                return false;
            }
        } else {
            if (!obj.getClass().isAnnotation() && !ReflectUtils.isCast(Constructor.class, obj)
                    && !ReflectUtils.isField(obj) && !ReflectUtils.isMethod(obj.getClass())
                    && !ReflectUtils.isClass(obj) && !ReflectUtils.isInterface(obj)) {
                return false;
            }
        }
    }
    return true;
}

From source file:org.mule.util.scan.ClasspathScanner.java

/**
 * Works out the correct scanner based on the class passed in
 * <p/>//from ww w.java 2s  .co m
 * Note that these could be better architected by breaking out filters into strategy objects, but for now this
 * suits my needs
 *
 * @param clazz the type to scan for
 * @return a scanner suitable for handling the type passed in
 * @see AnnotationsScanner
 * @see InterfaceClassScanner
 * @see ImplementationClassScanner
 */
protected ClassScanner getScanner(Class<?> clazz) {
    if (clazz.isInterface()) {
        if (clazz.isAnnotation()) {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) clazz;

            AnnotationFilter filter = null;
            Annotation[] annos = clazz.getDeclaredAnnotations();
            for (int i = 0; i < annos.length; i++) {
                Annotation anno = annos[i];
                if (anno instanceof Target) {
                    if (((Target) anno).value()[0] == ElementType.ANNOTATION_TYPE) {
                        filter = new MetaAnnotationTypeFilter(annotationClass, classLoader);
                    }
                }
            }
            if (filter == null) {
                filter = new AnnotationTypeFilter(annotationClass);
            }
            return new AnnotationsScanner(filter);
        } else {
            return new InterfaceClassScanner(clazz);
        }
    } else {
        return new ImplementationClassScanner(clazz);
    }
}