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

The following examples show how to use org.codehaus.groovy.ast.AnnotationNode#addMember() . 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: GroovydocManager.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void attachGroovydocAnnotation(ASTNode node, String docCommentNodeText) {
    if (!runtimeGroovydocEnabled) {
        return;
    }

    if (!(node instanceof AnnotatedNode)) {
        return;
    }

    if (!docCommentNodeText.startsWith(RUNTIME_GROOVYDOC_PREFIX)) {
        return;
    }

    AnnotatedNode annotatedNode = (AnnotatedNode) node;
    AnnotationNode annotationNode = new AnnotationNode(ClassHelper.make(Groovydoc.class));
    annotationNode.addMember(VALUE, new ConstantExpression(docCommentNodeText));
    annotatedNode.addAnnotation(annotationNode);
}
 
Example 2
Source File: NamedVariantASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean processExplicitNamedParam(final MethodNode mNode, final Parameter mapParam, final BlockStatement inner, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam) {
    AnnotationNode namedParam = fromParam.getAnnotations(NAMED_PARAM_TYPE).get(0);
    boolean required = memberHasValue(namedParam, "required", true);
    if (getMemberStringValue(namedParam, "value") == null) {
        namedParam.addMember("value", constX(fromParam.getName()));
    }
    String name = getMemberStringValue(namedParam, "value");
    if (getMemberValue(namedParam, "type") == null) {
        namedParam.addMember("type", classX(fromParam.getType()));
    }
    if (hasDuplicates(mNode, propNames, name)) return false;
    // TODO: Check specified type is assignable from declared param type?
    //ClassNode type = getMemberClassValue(namedParam, "type");
    if (required) {
        if (fromParam.hasInitialExpression()) {
            addError("Error during " + NAMED_VARIANT + " processing. A required parameter can't have an initial value.", mNode);
            return false;
        }
        inner.addStatement(new AssertStatement(boolX(callX(varX(mapParam), "containsKey", args(constX(name)))),
                plusX(constX("Missing required named argument '" + name + "'. Keys found: "), callX(varX(mapParam), "keySet"))));
    }
    args.addExpression(propX(varX(mapParam), name));
    mapParam.addAnnotation(namedParam);
    fromParam.getAnnotations().remove(namedParam);
    return true;
}
 
Example 3
Source File: NamedVariantASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean processImplicitNamedParam(final MethodNode mNode, final Parameter mapParam, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam) {
    boolean required = fromParam.hasInitialExpression();
    String name = fromParam.getName();
    if (hasDuplicates(mNode, propNames, name)) return false;
    AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
    namedParam.addMember("value", constX(name));
    namedParam.addMember("type", classX(fromParam.getType()));
    namedParam.addMember("required", constX(required, true));
    mapParam.addAnnotation(namedParam);
    args.addExpression(propX(varX(mapParam), name));
    return true;
}
 
Example 4
Source File: NamedVariantASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean processDelegateParam(final MethodNode mNode, final Parameter mapParam, final ArgumentListExpression args, final List<String> propNames, final Parameter fromParam) {
    if (isInnerClass(fromParam.getType())) {
        if (mNode.isStatic()) {
            addError("Error during " + NAMED_VARIANT + " processing. Delegate type '" + fromParam.getType().getNameWithoutPackage() + "' is an inner class which is not supported.", mNode);
            return false;
        }
    }

    Set<String> names = new HashSet<>();
    List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
    for (String next : names) {
        if (hasDuplicates(mNode, propNames, next)) return false;
    }
    List<MapEntryExpression> entries = new ArrayList<>();
    for (PropertyNode pNode : props) {
        String name = pNode.getName();
        // create entry [name: __namedArgs.getOrDefault('name', initialValue)]
        Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> getDefaultExpression(pNode.getType()));
        entries.add(entryX(constX(name), callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue))));
        // create annotation @NamedParam(value='name', type=DelegateType)
        AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
        namedParam.addMember("value", constX(name));
        namedParam.addMember("type", classX(pNode.getType()));
        mapParam.addAnnotation(namedParam);
    }
    Expression delegateMap = mapX(entries);
    args.addExpression(castX(fromParam.getType(), delegateMap));
    return true;
}
 
Example 5
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
 * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
 * {@link groovy.transform.Generated} annotations will be copied if {@code includeGenerated} is true.
 * <p>
 * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
 */
public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, final List<AnnotationNode> notCopied, final boolean includeGenerated) {
    List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
    for (AnnotationNode annotation : annotationList)  {
        List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
        if (annotations.isEmpty()) continue;

        if (hasClosureMember(annotation)) {
            notCopied.add(annotation);
            continue;
        }

        if (!includeGenerated && annotation.getClassNode().getName().equals("groovy.transform.Generated")) {
            continue;
        }

        AnnotationNode retentionPolicyAnnotation = annotations.get(0);
        Expression valueExpression = retentionPolicyAnnotation.getMember("value");
        if (!(valueExpression instanceof PropertyExpression)) continue;

        PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
        boolean processAnnotation = propertyExpression.getProperty() instanceof ConstantExpression
                && ("RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue())
                    || "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()));
        if (processAnnotation)  {
            AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
            for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet())  {
                newAnnotation.addMember(member.getKey(), member.getValue());
            }
            newAnnotation.setSourcePosition(annotatedNode);

            copied.add(newAnnotation);
        }
    }
}
 
Example 6
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void copyMembers(final Map<String, Expression> members, final AnnotationNode to) {
    for (Map.Entry<String, Expression> entry : members.entrySet()) {
        to.addMember(entry.getKey(), entry.getValue());
    }
}
 
Example 7
Source File: CompileDynamicProcessor.java    From groovy with Apache License 2.0 4 votes vote down vote up
public List<AnnotationNode> visit(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, AnnotatedNode aliasAnnotated, SourceUnit source) {
    AnnotationNode node = new AnnotationNode(COMPILESTATIC_NODE);
    node.addMember("value", new PropertyExpression(new ClassExpression(TYPECHECKINGMODE_NODE), "SKIP"));
    return Collections.singletonList(node);
}
 
Example 8
Source File: Annotations.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void addMemberIfFound(AsmReferenceResolver resolver, AnnotationNode node, Map.Entry<String, Object> entry) {
    Expression value = annotationValueToExpression(entry.getValue(), resolver);
    if (value != null) {
        node.addMember(entry.getKey(), value);
    }
}