Search in sources :

Example 16 with AnnotatedNode

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

the class AutoImplementASTTransformation 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;
        ClassNode exception = getMemberClassValue(anno, "exception");
        if (exception != null && Undefined.isUndefinedException(exception)) {
            exception = null;
        }
        String message = getMemberStringValue(anno, "message");
        Expression code = anno.getMember("code");
        if (code != null && !(code instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'code'. Found " + code, cNode);
            return;
        }
        createMethods(cNode, exception, message, (ClosureExpression) code);
        if (code != null) {
            anno.setMember("code", new ClosureExpression(new Parameter[0], new EmptyStatement()));
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 17 with AnnotatedNode

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

the class BaseScriptASTTransformation 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) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 18 with AnnotatedNode

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

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

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

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 = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        final boolean allNames = memberHasValue(anno, "allNames", true);
        if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
            return;
        createHashCode(cNode, cacheHashCode, includeFields, callSuper, excludes, includes, allNames);
        createEquals(cNode, includeFields, callSuper, useCanEqual, excludes, includes, allNames);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 20 with AnnotatedNode

use of org.codehaus.groovy.ast.AnnotatedNode in project spring-boot by spring-projects.

the class GenericBomAstTransformation method addDependencyManagementBom.

private void addDependencyManagementBom(ModuleNode node, String module) {
    AnnotatedNode annotated = getAnnotatedNode(node);
    if (annotated != null) {
        AnnotationNode bom = getAnnotation(annotated);
        List<Expression> expressions = new ArrayList<>(getConstantExpressions(bom.getMember("value")));
        expressions.add(new ConstantExpression(module));
        bom.setMember("value", new ListExpression(expressions));
    }
}
Also used : 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) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList)

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