Search in sources :

Example 21 with AnnotatedNode

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

the class TraitASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!Traits.TRAIT_CLASSNODE.equals(anno.getClassNode()))
        return;
    unit = source;
    init(nodes, source);
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, Traits.TRAIT_TYPE_NAME))
            return;
        checkNoConstructor(cNode);
        checkExtendsClause(cNode);
        generateMethodsWithDefaultArgs(cNode);
        replaceExtendsByImplements(cNode);
        createHelperClass(cNode);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 22 with AnnotatedNode

use of org.codehaus.groovy.ast.AnnotatedNode 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 23 with AnnotatedNode

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

the class ToStringASTTransformation 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 includeSuper = memberHasValue(anno, "includeSuper", true);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean cacheToString = memberHasValue(anno, "cache", true);
        if (includeSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) {
            addError("Error during " + MY_TYPE_NAME + " processing: includeSuper=true but '" + cNode.getName() + "' has no super class.", anno);
        }
        boolean includeNames = memberHasValue(anno, "includeNames", true);
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        List<String> excludes = getMemberList(anno, "excludes");
        List<String> includes = getMemberList(anno, "includes");
        boolean ignoreNulls = memberHasValue(anno, "ignoreNulls", true);
        boolean includePackage = !memberHasValue(anno, "includePackage", false);
        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;
        createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cacheToString, includeSuperProperties);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ToString(groovy.transform.ToString)

Example 24 with AnnotatedNode

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

the class TupleConstructorASTTransformation 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 includeFields = memberHasValue(anno, "includeFields", true);
        boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
        boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean callSuper = memberHasValue(anno, "callSuper", true);
        boolean force = memberHasValue(anno, "force", true);
        boolean useSetters = memberHasValue(anno, "useSetters", 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;
        // if @Immutable is found, let it pick up options and do work so we'll skip
        if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE))
            return;
        createConstructor(cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 25 with AnnotatedNode

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

the class MixinTransformation method visit.

public void visit(ASTNode[] astNodes, SourceUnit source) {
    if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode node = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
        return;
    }
    ClassNode classNode = (ClassNode) parent;
    String cName = classNode.getName();
    if (classNode.isInterface()) {
        throw new RuntimeException("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
    }
    ListExpression values = getListOfClasses(node);
    weaveMixinsIntoClass(classNode, values);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Aggregations

AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)67 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)67 ClassNode (org.codehaus.groovy.ast.ClassNode)59 FieldNode (org.codehaus.groovy.ast.FieldNode)31 MethodNode (org.codehaus.groovy.ast.MethodNode)18 Expression (org.codehaus.groovy.ast.expr.Expression)17 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)10 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)10 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)10 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)9 GroovyBugError (org.codehaus.groovy.GroovyBugError)8 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)7 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)7 VariableScopeVisitor (org.codehaus.groovy.classgen.VariableScopeVisitor)6 ArrayList (java.util.ArrayList)5 Statement (org.codehaus.groovy.ast.stmt.Statement)5 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)4 PropertyNode (org.codehaus.groovy.ast.PropertyNode)4