Search in sources :

Example 31 with AnnotatedNode

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

the class MemoizedASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
    if (MY_TYPE.equals(annotationNode.getClassNode()) && annotatedNode instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) annotatedNode;
        if (methodNode.isAbstract()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for abstract methods.", methodNode);
            return;
        }
        if (methodNode.isVoidMethod()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for void methods.", methodNode);
            return;
        }
        ClassNode ownerClassNode = methodNode.getDeclaringClass();
        MethodNode delegatingMethod = buildDelegatingMethod(methodNode, ownerClassNode);
        ownerClassNode.addMethod(delegatingMethod);
        int modifiers = FieldNode.ACC_PRIVATE | FieldNode.ACC_FINAL;
        if (methodNode.isStatic()) {
            modifiers = modifiers | FieldNode.ACC_STATIC;
        }
        int protectedCacheSize = getMemberIntValue(annotationNode, PROTECTED_CACHE_SIZE_NAME);
        int maxCacheSize = getMemberIntValue(annotationNode, MAX_CACHE_SIZE_NAME);
        MethodCallExpression memoizeClosureCallExpression = buildMemoizeClosureCallExpression(delegatingMethod, protectedCacheSize, maxCacheSize);
        String memoizedClosureFieldName = buildUniqueName(ownerClassNode, CLOSURE_LABEL, methodNode);
        FieldNode memoizedClosureField = new FieldNode(memoizedClosureFieldName, modifiers, newClass(ClassHelper.CLOSURE_TYPE), null, memoizeClosureCallExpression);
        ownerClassNode.addField(memoizedClosureField);
        BlockStatement newCode = new BlockStatement();
        MethodCallExpression closureCallExpression = callX(fieldX(memoizedClosureField), CLOSURE_CALL_METHOD_NAME, args(methodNode.getParameters()));
        closureCallExpression.setImplicitThis(false);
        newCode.addStatement(returnS(closureCallExpression));
        methodNode.setCode(newCode);
        VariableScopeVisitor visitor = new VariableScopeVisitor(source);
        visitor.visitClass(ownerClassNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 32 with AnnotatedNode

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

the class SourceURIASTTransformation 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) {
        setScriptURIOnDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof FieldNode) {
        setScriptURIOnField((FieldNode) parent, node);
    } else {
        addError("Expected to find the annotation " + MY_TYPE_NAME + " on an declaration statement.", parent);
    }
}
Also used : 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)

Example 33 with AnnotatedNode

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

the class StaticTypesTransformation method visit.

//    @Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    Map<String, Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        visitor = newVisitor(source, methodNode.getDeclaringClass());
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) StaticTypeCheckingVisitor(org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 34 with AnnotatedNode

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

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 35 with AnnotatedNode

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

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);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        if (includes != null && includes.contains("super")) {
            includeSuper = 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);
        boolean ignoreNulls = memberHasValue(anno, "ignoreNulls", true);
        boolean includePackage = !memberHasValue(anno, "includePackage", false);
        boolean allProperties = !memberHasValue(anno, "allProperties", false);
        boolean allNames = memberHasValue(anno, "allNames", true);
        if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!checkPropertyList(cNode, includes != null ? DefaultGroovyMethods.minus(includes, "super") : null, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties))
            return;
        createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cacheToString, includeSuperProperties, allProperties, allNames);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ToString(groovy.transform.ToString)

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