Search in sources :

Example 51 with AnnotationNode

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

the class EqualsAndHashCodeASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        boolean callSuper = memberHasValue(anno, "callSuper", true);
        boolean cacheHashCode = memberHasValue(anno, "cache", true);
        boolean useCanEqual = !memberHasValue(anno, "useCanEqual", false);
        if (callSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) {
            addError("Error during " + MY_TYPE_NAME + " processing: callSuper=true but '" + cNode.getName() + "' has no super class.", anno);
        }
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        final boolean allNames = memberHasValue(anno, "allNames", true);
        if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
            return;
        createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes, allNames);
        createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes, allNames);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 52 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 53 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 54 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 55 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)173 ClassNode (org.codehaus.groovy.ast.ClassNode)117 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)67 FieldNode (org.codehaus.groovy.ast.FieldNode)43 MethodNode (org.codehaus.groovy.ast.MethodNode)39 Expression (org.codehaus.groovy.ast.expr.Expression)37 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)36 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)31 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)29 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)29 ArrayList (java.util.ArrayList)26 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)22 Parameter (org.codehaus.groovy.ast.Parameter)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)16 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)16 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)16 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)15 AST (antlr.collections.AST)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)14 Statement (org.codehaus.groovy.ast.stmt.Statement)14