Java Code Examples for org.codehaus.groovy.ast.AnnotationNode#getMembers()

The following examples show how to use org.codehaus.groovy.ast.AnnotationNode#getMembers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VisibilityUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static Visibility getVisForAnnotation(Class<? extends AnnotatedNode> clazz, AnnotationNode visAnno, String visId) {
    Map<String, Expression> visMembers = visAnno.getMembers();
    if (visMembers == null) return Visibility.UNDEFINED;
    String id = getMemberStringValue(visAnno, "id", null);
    if ((id == null && visId != null) || (id != null && !id.equals(visId))) return Visibility.UNDEFINED;

    Visibility vis = null;
    if (clazz.equals(ConstructorNode.class)) {
        vis = getVisibility(visMembers.get("constructor"));
    } else if (clazz.equals(MethodNode.class)) {
        vis = getVisibility(visMembers.get("method"));
    } else if (clazz.equals(ClassNode.class)) {
        vis = getVisibility(visMembers.get("type"));
    }
    if (vis == null || vis == Visibility.UNDEFINED) {
        vis = getVisibility(visMembers.get("value"));
    }
    return vis;
}
 
Example 2
Source File: StaticTypesTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    Map<String,Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        visitor = newVisitor(source, methodNode.getDeclaringClass());
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type",
                node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
}
 
Example 3
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean hasClosureMember(final AnnotationNode annotation) {
    Map<String, Expression> members = annotation.getMembers();
    for (Map.Entry<String, Expression> member : members.entrySet())  {
        if (member.getValue() instanceof ClosureExpression) return true;

        if (member.getValue() instanceof ClassExpression)  {
            ClassExpression classExpression = (ClassExpression) member.getValue();
            Class<?> typeClass = classExpression.getType().isResolved() ? classExpression.getType().redirect().getTypeClass() : null;
            if (typeClass != null && GeneratedClosure.class.isAssignableFrom(typeClass)) return true;
        }
    }
    return false;
}
 
Example 4
Source File: AnnotationConstantExpression.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visit(GroovyCodeVisitor visitor) {
    AnnotationNode node = (AnnotationNode) getValue();
    Map<String, Expression> attrs = node.getMembers();
    for (Expression expr : attrs.values()) {
        expr.visit(visitor);
    }
    super.visit(visitor);
}
 
Example 5
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
public AnnotationNode visit(AnnotationNode node) {
    this.annotation = node;
    this.reportClass = node.getClassNode();

    if (!isValidAnnotationClass(node.getClassNode())) {
        addError("class " + node.getClassNode().getName() + " is not an annotation");
        return node;
    }

    // check if values have been passed for all annotation attributes that don't have defaults
    if (!checkIfMandatoryAnnotationValuesPassed(node)) {
        return node;
    }

    // if enum constants have been used, check if they are all valid
    if (!checkIfValidEnumConstsAreUsed(node)) {
        return node;
    }

    Map<String, Expression> attributes = node.getMembers();
    for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
        String attrName = entry.getKey();
        ClassNode attrType = getAttributeType(node, attrName);
        Expression attrExpr = transformInlineConstants(entry.getValue(), attrType);
        entry.setValue(attrExpr);
        visitExpression(attrName, attrExpr, attrType);
    }
    VMPluginFactory.getPlugin().configureAnnotation(node);
    return this.annotation;
}
 
Example 6
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean checkIfValidEnumConstsAreUsed(AnnotationNode node) {
    Map<String, Expression> attributes = node.getMembers();
    for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
        if (!validateEnumConstant(entry.getValue()))
            return false;
    }
    return true;
}
 
Example 7
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean checkIfMandatoryAnnotationValuesPassed(AnnotationNode node) {
    boolean ok = true;
    Map attributes = node.getMembers();
    ClassNode classNode = node.getClassNode();
    for (MethodNode mn : classNode.getMethods()) {
        String methodName = mn.getName();
        // if the annotation attribute has a default, getCode() returns a ReturnStatement with the default value
        if (mn.getCode() == null && !attributes.containsKey(methodName)) {
            addError("No explicit/default value found for annotation attribute '" + methodName + "'", node);
            ok = false;
        }
    }
    return ok;
}
 
Example 8
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void printAnnotation(PrintWriter out, AnnotationNode annotation) {
    out.print("@" + annotation.getClassNode().getName().replace('$', '.') + "(");
    boolean first = true;
    Map<String, Expression> members = annotation.getMembers();
    for (Map.Entry<String, Expression> entry : members.entrySet()) {
        String key = entry.getKey();
        if (first) first = false;
        else out.print(", ");
        out.print(key + "=" + getAnnotationValue(entry.getValue()).replace('$', '.'));
    }
    out.print(") ");
}
 
Example 9
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void copyMembers(final AnnotationNode from, final AnnotationNode to) {
    Map<String, Expression> members = from.getMembers();
    copyMembers(members, to);
}
 
Example 10
Source File: StaticCompileTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(final ASTNode[] nodes, final SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    Map<String,Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        classNode.putNodeMetaData(WriterControllerFactory.class, factory);
        node.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        ClassNode declaringClass = methodNode.getDeclaringClass();
        visitor = newVisitor(source, declaringClass);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        methodNode.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        if (declaringClass.getNodeMetaData(WriterControllerFactory.class) == null) {
            declaringClass.putNodeMetaData(WriterControllerFactory.class, factory);
        }
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type",
                node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
    StaticCompilationTransformer transformer = new StaticCompilationTransformer(source, visitor);
    if (node instanceof ClassNode) {
        transformer.visitClass((ClassNode) node);
    } else if (node instanceof MethodNode) {
        transformer.visitMethod((MethodNode) node);
    }
}