Search in sources :

Example 6 with AnnotationNode

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

the class AntlrParserPlugin method annotationDef.

protected void annotationDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
    String name = identifier(node);
    node = node.getNextSibling();
    ClassNode superClass = ClassHelper.OBJECT_TYPE;
    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }
    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(EXTENDS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.addInterface(ClassHelper.Annotation_TYPE);
    configureAST(classNode, classDef);
    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);
    classNode = null;
}
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) ArrayList(java.util.ArrayList) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 7 with AnnotationNode

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

the class AntlrParserPlugin method annotation.

protected AnnotationNode annotation(AST annotationNode) {
    annotationBeingDef = true;
    AST node = annotationNode.getFirstChild();
    String name = qualifiedName(node);
    AnnotationNode annotatedNode = new AnnotationNode(ClassHelper.make(name));
    configureAST(annotatedNode, annotationNode);
    while (true) {
        node = node.getNextSibling();
        if (isType(ANNOTATION_MEMBER_VALUE_PAIR, node)) {
            AST memberNode = node.getFirstChild();
            String param = identifier(memberNode);
            Expression expression = expression(memberNode.getNextSibling());
            if (annotatedNode.getMember(param) != null) {
                throw new ASTRuntimeException(memberNode, "Annotation member '" + param + "' has already been associated with a value");
            }
            annotatedNode.setMember(param, expression);
        } else {
            break;
        }
    }
    annotationBeingDef = false;
    return annotatedNode;
}
Also used : AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 8 with AnnotationNode

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

the class AntlrParserPlugin method declarationExpression.

protected Expression declarationExpression(AST variableDef) {
    AST node = variableDef.getFirstChild();
    ClassNode type = null;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        // force check of modifier conflicts
        modifiers = modifiers(node, annotations, 0);
        node = node.getNextSibling();
    }
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    Expression leftExpression;
    Expression rightExpression = EmptyExpression.INSTANCE;
    AST right;
    if (isType(ASSIGN, node)) {
        node = node.getFirstChild();
        AST left = node.getFirstChild();
        ArgumentListExpression alist = new ArgumentListExpression();
        for (AST varDef = left; varDef != null; varDef = varDef.getNextSibling()) {
            assertNodeType(VARIABLE_DEF, varDef);
            DeclarationExpression de = (DeclarationExpression) declarationExpression(varDef);
            alist.addExpression(de.getVariableExpression());
        }
        leftExpression = alist;
        right = node.getNextSibling();
        if (right != null)
            rightExpression = expression(right);
    } else {
        String name = identifier(node);
        VariableExpression ve = new VariableExpression(name, type);
        ve.setModifiers(modifiers);
        leftExpression = ve;
        right = node.getNextSibling();
        if (right != null) {
            assertNodeType(ASSIGN, right);
            rightExpression = expression(right.getFirstChild());
        }
    }
    configureAST(leftExpression, node);
    Token token = makeToken(Types.ASSIGN, variableDef);
    DeclarationExpression expression = new DeclarationExpression(leftExpression, token, rightExpression);
    expression.addAnnotations(annotations);
    configureAST(expression, variableDef);
    ExpressionStatement expressionStatement = new ExpressionStatement(expression);
    configureAST(expressionStatement, variableDef);
    return expression;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) ArrayList(java.util.ArrayList) Token(org.codehaus.groovy.syntax.Token) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 9 with AnnotationNode

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

the class AntlrParserPlugin method methodDef.

protected void methodDef(AST methodDef) {
    MethodNode oldNode = methodNode;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = methodDef.getFirstChild();
    GenericsType[] generics = null;
    if (isType(TYPE_PARAMETERS, node)) {
        generics = makeGenericsType(node);
        node = node.getNextSibling();
    }
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
        node = node.getNextSibling();
    }
    if (isAnInterface()) {
        modifiers |= Opcodes.ACC_ABSTRACT;
    }
    ClassNode returnType = null;
    if (isType(TYPE, node)) {
        returnType = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    String name = identifier(node);
    if (classNode != null && !classNode.isAnnotationDefinition()) {
        if (classNode.getNameWithoutPackage().equals(name)) {
            if (isAnInterface()) {
                throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
            }
            throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
        }
    }
    node = node.getNextSibling();
    Parameter[] parameters = Parameter.EMPTY_ARRAY;
    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    if (classNode == null || !classNode.isAnnotationDefinition()) {
        assertNodeType(PARAMETERS, node);
        parameters = parameters(node);
        if (parameters == null)
            parameters = Parameter.EMPTY_ARRAY;
        node = node.getNextSibling();
        if (isType(LITERAL_throws, node)) {
            AST throwsNode = node.getFirstChild();
            List<ClassNode> exceptionList = new ArrayList<ClassNode>();
            throwsList(throwsNode, exceptionList);
            exceptions = exceptionList.toArray(exceptions);
            node = node.getNextSibling();
        }
    }
    boolean hasAnnotationDefault = false;
    Statement code = null;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
    if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
        if (node == null) {
            throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
        }
        assertNodeType(SLIST, node);
        code = statementList(node);
    } else if (node != null && classNode.isAnnotationDefinition()) {
        code = statement(node);
        hasAnnotationDefault = true;
    } else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
        if (node != null) {
            throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
        }
    }
    methodNode.setCode(code);
    methodNode.addAnnotations(annotations);
    methodNode.setGenericsTypes(generics);
    methodNode.setAnnotationDefault(hasAnnotationDefault);
    methodNode.setSyntheticPublic(syntheticPublic);
    configureAST(methodNode, methodDef);
    if (classNode != null) {
        classNode.addMethod(methodNode);
    } else {
        output.addMethod(methodNode);
    }
    methodNode = oldNode;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) 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) ArrayList(java.util.ArrayList) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 10 with AnnotationNode

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

the class GeneralUtils 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 at present.
     */
public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied) {
    List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
    for (AnnotationNode annotation : annotationList) {
        List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
        if (annotations.isEmpty())
            continue;
        if (hasClosureMember(annotation)) {
            notCopied.add(annotation);
            continue;
        }
        AnnotationNode retentionPolicyAnnotation = annotations.get(0);
        Expression valueExpression = retentionPolicyAnnotation.getMember("value");
        if (!(valueExpression instanceof PropertyExpression))
            continue;
        PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
        boolean processAnnotation = propertyExpression.getProperty() instanceof ConstantExpression && ("RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()) || "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()));
        if (processAnnotation) {
            AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
            for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) {
                newAnnotation.addMember(member.getKey(), member.getValue());
            }
            newAnnotation.setSourcePosition(annotatedNode);
            copied.add(newAnnotation);
        }
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) Map(java.util.Map)

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