Search in sources :

Example 61 with AnnotatedNode

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

the class AutoCloneASTTransformation 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;
        cNode.addInterface(CLONEABLE_TYPE);
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        AutoCloneStyle style = getStyle(anno, "style");
        List<String> excludes = getMemberStringList(anno, "excludes");
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
            return;
        List<FieldNode> list = getInstancePropertyFields(cNode);
        if (includeFields) {
            list.addAll(getInstanceNonPropertyFields(cNode));
        }
        if (style == null)
            style = AutoCloneStyle.CLONE;
        switch(style) {
            case COPY_CONSTRUCTOR:
                createCloneCopyConstructor(cNode, list, excludes);
                break;
            case SERIALIZATION:
                createCloneSerialization(cNode);
                break;
            case SIMPLE:
                createSimpleClone(cNode, list, excludes);
                break;
            default:
                createClone(cNode, list, excludes);
                break;
        }
    }
}
Also used : AutoCloneStyle(groovy.transform.AutoCloneStyle) ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 62 with AnnotatedNode

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

the class NewifyASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    this.source = source;
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode())) {
        internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
    }
    boolean autoFlag = determineAutoFlag(node.getMember("auto"));
    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
        newifyClass((ClassNode) parent, autoFlag, determineClasses(value, false));
    } else if (parent instanceof MethodNode || parent instanceof FieldNode) {
        newifyMethodOrField(parent, autoFlag, determineClasses(value, false));
    } else if (parent instanceof DeclarationExpression) {
        newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(value, true));
    }
}
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) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 63 with AnnotatedNode

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

the class PackageScopeASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
    if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
        return;
    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
        List<groovy.transform.PackageScopeTarget> targets;
        if (value == null)
            targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
        else
            targets = determineTargets(value);
        visitClassNode((ClassNode) parent, targets);
        parent.getAnnotations();
    } else {
        if (value != null) {
            addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
            return;
        }
        if (parent instanceof MethodNode) {
            visitMethodNode((MethodNode) parent);
        } else if (parent instanceof FieldNode) {
            visitFieldNode((FieldNode) parent);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PackageScopeTarget(groovy.transform.PackageScopeTarget) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 64 with AnnotatedNode

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

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)

Example 65 with AnnotatedNode

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

the class SingletonASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (parent instanceof ClassNode) {
        ClassNode classNode = (ClassNode) parent;
        String propertyName = getMemberStringValue(node, "property", "instance");
        boolean isLazy = memberHasValue(node, "lazy", true);
        boolean isStrict = !memberHasValue(node, "strict", false);
        createField(classNode, propertyName, isLazy, isStrict);
    }
}
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