Search in sources :

Example 6 with Retention

use of java.lang.annotation.Retention in project guice by google.

the class Matchers method checkForRuntimeRetention.

private static void checkForRuntimeRetention(Class<? extends Annotation> annotationType) {
    Retention retention = annotationType.getAnnotation(Retention.class);
    checkArgument(retention != null && retention.value() == RetentionPolicy.RUNTIME, "Annotation %s is missing RUNTIME retention", annotationType.getSimpleName());
}
Also used : Retention(java.lang.annotation.Retention)

Example 7 with Retention

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

the class ElementPredicates method effectiveRetentionPolicy.

private static RetentionPolicy effectiveRetentionPolicy(Element element) {
    RetentionPolicy retentionPolicy = RetentionPolicy.CLASS;
    Retention retentionAnnotation = element.getAnnotation(Retention.class);
    if (retentionAnnotation != null) {
        retentionPolicy = retentionAnnotation.value();
    }
    return retentionPolicy;
}
Also used : Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Example 8 with Retention

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

the class ResolveVisitor method visitAnnotations.

public void visitAnnotations(AnnotatedNode node) {
    List<AnnotationNode> annotations = node.getAnnotations();
    if (annotations.isEmpty())
        return;
    Map<String, AnnotationNode> tmpAnnotations = new HashMap<String, AnnotationNode>();
    ClassNode annType;
    for (AnnotationNode an : annotations) {
        // skip built-in properties
        if (an.isBuiltIn())
            continue;
        annType = an.getClassNode();
        resolveOrFail(annType, ",  unable to find class for annotation", an);
        for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
            Expression newValue = transform(member.getValue());
            newValue = transformInlineConstants(newValue);
            member.setValue(newValue);
            checkAnnotationMemberValue(newValue);
        }
        if (annType.isResolved()) {
            Class annTypeClass = annType.getTypeClass();
            Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
            if (retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME)) {
                AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(), an);
                if (anyPrevAnnNode != null) {
                    addError("Cannot specify duplicate annotation on the same member : " + annType.getName(), an);
                }
            }
        }
    }
}
Also used : Retention(java.lang.annotation.Retention)

Example 9 with Retention

use of java.lang.annotation.Retention in project libgdx by libgdx.

the class ReflectionCacheSourceCreator method getAnnotations.

