Search in sources :

Example 1 with ElementType

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

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

the class AnnotatedTypes method hasTypeQualifierElementTypes.

/**
 * Sees if the passed in array of {@link ElementType} values have the correct set of values
 * which defines a type qualifier
 *
 * @param elements an array of {@link ElementType} values
 * @param cls the annotation class being tested; used for diagnostic messages only
 * @throws RuntimeException if the array contains both {@link ElementType#TYPE_USE} and
 *     something besides {@link ElementType#TYPE_PARAMETER}
 */
public static boolean hasTypeQualifierElementTypes(ElementType[] elements, Class<?> cls) {
    boolean hasTypeUse = false;
    ElementType otherElementType = null;
    for (ElementType element : elements) {
        if (element.equals(ElementType.TYPE_USE)) {
            // valid annotations have to have TYPE_USE
            hasTypeUse = true;
        } else if (!element.equals(ElementType.TYPE_PARAMETER)) {
            // if there's an ElementType with a enumerated value of something other than
            // TYPE_USE or TYPE_PARAMETER then it isn't a valid annotation
            otherElementType = element;
        }
        if (hasTypeUse && otherElementType != null) {
            ErrorReporter.errorAbort("@Target meta-annotation should not contain both TYPE_USE and " + otherElementType + ", for annotation " + cls.getName());
        }
    }
    return hasTypeUse;
}
Also used : ElementType(java.lang.annotation.ElementType)

Example 3 with ElementType

use of java.lang.annotation.ElementType in project dolphin-platform by canoo.

the class ActionTestControllerTest method callPrivateMethodWithElementTypeParam.

@Test
public void callPrivateMethodWithElementTypeParam() {
    Assert.assertNull(controller.getModel().getEnumValue());
    final ElementType value = ElementType.METHOD;
    controller.invoke(PUBLIC_WITH_ELEMENT_TYPE_PARAM_ACTION, new Param(PARAM_NAME, value));
    Assert.assertEquals(controller.getModel().getEnumValue().compareTo(value), 0);
}
Also used : ElementType(java.lang.annotation.ElementType) Param(com.canoo.platform.remoting.client.Param) Test(org.testng.annotations.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SpringTestNGControllerTest(com.canoo.platform.spring.test.SpringTestNGControllerTest) ControllerUnderTest(com.canoo.platform.spring.test.ControllerUnderTest)

Example 4 with ElementType

use of java.lang.annotation.ElementType in project groovy-core by groovy.

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 5 with ElementType

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

the class Java8 method configureAnnotation.

protected void configureAnnotation(final AnnotationNode node, final 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 = EMPTY_METHOD_ARRAY;
        }
        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 | InvocationTargetException ignore) {
            }
        }
    }
}
Also used : Target(java.lang.annotation.Target) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) GroovyObject(groovy.lang.GroovyObject) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) MetaMethod(groovy.lang.MetaMethod) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy)

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