Search in sources :

Example 11 with Target

use of java.lang.annotation.Target 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)

Example 12 with Target

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

the class Java5 method configureAnnotation.

private void configureAnnotation(AnnotationNode node, Annotation annotation) {
    Class type = annotation.annotationType();
    if (type == Retention.class) {
        Retention r = (Retention) annotation;
        RetentionPolicy value = r.value();
        setRetentionPolicy(value, node);
        node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
    } else if (type == Target.class) {
        Target t = (Target) annotation;
        ElementType[] elements = t.value();
        ListExpression elementExprs = new ListExpression();
        for (ElementType element : elements) {
            elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
        }
        node.setMember("value", elementExprs);
    } else {
        Method[] declaredMethods;
        try {
            declaredMethods = type.getDeclaredMethods();
        } catch (SecurityException se) {
            declaredMethods = new Method[0];
        }
        for (Method declaredMethod : declaredMethods) {
            try {
                Object value = declaredMethod.invoke(annotation);
                Expression valueExpression = annotationValueToExpression(value);
                if (valueExpression == null)
                    continue;
                node.setMember(declaredMethod.getName(), valueExpression);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Target(java.lang.annotation.Target) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 13 with Target

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

the class StubParser method annotateDecl.

private void annotateDecl(Map<String, Set<AnnotationMirror>> declAnnos, Element elt, List<AnnotationExpr> annotations) {
    if (annotations == null) {
        return;
    }
    Set<AnnotationMirror> annos = AnnotationUtils.createAnnotationSet();
    for (AnnotationExpr annotation : annotations) {
        AnnotationMirror annoMirror = getAnnotation(annotation, allStubAnnotations);
        if (annoMirror != null) {
            Target target = annoMirror.getAnnotationType().asElement().getAnnotation(Target.class);
            // Only add the declaration annotation if the annotation applies to the element.
            if (AnnotationUtils.getElementKindsForTarget(target).contains(elt.getKind())) {
                annos.add(annoMirror);
            }
        }
    }
    String key = ElementUtils.getVerboseName(elt);
    putOrAddToMap(declAnnos, key, annos);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Target(java.lang.annotation.Target) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 14 with Target

use of java.lang.annotation.Target in project soot by Sable.

the class AnnotationGenerator method annotate.

/**
 * Applies a Java 1.5-style annotation to a given Host. The Host must be of type {@link SootClass}, {@link SootMethod}
 * or {@link SootField}.
 *
 * @param h a method, field, or class
 * @param klass the class of the annotation to apply to <code>h</code>
 * @param elems a (possibly empty) sequence of AnnotationElem objects corresponding to the elements that should be contained in this annotation
 */
public void annotate(Host h, Class<? extends Annotation> klass, List<AnnotationElem> elems) {
    // error-checking -- is this annotation appropriate for the target Host?
    Target t = klass.getAnnotation(Target.class);
    Collection<ElementType> elementTypes = Arrays.asList(t.value());
    final String ERR = "Annotation class " + klass + " not applicable to host of type " + h.getClass() + ".";
    if (h instanceof SootClass) {
        if (!elementTypes.contains(ElementType.TYPE)) {
            throw new RuntimeException(ERR);
        }
    } else if (h instanceof SootMethod) {
        if (!elementTypes.contains(ElementType.METHOD)) {
            throw new RuntimeException(ERR);
        }
    } else if (h instanceof SootField) {
        if (!elementTypes.contains(ElementType.FIELD)) {
            throw new RuntimeException(ERR);
        }
    } else {
        throw new RuntimeException("Tried to attach annotation to host of type " + h.getClass() + ".");
    }
    // get the retention type of the class
    Retention r = klass.getAnnotation(Retention.class);
    // CLASS (runtime invisible) retention is the default
    int retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
    if (r != null) {
        // RetentionPolicy directly? (Eric Bodden 20/05/2008)
        switch(r.value()) {
            case CLASS:
                retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
                break;
            case RUNTIME:
                retPolicy = AnnotationConstants.RUNTIME_VISIBLE;
                break;
            default:
                throw new RuntimeException("Unexpected retention policy: " + retPolicy);
        }
    }
    annotate(h, klass.getCanonicalName(), retPolicy, elems);
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) SootMethod(soot.SootMethod) SootField(soot.SootField) SootClass(soot.SootClass) Retention(java.lang.annotation.Retention)

Example 15 with Target

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

the class WholeProgramInferenceScenesStorage 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.util.defaults.QualifierDefaults}. Look into the
 * createQualifierDefaults method in {@link GenericAnnotatedTypeFactory} (which uses the
 * QualifierDefaults class linked above) before changing anything here. See
 * https://github.com/typetools/checker-framework/issues/683 .
 *
 * @param am an annotation to test for whether it should be inserted into source code
 * @param location where the location would be inserted; used to determine if {@code am} is the
 *     default for that location
 * @param atm its kind is used to determine if {@code am} is the default for that kind
 * @return true if am should not be inserted into source code, or if am is invisible
 */
private boolean shouldIgnore(AnnotationMirror am, TypeUseLocation location, 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 a default annotation.
    // This case checks if it is meta-annotated with @DefaultFor.
    // TODO: Handle cases of annotations added via an
    // org.checkerframework.framework.type.treeannotator.LiteralTreeAnnotator.
    DefaultFor defaultFor = elt.getAnnotation(DefaultFor.class);
    if (defaultFor != null) {
        org.checkerframework.framework.qual.TypeKind[] types = defaultFor.typeKinds();
        TypeKind atmKind = atm.getUnderlyingType().getKind();
        if (hasMatchingTypeKind(atmKind, types)) {
            return true;
        }
    }
    return false;
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Target(java.lang.annotation.Target) DefaultQualifierInHierarchy(org.checkerframework.framework.qual.DefaultQualifierInHierarchy) ATypeElement(scenelib.annotations.el.ATypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) InvisibleQualifier(org.checkerframework.framework.qual.InvisibleQualifier) DefaultFor(org.checkerframework.framework.qual.DefaultFor) DefaultQualifier(org.checkerframework.framework.qual.DefaultQualifier)

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