Search in sources :

Example 16 with ElementType

use of java.lang.annotation.ElementType 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 17 with ElementType

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

Example 18 with ElementType

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

the class Java8 method configureAnnotationNodeFromDefinition.

@Override
public void configureAnnotationNodeFromDefinition(final AnnotationNode definition, final AnnotationNode root) {
    ClassNode type = definition.getClassNode();
    final String typeName = type.getName();
    if ("java.lang.annotation.Retention".equals(typeName)) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof PropertyExpression))
            return;
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        RetentionPolicy policy = RetentionPolicy.valueOf(name);
        setRetentionPolicy(policy, root);
    } else if ("java.lang.annotation.Target".equals(typeName)) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof ListExpression))
            return;
        ListExpression le = (ListExpression) exp;
        int bitmap = 0;
        for (Expression e : le.getExpressions()) {
            if (!(e instanceof PropertyExpression))
                return;
            PropertyExpression element = (PropertyExpression) e;
            String name = element.getPropertyAsString();
            ElementType value = ElementType.valueOf(name);
            bitmap |= getElementCode(value);
        }
        root.setAllowedTargets(bitmap);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Example 19 with ElementType

use of java.lang.annotation.ElementType in project xtext-xtend by eclipse.

the class XtendValidator method checkAnnotationTarget.

@Check
public void checkAnnotationTarget(XAnnotation annotation) {
    JvmType annotationType = annotation.getAnnotationType();
    if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType)) {
        return;
    }
    Set<ElementType> targets = annotationUtil.getAnnotationTargets((JvmAnnotationType) annotationType);
    if (targets.isEmpty())
        return;
    final EObject eContainer = getContainingAnnotationTarget(annotation);
    Class<? extends EObject> clazz = eContainer.getClass();
    if (eContainer instanceof XtendField && eContainer.eContainer() instanceof XtendAnnotationType) {
        clazz = XtendFunction.class;
    }
    for (Entry<Class<?>, Collection<ElementType>> mapping : targetInfos.asMap().entrySet()) {
        if (mapping.getKey().isAssignableFrom(clazz)) {
            targets.retainAll(mapping.getValue());
            if (targets.isEmpty()) {
                error("The annotation @" + annotation.getAnnotationType().getSimpleName() + " is disallowed for this location.", annotation, null, INSIGNIFICANT_INDEX, ANNOTATION_WRONG_TARGET);
            }
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) ElementType(java.lang.annotation.ElementType) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmType(org.eclipse.xtext.common.types.JvmType) XtendField(org.eclipse.xtend.core.xtend.XtendField) Check(org.eclipse.xtext.validation.Check)

Example 20 with ElementType

use of java.lang.annotation.ElementType in project antlr4 by tunnelvisionlabs.

the class RuleDependencyChecker method getDependencies.

public static List<Tuple2<RuleDependency, AnnotatedElement>> getDependencies(Class<?> clazz) {
    List<Tuple2<RuleDependency, AnnotatedElement>> result = new ArrayList<Tuple2<RuleDependency, AnnotatedElement>>();
    List<ElementType> supportedTarget = Arrays.asList(RuleDependency.class.getAnnotation(Target.class).value());
    for (ElementType target : supportedTarget) {
        switch(target) {
            case TYPE:
                if (!clazz.isAnnotation()) {
                    getElementDependencies(clazz, result);
                }
                break;
            case ANNOTATION_TYPE:
                if (!clazz.isAnnotation()) {
                    getElementDependencies(clazz, result);
                }
                break;
            case CONSTRUCTOR:
                for (Constructor<?> ctor : clazz.getDeclaredConstructors()) {
                    getElementDependencies(ctor, result);
                }
                break;
            case FIELD:
                for (Field field : clazz.getDeclaredFields()) {
                    getElementDependencies(field, result);
                }
                break;
            case LOCAL_VARIABLE:
                System.err.println("Runtime rule dependency checking is not supported for local variables.");
                break;
            case METHOD:
                for (Method method : clazz.getDeclaredMethods()) {
                    getElementDependencies(method, result);
                }
                break;
            case PACKAGE:
                // package is not a subset of class, so nothing to do here
                break;
            case PARAMETER:
                System.err.println("Runtime rule dependency checking is not supported for parameters.");
                break;
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) ElementType(java.lang.annotation.ElementType) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) RuleDependency(org.antlr.v4.runtime.RuleDependency)

Aggregations

ElementType (java.lang.annotation.ElementType)27 Target (java.lang.annotation.Target)15 RetentionPolicy (java.lang.annotation.RetentionPolicy)7 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)6 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)6 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)6 Param (com.canoo.platform.remoting.client.Param)4 Retention (java.lang.annotation.Retention)4 Method (java.lang.reflect.Method)4 Test (org.testng.annotations.Test)4 ClassNode (org.codehaus.groovy.ast.ClassNode)3 ControllerUnderTest (com.canoo.platform.spring.test.ControllerUnderTest)2 SpringTestNGControllerTest (com.canoo.platform.spring.test.SpringTestNGControllerTest)2 ClassName (com.squareup.javapoet.ClassName)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Nullable (javax.annotation.Nullable)2