Search in sources :

Example 46 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 47 with AnnotationNode

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

the class AbstractInterruptibleASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        internalError("Expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    this.source = source;
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
    if (!type().equals(node.getClassNode())) {
        internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
    }
    setupTransform(node);
    // should be limited to the current SourceUnit or propagated to the whole CompilationUnit
    final ModuleNode tree = source.getAST();
    if (applyToAllClasses) {
        // guard every class and method defined in this script
        if (tree != null) {
            final List<ClassNode> classes = tree.getClasses();
            for (ClassNode classNode : classes) {
                visitClass(classNode);
            }
        }
    } else if (annotatedNode instanceof ClassNode) {
        // only guard this particular class
        this.visitClass((ClassNode) annotatedNode);
    } else if (!applyToAllMembers && annotatedNode instanceof MethodNode) {
        this.visitMethod((MethodNode) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else if (!applyToAllMembers && annotatedNode instanceof FieldNode) {
        this.visitField((FieldNode) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else if (!applyToAllMembers && annotatedNode instanceof DeclarationExpression) {
        this.visitDeclarationExpression((DeclarationExpression) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else {
        // only guard the script class
        if (tree != null) {
            final List<ClassNode> classes = tree.getClasses();
            for (ClassNode classNode : classes) {
                if (classNode.isScript()) {
                    visitClass(classNode);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 48 with AnnotationNode

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

the class AutoImplementASTTransformation 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;
        ClassNode exception = getMemberClassValue(anno, "exception");
        if (exception != null && Undefined.isUndefinedException(exception)) {
            exception = null;
        }
        String message = getMemberStringValue(anno, "message");
        Expression code = anno.getMember("code");
        if (code != null && !(code instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'code'. Found " + code, cNode);
            return;
        }
        createMethods(cNode, exception, message, (ClosureExpression) code);
        if (code != null) {
            anno.setMember("code", new ClosureExpression(new Parameter[0], new EmptyStatement()));
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 49 with AnnotationNode

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

the class BaseScriptASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 50 with AnnotationNode

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

the class BuilderASTTransformation 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 || parent instanceof MethodNode) {
        if (parent instanceof ClassNode && !checkNotInterface((ClassNode) parent, MY_TYPE_NAME))
            return;
        if (parent instanceof MethodNode && !checkStatic((MethodNode) parent, MY_TYPE_NAME))
            return;
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final BuilderStrategy strategy = createBuilderStrategy(anno, classLoader);
        if (strategy == null)
            return;
        strategy.build(this, parent, anno);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

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