Search in sources :

Example 1 with PropertyHandler

use of groovy.transform.options.PropertyHandler in project groovy by apache.

the class ImmutableASTTransformation method visit.

@Override
public void visit(final ASTNode[] nodes, final 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) {
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, (ClassNode) parent);
        if (handler == null || !handler.validateAttributes(this, anno))
            return;
        doMakeImmutable((ClassNode) parent, anno, handler);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 2 with PropertyHandler

use of groovy.transform.options.PropertyHandler in project groovy by apache.

the class RecordTypeASTTransformation method visit.

@Override
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) {
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, (ClassNode) parent);
        if (handler == null)
            return;
        if (!handler.validateAttributes(this, anno))
            return;
        doProcessRecordType((ClassNode) parent, handler);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 3 with PropertyHandler

use of groovy.transform.options.PropertyHandler in project groovy by apache.

the class MapConstructorASTTransformation method visit.

@Override
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 includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
        boolean includeStatic = memberHasValue(anno, "includeStatic", true);
        boolean allProperties = memberHasValue(anno, "allProperties", true);
        boolean noArg = memberHasValue(anno, "noArg", true);
        boolean specialNamedArgHandling = !memberHasValue(anno, "specialNamedArgHandling", false);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        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, includeSuperProperties, allProperties))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties))
            return;
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode);
        if (handler == null)
            return;
        if (!handler.validateAttributes(this, anno))
            return;
        Expression pre = anno.getMember("pre");
        if (pre != null && !(pre instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode);
            return;
        }
        Expression post = anno.getMember("post");
        if (post != null && !(post instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode);
            return;
        }
        createConstructors(this, anno, handler, cNode, includeFields, includeProperties, includeSuperProperties, includeSuperFields, noArg, allNames, allProperties, specialNamedArgHandling, includeStatic, excludes, includes, (ClosureExpression) pre, (ClosureExpression) post, source);
        if (pre != null) {
            anno.setMember("pre", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
        if (post != null) {
            anno.setMember("post", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 4 with PropertyHandler

use of groovy.transform.options.PropertyHandler in project groovy by apache.

the class TupleConstructorASTTransformation method visit.

@Override
public void visit(final ASTNode[] nodes, final 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 allProperties = memberHasValue(anno, "allProperties", true);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        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, includeSuperProperties, allProperties, includeSuperFields, false))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false))
            return;
        GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode);
        if (handler == null)
            return;
        if (!handler.validateAttributes(this, anno))
            return;
        Expression pre = anno.getMember("pre");
        if (pre != null && !(pre instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode);
            return;
        }
        Expression post = anno.getMember("post");
        if (post != null && !(post instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode);
            return;
        }
        createConstructor(this, anno, cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, excludes, includes, allNames, allProperties, sourceUnit, handler, (ClosureExpression) pre, (ClosureExpression) post);
        if (pre != null) {
            anno.setMember("pre", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
        if (post != null) {
            anno.setMember("post", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)4 PropertyHandler (groovy.transform.options.PropertyHandler)4 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)4 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)2 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)1 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)1