private String getAnnotations(Annotation[] annotations) {
    if (annotations != null && annotations.length > 0) {
        int numValidAnnotations = 0;
        final Class<?>[] ignoredAnnotations = { Deprecated.class, Retention.class };
        StringBuilder b = new StringBuilder();
        b.append("new java.lang.annotation.Annotation[] {");
        for (Annotation annotation : annotations) {
            Class<?> type = annotation.annotationType();
            // skip ignored types, assuming we are not interested in those at runtime
            boolean ignoredType = false;
            for (int i = 0; !ignoredType && i < ignoredAnnotations.length; i++) {
                ignoredType = ignoredAnnotations[i].equals(type);
            }
            if (ignoredType) {
                continue;
            }
            // skip if not annotated with RetentionPolicy.RUNTIME
            Retention retention = type.getAnnotation(Retention.class);
            if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
                continue;
            }
            numValidAnnotations++;
            // anonymous class
            b.append(" new ").append(type.getCanonicalName()).append("() {");
            // override all methods
            Method[] methods = type.getDeclaredMethods();
            for (Method method : methods) {
                Class<?> returnType = method.getReturnType();
                b.append(" @Override public");
                b.append(" ").append(returnType.getCanonicalName());
                b.append(" ").append(method.getName()).append("() { return");
                if (returnType.isArray()) {
                    b.append(" new ").append(returnType.getCanonicalName()).append(" {");
                }
                // invoke the annotation method
                Object invokeResult = null;
                try {
                    invokeResult = method.invoke(annotation);
                } catch (IllegalAccessException e) {
                    logger.log(Type.ERROR, "Error invoking annotation method.");
                } catch (InvocationTargetException e) {
                    logger.log(Type.ERROR, "Error invoking annotation method.");
                }
                // write result as return value
                if (invokeResult != null) {
                    if (returnType.equals(String[].class)) {
                        // String[]
                        for (String s : (String[]) invokeResult) {
                            b.append(" \"").append(s).append("\",");
                        }
                    } else if (returnType.equals(String.class)) {
                        // String
                        b.append(" \"").append((String) invokeResult).append("\"");
                    } else if (returnType.equals(Class[].class)) {
                        // Class[]
                        for (Class c : (Class[]) invokeResult) {
                            b.append(" ").append(c.getCanonicalName()).append(".class,");
                        }
                    } else if (returnType.equals(Class.class)) {
                        // Class
                        b.append(" ").append(((Class) invokeResult).getCanonicalName()).append(".class");
                    } else if (returnType.isArray() && returnType.getComponentType().isEnum()) {
                        // enum[]
                        String enumTypeName = returnType.getComponentType().getCanonicalName();
                        int length = Array.getLength(invokeResult);
                        for (int i = 0; i < length; i++) {
                            Object e = Array.get(invokeResult, i);
                            b.append(" ").append(enumTypeName).append(".").append(e.toString()).append(",");
                        }
                    } else if (returnType.isEnum()) {
                        // enum
                        b.append(" ").append(returnType.getCanonicalName()).append(".").append(invokeResult.toString());
                    } else if (returnType.isArray() && returnType.getComponentType().isPrimitive()) {
                        // primitive []
                        Class<?> primitiveType = returnType.getComponentType();
                        int length = Array.getLength(invokeResult);
                        for (int i = 0; i < length; i++) {
                            Object n = Array.get(invokeResult, i);
                            b.append(" ").append(n.toString());
                            if (primitiveType.equals(float.class)) {
                                b.append("f");
                            }
                            b.append(",");
                        }
                    } else if (returnType.isPrimitive()) {
                        // primitive
                        b.append(" ").append(invokeResult.toString());
                        if (returnType.equals(float.class)) {
                            b.append("f");
                        }
                    } else {
                        logger.log(Type.ERROR, "Return type not supported (or not yet implemented).");
                    }
                }
                if (returnType.isArray()) {
                    b.append(" }");
                }
                b.append("; ");
                b.append("}");
            }
            // must override annotationType()
            b.append(" @Override public Class<? extends java.lang.annotation.Annotation> annotationType() { return ");
            b.append(type.getCanonicalName());
            b.append(".class; }");
            b.append("}, ");
        }
        b.append("}");
        return (numValidAnnotations > 0) ? b.toString() : "null";
    }
    return "null";
}
Also used : Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with Retention

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

the class ResolveVisitor method visitAnnotations.

public void visitAnnotations(AnnotatedNode node) {
    List<AnnotationNode> annotations = node.getAnnotations();
    if (annotations.isEmpty())
        return;
    Map<String, AnnotationNode> tmpAnnotations = new HashMap<String, AnnotationNode>();
    ClassNode annType;
    for (AnnotationNode an : annotations) {
        // skip built-in properties
        if (an.isBuiltIn())
            continue;
        annType = an.getClassNode();
        resolveOrFail(annType, ",  unable to find class for annotation", an);
        for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
            Expression newValue = transform(member.getValue());
            newValue = transformInlineConstants(newValue);
            member.setValue(newValue);
            checkAnnotationMemberValue(newValue);
        }
        if (annType.isResolved()) {
            Class annTypeClass = annType.getTypeClass();
            Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
            if (retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME)) {
                AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(), an);
                if (anyPrevAnnNode != null) {
                    addError("Cannot specify duplicate annotation on the same member : " + annType.getName(), an);
                }
            }
        }
    }
}
Also used : Retention(java.lang.annotation.Retention)

Aggregations

Retention (java.lang.annotation.Retention)11 Method (java.lang.reflect.Method)4 RetentionPolicy (java.lang.annotation.RetentionPolicy)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ElementType (java.lang.annotation.ElementType)2 Target (java.lang.annotation.Target)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 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 Annotation (java.lang.annotation.Annotation)1 Inherited (java.lang.annotation.Inherited)1