Search in sources :

Example 41 with AnnotatedNode

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

the class ArtefactTypeAstTransformation method visit.

public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode node = (AnnotationNode) astNodes[0];
    if (!(node instanceof AnnotationNode) || !(parent instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    if (!isArtefactAnnotationNode(node) || !(parent instanceof ClassNode)) {
        return;
    }
    ClassNode cNode = (ClassNode) parent;
    if (cNode.isInterface()) {
        throw new RuntimeException("Error processing interface '" + cNode.getName() + "'. @" + getAnnotationType().getNameWithoutPackage() + " not allowed for interfaces.");
    }
    if (isApplied(cNode)) {
        return;
    }
    String artefactType = resolveArtefactType(sourceUnit, node, cNode);
    if (artefactType != null) {
        AbstractGrailsArtefactTransformer.addToTransformedClasses(cNode.getName());
    }
    performInjectionOnArtefactType(sourceUnit, cNode, artefactType);
    performTraitInjectionOnArtefactType(sourceUnit, cNode, artefactType);
    postProcess(sourceUnit, node, cNode, artefactType);
    markApplied(cNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 42 with AnnotatedNode

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

the class TestMixinTransformation 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: " + astNodes[0].getClass() + " / " + astNodes[1].getClass());
    }
    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode annotationNode = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(annotationNode.getClassNode()) || !(parent instanceof ClassNode)) {
        return;
    }
    String mainClass = MainClassFinder.searchMainClass(source.getSource().getURI());
    ClassNode applicationClassNode = null;
    if (mainClass != null) {
        applicationClassNode = ClassHelper.make(mainClass);
    }
    ClassNode classNode = (ClassNode) parent;
    ListExpression values = getListOfClasses(annotationNode);
    weaveTestMixins(classNode, values, applicationClassNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 43 with AnnotatedNode

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

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)

Example 44 with AnnotatedNode

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

the class CanonicalASTTransformation 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;
        // TODO remove - let other validation steps pick this up
        if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE)) {
            addError(MY_TYPE_NAME + " class '" + cNode.getName() + "' can't also be " + ImmutableASTTransformation.MY_TYPE_NAME, parent);
        }
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        List<String> excludes = getMemberList(anno, "excludes");
        List<String> includes = getMemberList(anno, "includes");
        if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
            createConstructor(cNode, false, true, false, false, false, false, excludes, includes, false);
        }
        if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
            createHashCode(cNode, false, false, false, excludes, includes);
            createEquals(cNode, false, false, true, excludes, includes);
        }
        if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
            createToString(cNode, false, false, excludes, includes, false);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ToStringASTTransformation.createToString(org.codehaus.groovy.transform.ToStringASTTransformation.createToString)

Example 45 with AnnotatedNode

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

the class ReadWriteLockASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    final boolean isWriteLock;
    if (READ_LOCK_TYPE.equals(node.getClassNode())) {
        isWriteLock = false;
    } else if (WRITE_LOCK_TYPE.equals(node.getClassNode())) {
        isWriteLock = true;
    } else {
        throw new GroovyBugError("Internal error: expecting [" + READ_LOCK_TYPE.getName() + ", " + WRITE_LOCK_TYPE.getName() + "]" + " but got: " + node.getClassNode().getName());
    }
    String myTypeName = "@" + node.getClassNode().getNameWithoutPackage();
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode.isStatic(), myTypeName);
        if (lockExpr == null)
            return;
        // get lock type
        final Expression lockType;
        if (isWriteLock) {
            lockType = callX(varX(lockExpr, LOCK_TYPE), "writeLock");
        } else {
            lockType = callX(varX(lockExpr, LOCK_TYPE), "readLock");
        }
        Expression acquireLock = callX(lockType, "lock");
        Expression releaseLock = callX(lockType, "unlock");
        Statement originalCode = mNode.getCode();
        mNode.setCode(block(stmt(acquireLock), new TryCatchStatement(originalCode, stmt(releaseLock))));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

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