Search in sources :

Example 1 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class AbstractASTTransformation method copyAnnotatedNodeAnnotations.

/**
     * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
     * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
     * <p>
     * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported for now.
     */
protected List<AnnotationNode> copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, String myTypeName) {
    final List<AnnotationNode> copiedAnnotations = new ArrayList<AnnotationNode>();
    final List<AnnotationNode> notCopied = new ArrayList<AnnotationNode>();
    GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied);
    for (AnnotationNode annotation : notCopied) {
        addError(myTypeName + " does not support keeping Closure annotation members.", annotation);
    }
    return copiedAnnotations;
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList)

Example 2 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class AntlrParserPlugin method packageDef.

// Top level control structures
//-------------------------------------------------------------------------
protected void packageDef(AST packageDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = packageDef.getFirstChild();
    if (isType(ANNOTATIONS, node)) {
        processAnnotations(annotations, node);
        node = node.getNextSibling();
    }
    String name = qualifiedName(node);
    // TODO should we check package node doesn't already exist? conflict?
    PackageNode packageNode = setPackage(name, annotations);
    configureAST(packageNode, packageDef);
}
Also used : AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 3 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class AntlrParserPlugin method forStatement.

protected Statement forStatement(AST forNode) {
    AST inNode = forNode.getFirstChild();
    Expression collectionExpression;
    Parameter forParameter;
    if (isType(CLOSURE_LIST, inNode)) {
        forStatementBeingDef = true;
        ClosureListExpression clist = closureListExpression(inNode);
        forStatementBeingDef = false;
        int size = clist.getExpressions().size();
        if (size != 3) {
            throw new ASTRuntimeException(inNode, "3 expressions are required for the classic for loop, you gave " + size);
        }
        collectionExpression = clist;
        forParameter = ForStatement.FOR_LOOP_DUMMY;
    } else {
        AST variableNode = inNode.getFirstChild();
        AST collectionNode = variableNode.getNextSibling();
        ClassNode type = ClassHelper.OBJECT_TYPE;
        if (isType(VARIABLE_DEF, variableNode)) {
            AST node = variableNode.getFirstChild();
            // skip the final modifier if it's present
            if (isType(MODIFIERS, node)) {
                int modifiersMask = modifiers(node, new ArrayList<AnnotationNode>(), 0);
                // only final modifier allowed
                if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
                    throw new ASTRuntimeException(node, "Only the 'final' modifier is allowed in front of the for loop variable.");
                }
                node = node.getNextSibling();
            }
            type = makeTypeWithArguments(node);
            variableNode = node.getNextSibling();
        }
        String variable = identifier(variableNode);
        collectionExpression = expression(collectionNode);
        forParameter = new Parameter(type, variable);
        configureAST(forParameter, variableNode);
    }
    final AST node = inNode.getNextSibling();
    Statement block;
    if (isType(SEMI, node)) {
        block = EmptyStatement.INSTANCE;
    } else {
        block = statement(node);
    }
    ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
    configureAST(forStatement, forNode);
    return forStatement;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) Parameter(org.codehaus.groovy.ast.Parameter) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 4 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class AntlrParserPlugin method fieldDef.

