Search in sources :

Example 16 with Target

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

the class TypeAnnotationMover method isDeclarationAnnotation.

/**
 * Returns whether the annotation represented by {@code annotationDeclaration} might be a
 * declaration annotation for {@code declarationType}. This holds if the TypeElement has no
 * {@code @Target} meta-annotation, or if {@code declarationType} is a target of the annotation.
 *
 * @param annotationDeclaration declaration for an annotation
 * @param declarationType the declaration type to check if the annotation might be a declaration
 *     annotation for
 * @return true if {@code annotationDeclaration} contains {@code declarationType} as a target or
 *     doesn't contain {@code ElementType.TYPE_USE} as a target
 */
private boolean isDeclarationAnnotation(TypeElement annotationDeclaration, ElementType declarationType) {
    Target target = annotationDeclaration.getAnnotation(Target.class);
    if (target == null) {
        return true;
    }
    boolean hasTypeUse = false;
    for (ElementType elementType : target.value()) {
        if (elementType == declarationType) {
            return true;
        }
        if (elementType == ElementType.TYPE_USE) {
            hasTypeUse = true;
        }
    }
    if (!hasTypeUse) {
        throw new Error(String.format("Annotation %s cannot be used on declaration with type %s", annotationDeclaration.getQualifiedName(), declarationType));
    }
    return false;
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType)

Example 17 with Target

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

the class AnnotationUtils method isDeclarationAnnotation.

/**
 * Returns true if anno is a declaration annotation. In other words, returns true if anno cannot
 * be written on uses of types.
 *
 * @param anno the AnnotationMirror
 * @return true if anno is a declaration annotation
 */
public static boolean isDeclarationAnnotation(AnnotationMirror anno) {
    TypeElement elem = (TypeElement) anno.getAnnotationType().asElement();
    Target t = elem.getAnnotation(Target.class);
    if (t == null) {
        return true;
    }
    for (ElementType elementType : t.value()) {
        if (elementType == ElementType.TYPE_USE) {
            return false;
        }
    }
    return true;
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) TypeElement(javax.lang.model.element.TypeElement)

Example 18 with Target

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

the class AnnotatedTypeFactory method checkSupportedQuals.

/**
 * @throws BugInCF If supportedQuals is empty or if any of the support qualifiers has a @Target
 *     meta-annotation that contain something besides TYPE_USE or TYPE_PARAMETER. (@Target({}) is
 *     allowed.)
 */
private void checkSupportedQuals() {
    if (supportedQuals.isEmpty()) {
        throw new TypeSystemError("Found no supported qualifiers.");
    }
    for (Class<? extends Annotation> annotationClass : supportedQuals) {
        // Check @Target values
        ElementType[] targetValues = annotationClass.getAnnotation(Target.class).value();
        List<ElementType> badTargetValues = new ArrayList<>();
        for (ElementType element : targetValues) {
            if (!(element == ElementType.TYPE_USE || element == ElementType.TYPE_PARAMETER)) {
                // if there's an ElementType with an enumerated value of something other
                // than TYPE_USE or TYPE_PARAMETER then it isn't a valid qualifier
                badTargetValues.add(element);
            }
        }
        if (!badTargetValues.isEmpty()) {
            String msg = "The @Target meta-annotation on type qualifier " + annotationClass.toString() + " must not contain " + StringsPlume.conjunction("or", badTargetValues) + ".";
            throw new TypeSystemError(msg);
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) Target(java.lang.annotation.Target) ArrayList(java.util.ArrayList) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 19 with Target

use of java.lang.annotation.Target in project kie-wb-common by kiegroup.

the class DriverUtils method copyAnnotationTarget.

public static void copyAnnotationTarget(Class annotationClass, AnnotationDefinition annotationDefinition) {
    if (annotationClass.isAnnotationPresent(Target.class)) {
        Target targetAnnotation = (Target) annotationClass.getAnnotation(Target.class);
        java.lang.annotation.ElementType[] targets = targetAnnotation.value();
        if (targets != null && targets.length > 0) {
            for (int i = 0; i < targets.length; i++) {
                annotationDefinition.addTarget(buildElementType(targets[i]));
            }
        } else {
            // added to avoid an errai unmarshalling error in broser side, when an annotation has no targets, e.g.
            // javax.persistence.UniqueConstraint
            annotationDefinition.addTarget(ElementType.UNDEFINED);
        }
    }
}
Also used : Target(java.lang.annotation.Target) ElementType(org.kie.workbench.common.services.datamodeller.core.ElementType)

Example 20 with Target

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

the class MetaAnnotatedElement 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)

Aggregations

Target (java.lang.annotation.Target)24 ElementType (java.lang.annotation.ElementType)15 Retention (java.lang.annotation.Retention)5 RetentionPolicy (java.lang.annotation.RetentionPolicy)4 AnnotationMirror (javax.lang.model.element.AnnotationMirror)4 ClassName (com.squareup.javapoet.ClassName)3 Element (javax.lang.model.element.Element)3 TypeKind (javax.lang.model.type.TypeKind)3 Test (org.junit.Test)3 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)2 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 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 Method (java.lang.reflect.Method)2 VariableElement (javax.lang.model.element.VariableElement)2