Java Code Examples for org.codehaus.groovy.ast.AnnotationNode#setMember()

The following examples show how to use org.codehaus.groovy.ast.AnnotationNode#setMember() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Implementation method of the alias annotation processor. This method will
 * get the list of annotations we aliased from the collector and adds it to
 * aliasAnnotationUsage. The method will also map all members from
 * aliasAnnotationUsage to the aliased nodes. Should a member stay unmapped,
 * we will ad an error. Further processing of those members is done by the
 * annotations.
 *
 * @param collector                 reference to the annotation with {@link AnnotationCollector}
 * @param aliasAnnotationUsage      reference to the place of usage of the alias
 * @param aliasAnnotated            reference to the node that has been annotated by the alias
 * @param source                    source unit for error reporting
 * @return list of the new AnnotationNodes
 */
public List<AnnotationNode> visit(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, AnnotatedNode aliasAnnotated, SourceUnit source) {
    List<AnnotationNode> ret =  getTargetAnnotationList(collector, aliasAnnotationUsage, source);
    Set<String> unusedNames = new HashSet<>(aliasAnnotationUsage.getMembers().keySet());

    for (AnnotationNode an: ret) {
        for (String name : aliasAnnotationUsage.getMembers().keySet()) {
            if (an.getClassNode().hasMethod(name, Parameter.EMPTY_ARRAY)) {
                unusedNames.remove(name);
                an.setMember(name, aliasAnnotationUsage.getMember(name));
            }
        }
    }

    if (!unusedNames.isEmpty()) {
        String message = "Annotation collector got unmapped names "+unusedNames.toString()+".";
        addError(message, aliasAnnotationUsage, source);
    }

    return ret;
}
 
Example 2
Source File: AutoImplementASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
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(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
    }
}
 
Example 3
Source File: ASTTransformationCollectorCodeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void mergeParameters(final AnnotationNode to, final AnnotationNode from) {
    for (String name : from.getMembers().keySet()) {
        if (to.getMember(name) == null) {
            to.setMember(name, from.getMember(name));
        }
    }
}
 
Example 4
Source File: GrabAnnotationTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void extractGrab(Expression init, ConstantExpression ce) {
    if (ce.getValue() instanceof AnnotationNode) {
        AnnotationNode annotation = (AnnotationNode) ce.getValue();
        if ((init != null) && (annotation.getMember("initClass") != null)) {
            annotation.setMember("initClass", init);
        }
        String name = annotation.getClassNode().getName();
        if ((GRAB_CLASS_NAME.equals(name))
                || (allowShortGrab && GRAB_SHORT_NAME.equals(name))
                || (grabAliases.contains(name))) {
            grabAnnotations.add(annotation);
        }
        if ((GRABEXCLUDE_CLASS_NAME.equals(name))
                || (allowShortGrabExcludes && GRABEXCLUDE_SHORT_NAME.equals(name))
                || (grabExcludeAliases.contains(name))) {
            grabExcludeAnnotations.add(annotation);
        }
        if ((GRABCONFIG_CLASS_NAME.equals(name))
                || (allowShortGrabConfig && GRABCONFIG_SHORT_NAME.equals(name))
                || (grabConfigAliases.contains(name))) {
            grabConfigAnnotations.add(annotation);
        }
        if ((GRABRESOLVER_CLASS_NAME.equals(name))
                || (allowShortGrabResolver && GRABRESOLVER_SHORT_NAME.equals(name))
                || (grabResolverAliases.contains(name))) {
            grabResolverAnnotations.add(annotation);
        }
    }
}
 
Example 5
Source File: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
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));
        }
    }
}
 
Example 6
Source File: TupleConstructorASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
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 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;
        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;
        }

        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));
        }
    }
}