Search in sources :

Example 1 with Target

use of java.lang.annotation.Target in project error-prone by google.

the class QualifierWithTypeUse method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    if (IS_QUALIFIER_WITH_TARGET.matches(tree, state)) {
        MultiMatchResult<AnnotationTree> targetAnnotation = HAS_TARGET_ANNOTATION.multiMatchResult(tree, state);
        if (targetAnnotation.matches()) {
            AnnotationTree annotationTree = targetAnnotation.onlyMatchingNode();
            Target target = ASTHelpers.getAnnotation(tree, Target.class);
            if (hasTypeUseOrTypeParameter(target)) {
                return describeMatch(annotationTree, removeTypeUse(target, annotationTree));
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Target(java.lang.annotation.Target) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 2 with Target

use of java.lang.annotation.Target in project felix by apache.

the class Predicates method onlySupportedElements.

/**
 * Restrict to the supported {@link ElementType}(s) of the annotation (use the @Target, if provided).
 * @param annotationType annotation to explore
 */
public static Predicate onlySupportedElements(final Class<? extends Annotation> annotationType) {
    Target target = annotationType.getAnnotation(Target.class);
    if (target == null) {
        return alwaysTrue();
    }
    Collection<Predicate> supportedTypes = new HashSet<Predicate>();
    for (ElementType type : target.value()) {
        supportedTypes.add(on(type));
    }
    return or(supportedTypes);
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Predicate(org.apache.felix.ipojo.manipulator.spi.Predicate) HashSet(java.util.HashSet)

Example 3 with Target

use of java.lang.annotation.Target in project geronimo-xbean by apache.

the class AnnotationFinder method validTarget.

private static boolean validTarget(Class<? extends Annotation> type) {
    final Target target = type.getAnnotation(Target.class);
    if (target == null)
        return false;
    final ElementType[] targets = target.value();
    return targets.length == 1 && targets[0] == ElementType.ANNOTATION_TYPE;
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType)

Example 4 with Target

use of java.lang.annotation.Target in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method shouldIgnore.

/**
 * Returns true if {@code am} should not be inserted in source code, for example {@link
 * org.checkerframework.common.value.qual.BottomVal}. This happens when {@code am} cannot be
 * inserted in source code or is the default for the location passed as argument.
 *
 * <p>Invisible qualifiers, which are annotations that contain the {@link
 * org.checkerframework.framework.qual.InvisibleQualifier} meta-annotation, also return true.
 *
 * <p>TODO: Merge functionality somewhere else with {@link
 * org.checkerframework.framework.type.GenericAnnotatedTypeFactory#createQualifierDefaults}.
 * Look into the createQualifierDefaults method before changing anything here. See Issue 683
 * https://github.com/typetools/checker-framework/issues/683
 */
private boolean shouldIgnore(AnnotationMirror am, TypeUseLocation location, AnnotatedTypeFactory atf, AnnotatedTypeMirror atm) {
    Element elt = am.getAnnotationType().asElement();
    // Checks if am is an implementation detail (a type qualifier used
    // internally by the type system and not meant to be seen by the user.)
    Target target = elt.getAnnotation(Target.class);
    if (target != null && target.value().length == 0)
        return true;
    if (elt.getAnnotation(InvisibleQualifier.class) != null)
        return true;
    // Checks if am is default
    if (elt.getAnnotation(DefaultQualifierInHierarchy.class) != null) {
        return true;
    }
    DefaultQualifier defaultQual = elt.getAnnotation(DefaultQualifier.class);
    if (defaultQual != null) {
        for (TypeUseLocation loc : defaultQual.locations()) {
            if (loc == TypeUseLocation.ALL || loc == location) {
                return true;
            }
        }
    }
    DefaultFor defaultQualForLocation = elt.getAnnotation(DefaultFor.class);
    if (defaultQualForLocation != null) {
        for (TypeUseLocation loc : defaultQualForLocation.value()) {
            if (loc == TypeUseLocation.ALL || loc == location) {
                return true;
            }
        }
    }
    // Checks if am is an implicit annotation.
    // This case checks if it is meta-annotated with @ImplicitFor.
    // TODO: Handle cases of implicit annotations added via an
    // org.checkerframework.framework.type.treeannotator.ImplicitsTreeAnnotator.
    ImplicitFor implicitFor = elt.getAnnotation(ImplicitFor.class);
    if (implicitFor != null) {
        org.checkerframework.framework.qual.TypeKind[] types = implicitFor.types();
        TypeKind atmKind = atm.getUnderlyingType().getKind();
        if (hasMatchingTypeKind(atmKind, types)) {
            return true;
        }
        try {
            Class<?>[] names = implicitFor.typeNames();
            for (Class<?> c : names) {
                TypeMirror underlyingtype = atm.getUnderlyingType();
                while (underlyingtype instanceof javax.lang.model.type.ArrayType) {
                    underlyingtype = ((javax.lang.model.type.ArrayType) underlyingtype).getComponentType();
                }
                if (c.getCanonicalName().equals(atm.getUnderlyingType().toString())) {
                    return true;
                }
            }
        } catch (MirroredTypesException e) {
        }
    }
    return false;
}
Also used : MirroredTypesException(javax.lang.model.type.MirroredTypesException) ImplicitFor(org.checkerframework.framework.qual.ImplicitFor) ATypeElement(scenelib.annotations.el.ATypeElement) Element(javax.lang.model.element.Element) TypeKind(javax.lang.model.type.TypeKind) InvisibleQualifier(org.checkerframework.framework.qual.InvisibleQualifier) DefaultFor(org.checkerframework.framework.qual.DefaultFor) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Target(java.lang.annotation.Target) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) AClass(scenelib.annotations.el.AClass) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

Example 5 with Target

use of java.lang.annotation.Target in project spoon by INRIA.

the class AnnotationTest method testPersistenceProperty.

@Test
public void testPersistenceProperty() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PersistenceProperty.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.PersistenceProperty");
    assertEquals("PersistenceProperty", type.getSimpleName());
    assertEquals(2, type.getAnnotations().size());
    CtAnnotation<Target> a1 = type.getAnnotation(type.getFactory().Type().createReference(Target.class));
    assertNotNull(a1);
    CtAnnotation<Retention> a2 = type.getAnnotation(type.getFactory().Type().createReference(Retention.class));
    assertNotNull(a2);
    assertTrue(a1.getValues().containsKey("value"));
    assertTrue(a2.getValues().containsKey("value"));
}
Also used : Target(java.lang.annotation.Target) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Retention(java.lang.annotation.Retention) Test(org.junit.Test)

Aggregations

Target (java.lang.annotation.Target)20 ElementType (java.lang.annotation.ElementType)13 Retention (java.lang.annotation.Retention)5 RetentionPolicy (java.lang.annotation.RetentionPolicy)4 ClassName (com.squareup.javapoet.ClassName)3 Method (java.lang.reflect.Method)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)3 Test (org.junit.Test)3 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 AnnotationTree (com.sun.source.tree.AnnotationTree)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DeclaredType (javax.lang.model.type.DeclaredType)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)2 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)2