Search in sources :

Example 36 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.

the class SynchronizedASTTransformation 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;
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        if (mNode.isAbstract()) {
            addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
            return;
        }
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode);
        if (lockExpr == null)
            return;
        Statement origCode = mNode.getCode();
        Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
        mNode.setCode(newCode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 37 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.

the class CompileDynamicProcessor method visit.

public List<AnnotationNode> visit(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, AnnotatedNode aliasAnnotated, SourceUnit source) {
    AnnotationNode node = new AnnotationNode(COMPILESTATIC_NODE);
    node.addMember("value", new PropertyExpression(new ClassExpression(TYPECHECKINGMODE_NODE), "SKIP"));
    return Collections.singletonList(node);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 38 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.

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 = getMemberList(anno, "excludes");
        List<String> includes = getMemberList(anno, "includes");
        if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) {
            AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0);
            if (excludes == null || excludes.isEmpty())
                excludes = getMemberList(canonical, "excludes");
            if (includes == null || includes.isEmpty())
                includes = getMemberList(canonical, "includes");
        }
        if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
            return;
        createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes);
        createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 39 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.

the class InheritConstructorsASTTransformation 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 ClassNode) {
        processClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 40 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.

the class InheritConstructorsASTTransformation method processClass.

private void processClass(ClassNode cNode, AnnotationNode node) {
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cNode.getName() + "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
        return;
    }
    boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true);
    boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true);
    ClassNode sNode = cNode.getSuperClass();
    List<AnnotationNode> superAnnotations = sNode.getAnnotations(MY_TYPE);
    if (superAnnotations.size() == 1) {
        // We need @InheritConstructors from parent classes processed first
        // so force that order here. The transformation is benign on an already
        // processed node so processing twice in any order won't matter bar
        // a very small time penalty.
        processClass(sNode, node);
    }
    for (ConstructorNode cn : sNode.getDeclaredConstructors()) {
        addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

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