Search in sources :

Example 6 with ElementType

use of java.lang.annotation.ElementType in project byte-buddy by raphw.

the class TypeDescriptionGenericVisitorValidatorForTypeAnnotations method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(otherLegal.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(true);
    when(otherIllegal.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(false);
    when(illegal.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(illegalAnnotation));
    when(illegalAnnotation.getElementTypes()).thenReturn(new HashSet<ElementType>());
    when(illegalAnnotation.getAnnotationType()).thenReturn(illegalType);
    when(otherLegal.asGenericType()).thenReturn(otherLegal);
    when(otherIllegal.asGenericType()).thenReturn(otherIllegal);
    try {
        Enum<?> typeUse = Enum.valueOf(ElementType.class, "TYPE_USE");
        Enum<?> typeParameter = Enum.valueOf(ElementType.class, "TYPE_PARAMETER");
        when(legalAnnotation.getElementTypes()).thenReturn(new HashSet(Arrays.asList(typeUse, typeParameter)));
        when(duplicateAnnotation.getElementTypes()).thenReturn(new HashSet(Arrays.asList(typeUse, typeParameter)));
    } catch (IllegalArgumentException ignored) {
        when(legalAnnotation.getElementTypes()).thenReturn(Collections.<ElementType>emptySet());
        when(duplicateAnnotation.getElementTypes()).thenReturn(Collections.<ElementType>emptySet());
    }
    when(legal.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(legalAnnotation));
    when(duplicate.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(legalAnnotation, duplicateAnnotation));
    when(legalAnnotation.getAnnotationType()).thenReturn(legalType);
    when(duplicateAnnotation.getAnnotationType()).thenReturn(legalType);
}
Also used : ElementType(java.lang.annotation.ElementType) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 7 with ElementType

use of java.lang.annotation.ElementType in project epoxy by airbnb.

the class AttributeInfo method buildAnnotationLists.

/**
   * Keeps track of annotations on the attribute so that they can be used in the generated setter
   * and getter method. Setter and getter annotations are stored separately since the annotation may
   * not target both method and parameter types.
   */
private void buildAnnotationLists(List<? extends AnnotationMirror> annotationMirrors) {
    for (AnnotationMirror annotationMirror : annotationMirrors) {
        if (!annotationMirror.getElementValues().isEmpty()) {
            // Not supporting annotations with values for now
            continue;
        }
        ClassName annotationClass = ClassName.bestGuess(annotationMirror.getAnnotationType().toString());
        if (annotationClass.equals(ClassName.get(EpoxyAttribute.class))) {
            // Don't include our own annotation
            continue;
        }
        DeclaredType annotationType = annotationMirror.getAnnotationType();
        // A target may exist on an annotation type to specify where the annotation can
        // be used, for example fields, methods, or parameters.
        Target targetAnnotation = annotationType.asElement().getAnnotation(Target.class);
        // Allow all target types if no target was specified on the annotation
        List<ElementType> elementTypes = Arrays.asList(targetAnnotation == null ? ElementType.values() : targetAnnotation.value());
        AnnotationSpec annotationSpec = AnnotationSpec.builder(annotationClass).build();
        if (elementTypes.contains(ElementType.PARAMETER)) {
            setterAnnotations.add(annotationSpec);
        }
        if (elementTypes.contains(ElementType.METHOD)) {
            getterAnnotations.add(annotationSpec);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) ClassName(com.squareup.javapoet.ClassName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ElementType (java.lang.annotation.ElementType)7 RetentionPolicy (java.lang.annotation.RetentionPolicy)4 Target (java.lang.annotation.Target)4 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)4 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)4 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)4 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)4 Retention (java.lang.annotation.Retention)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 ClassName (com.squareup.javapoet.ClassName)1 HashSet (java.util.HashSet)1 Nullable (javax.annotation.Nullable)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 DeclaredType (javax.lang.model.type.DeclaredType)1 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)1 Before (org.junit.Before)1