protected void fieldDef(AST fieldDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = fieldDef.getFirstChild();
    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        node = node.getNextSibling();
    }
    if (classNode.isInterface()) {
        modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
        if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
            modifiers |= Opcodes.ACC_PUBLIC;
        }
    }
    ClassNode type = null;
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    String name = identifier(node);
    node = node.getNextSibling();
    Expression initialValue = null;
    if (node != null) {
        assertNodeType(ASSIGN, node);
        initialValue = expression(node.getFirstChild());
    }
    if (classNode.isInterface() && initialValue == null && type != null) {
        initialValue = getDefaultValueForPrimitive(type);
    }
    FieldNode fieldNode = new FieldNode(name, modifiers, type, classNode, initialValue);
    fieldNode.addAnnotations(annotations);
    configureAST(fieldNode, fieldDef);
    if (!hasVisibility(modifiers)) {
        // let's set the modifiers on the field
        int fieldModifiers = 0;
        int flags = Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL;
        if (!hasVisibility(modifiers)) {
            modifiers |= Opcodes.ACC_PUBLIC;
            fieldModifiers |= Opcodes.ACC_PRIVATE;
        }
        // let's pass along any other modifiers we need
        fieldModifiers |= (modifiers & flags);
        fieldNode.setModifiers(fieldModifiers);
        fieldNode.setSynthetic(true);
        // in the case that there is already a field, we would
        // like to use that field, instead of the default field
        // for the property
        FieldNode storedNode = classNode.getDeclaredField(fieldNode.getName());
        if (storedNode != null && !classNode.hasProperty(name)) {
            fieldNode = storedNode;
            // we remove it here, because addProperty will add it
            // again and we want to avoid it showing up multiple
            // times in the fields list.
            classNode.getFields().remove(storedNode);
        }
        PropertyNode propertyNode = new PropertyNode(fieldNode, modifiers, null, null);
        configureAST(propertyNode, fieldDef);
        classNode.addProperty(propertyNode);
    } else {
        fieldNode.setModifiers(modifiers);
        // if there is a property of that name, then a field of that
        // name already exists, which means this new field here should
        // be used instead of the field the property originally has.
        PropertyNode pn = classNode.getProperty(name);
        if (pn != null && pn.getField().isSynthetic()) {
            classNode.getFields().remove(pn.getField());
            pn.setField(fieldNode);
        }
        classNode.addField(fieldNode);
    }
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 5 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class AntlrParserPlugin method enumConstantDef.

protected void enumConstantDef(AST node) {
    enumConstantBeingDef = true;
    assertNodeType(ENUM_CONSTANT_DEF, node);
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST element = node.getFirstChild();
    if (isType(ANNOTATIONS, element)) {
        processAnnotations(annotations, element);
        element = element.getNextSibling();
    }
    String identifier = identifier(element);
    Expression init = null;
    element = element.getNextSibling();
    if (element != null) {
        init = expression(element);
        ClassNode innerClass;
        if (element.getNextSibling() == null) {
            innerClass = getAnonymousInnerClassNode(init);
            if (innerClass != null) {
                init = null;
            }
        } else {
            element = element.getNextSibling();
            Expression next = expression(element);
            innerClass = getAnonymousInnerClassNode(next);
        }
        if (innerClass != null) {
            // we have to handle an enum constant with a class overriding
            // a method in which case we need to configure the inner class
            innerClass.setSuperClass(classNode.getPlainNodeReference());
            innerClass.setModifiers(classNode.getModifiers() | Opcodes.ACC_FINAL);
            // we use a ClassExpression for transportation to EnumVisitor
            Expression inner = new ClassExpression(innerClass);
            if (init == null) {
                ListExpression le = new ListExpression();
                le.addExpression(inner);
                init = le;
            } else {
                if (init instanceof ListExpression) {
                    ((ListExpression) init).addExpression(inner);
                } else {
                    ListExpression le = new ListExpression();
                    le.addExpression(init);
                    le.addExpression(inner);
                    init = le;
                }
            }
            // and remove the final modifier from classNode to allow the sub class
            classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_FINAL);
        } else if (isType(ELIST, element)) {
            if (init instanceof ListExpression && !((ListExpression) init).isWrapped()) {
                ListExpression le = new ListExpression();
                le.addExpression(init);
                init = le;
            }
        }
    }
    FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
    enumField.addAnnotations(annotations);
    configureAST(enumField, node);
    enumConstantBeingDef = false;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)248 ClassNode (org.codehaus.groovy.ast.ClassNode)158 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)78 Expression (org.codehaus.groovy.ast.expr.Expression)68 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)64 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)60 MethodNode (org.codehaus.groovy.ast.MethodNode)57 FieldNode (org.codehaus.groovy.ast.FieldNode)54 ArrayList (java.util.ArrayList)53 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)49 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)48 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)44 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)30 Parameter (org.codehaus.groovy.ast.Parameter)29 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)29 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)29 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)28