Java Code Examples for org.codehaus.groovy.ast.ClassNode#getAnnotations()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getAnnotations() . 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: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static List<String> getKnownImmutables(AbstractASTTransformation xform, ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    final List<String> immutables = new ArrayList<String>();
    if (anno == null) return immutables;

    final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLES);
    if (expression == null) return immutables;

    if (!(expression instanceof ListExpression)) {
        xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable property names via \"" + MEMBER_KNOWN_IMMUTABLES + "\"", anno);
        return immutables;
    }

    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ConstantExpression) {
            immutables.add((String) ((ConstantExpression) listItemExpression).getValue());
        }
    }
    if (!xform.checkPropertyList(cNode, immutables, "knownImmutables", anno, "immutable class", false)) return immutables;

    return immutables;
}
 
Example 2
Source File: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static List<String> getKnownImmutableClasses(AbstractASTTransformation xform, ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    final List<String> immutableClasses = new ArrayList<String>();

    if (anno == null) return immutableClasses;
    final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLE_CLASSES);
    if (expression == null) return immutableClasses;

    if (!(expression instanceof ListExpression)) {
        xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable classes via \"" + MEMBER_KNOWN_IMMUTABLE_CLASSES + "\"", anno);
        return immutableClasses;
    }

    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
        if (listItemExpression instanceof ClassExpression) {
            immutableClasses.add(listItemExpression.getType().getName());
        }
    }

    return immutableClasses;
}
 
Example 3
Source File: PropertyHandler.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static PropertyHandler createPropertyHandler(AbstractASTTransformation xform, GroovyClassLoader loader, ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(PROPERTY_OPTIONS_TYPE);
    AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
    if (anno == null) return new groovy.transform.options.DefaultPropertyHandler();

    ClassNode handlerClass = xform.getMemberClassValue(anno, "propertyHandler", ClassHelper.make(groovy.transform.options.DefaultPropertyHandler.class));

    if (handlerClass == null) {
        xform.addError("Couldn't determine propertyHandler class", anno);
        return null;
    }

    String className = handlerClass.getName();
    try {
        Object instance = loader.loadClass(className).getDeclaredConstructor().newInstance();
        if (!PropertyHandler.class.isAssignableFrom(instance.getClass())) {
            xform.addError("The propertyHandler class '" + handlerClass.getName() + "' on " + xform.getAnnotationName() + " is not a propertyHandler", anno);
            return null;
        }

        return (PropertyHandler) instance;
    } catch (Exception e) {
        xform.addError("Can't load propertyHandler '" + className + "' " + e, anno);
        return null;
    }
}
 
Example 4
Source File: InheritConstructorsASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void processClass(ClassNode cNode, AnnotationNode node) {
    if (cNode.isInterface()) {
        addError("Error processing interface '" + cNode.getName() +
                "'. " + ANNOTATION + " only allowed for classes.", cNode);
        return;
    }
    boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true);
    boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true);
    ClassNode sNode = cNode.getSuperClass();
    List<AnnotationNode> superAnnotations = sNode.getAnnotations(INHERIT_CONSTRUCTORS_TYPE);
    if (superAnnotations.size() == 1) {
        // We need @InheritConstructors from parent classes processed first
        // so force that order here. The transformation is benign on an already
        // processed node so processing twice in any order won't matter bar
        // a very small time penalty.
        processClass(sNode, node);
    }
    for (ConstructorNode cn : sNode.getDeclaredConstructors()) {
        addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations);
    }
}
 
Example 5
Source File: UnionTypeClassNode.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationNode> getAnnotations() {
    List<AnnotationNode> nodes = new LinkedList<AnnotationNode>();
    for (ClassNode delegate : delegates) {
        List<AnnotationNode> annotations = delegate.getAnnotations();
        if (annotations != null) nodes.addAll(annotations);
    }
    return nodes;
}
 
Example 6
Source File: BaseStreamCompilerAutoConfiguration.java    From spring-cloud-cli with Apache License 2.0 5 votes vote down vote up
static boolean isTransport(ClassNode node, String type) {
	for (AnnotationNode annotationNode : node.getAnnotations()) {
		String annotation = "EnableBinding";
		if (PatternMatchUtils.simpleMatch(annotation,
				annotationNode.getClassNode().getName())) {
			Expression expression = annotationNode.getMembers().get("transport");
			String transport = expression == null ? "rabbit" : expression.getText();
			if (transport != null) {
				transport = SystemPropertyUtils.resolvePlaceholders(transport);
			}
			return transport.equals(type);
		}
	}
	return false;
}
 
Example 7
Source File: Java8.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void configureAnnotation(AnnotationNode node) {
    ClassNode type = node.getClassNode();
    VMPlugin plugin = VMPluginFactory.getPlugin();
    List<AnnotationNode> annotations = type.getAnnotations();
    for (AnnotationNode an : annotations) {
        plugin.configureAnnotationNodeFromDefinition(an, node);
    }
    if (!node.getClassNode().getName().equals("java.lang.annotation.Retention")) {
        plugin.configureAnnotationNodeFromDefinition(node, node);
    }
}
 
Example 8
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Copies annotation from the trait to the helper, excluding the trait annotation itself
 * @param cNode the trait class node
 * @param helper the helper class node
 */
private static void copyClassAnnotations(final ClassNode cNode, final ClassNode helper) {
    List<AnnotationNode> annotations = cNode.getAnnotations();
    for (AnnotationNode annotation : annotations) {
        if (!annotation.getClassNode().equals(Traits.TRAIT_CLASSNODE)) {
            helper.addAnnotation(annotation);
        }
    }
}
 
Example 9
Source File: Traits.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Collects all the self types that a type should extend or implement, given
 * the traits is implements.
 * @param receiver a class node that may implement a trait
 * @param selfTypes a collection where the list of self types will be written
 * @param checkInterfaces should the interfaces that the node implements be collected too
 * @param checkSuper should we collect from the superclass too
 * @return the selfTypes collection itself
 * @since 2.4.0
 */
public static LinkedHashSet<ClassNode> collectSelfTypes(
        ClassNode receiver,
        LinkedHashSet<ClassNode> selfTypes,
        boolean checkInterfaces,
        boolean checkSuper) {
    if (Traits.isTrait(receiver)) {
        List<AnnotationNode> annotations = receiver.getAnnotations(SELFTYPE_CLASSNODE);
        for (AnnotationNode annotation : annotations) {
            Expression value = annotation.getMember("value");
            if (value instanceof ClassExpression) {
                selfTypes.add(value.getType());
            } else if (value instanceof ListExpression) {
                List<Expression> expressions = ((ListExpression) value).getExpressions();
                for (Expression expression : expressions) {
                    if (expression instanceof ClassExpression) {
                        selfTypes.add(expression.getType());
                    }
                }
            }
        }
    }
    if (checkInterfaces) {
        ClassNode[] interfaces = receiver.getInterfaces();
        for (ClassNode anInterface : interfaces) {
            collectSelfTypes(anInterface, selfTypes, true, checkSuper);
        }
    }

    if (checkSuper) {
        ClassNode superClass = receiver.getSuperClass();
        if (superClass != null) {
            collectSelfTypes(superClass, selfTypes, checkInterfaces, true);
        }
    }
    return selfTypes;
}
 
Example 10
Source File: UnionTypeClassNode.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public List<AnnotationNode> getAnnotations(final ClassNode type) {
    List<AnnotationNode> nodes = new LinkedList<AnnotationNode>();
    for (ClassNode delegate : delegates) {
        List<AnnotationNode> annotations = delegate.getAnnotations(type);
        if (annotations != null) nodes.addAll(annotations);
    }
    return nodes;
}
 
Example 11
Source File: VariableScopeVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addClassNodeOccurrences(ClassNode visitedNode, ClassNode findingNode) {
    final String findingName = ElementUtils.getTypeName(findingNode);
    final ClassNode superClass = visitedNode.getUnresolvedSuperClass(false);
    final ClassNode[] interfaces = visitedNode.getInterfaces();

    // Check if the caret is on the ClassNode itself
    if (findingName.equals(visitedNode.getName())) {
        occurrences.add(new FakeASTNode(visitedNode, visitedNode.getNameWithoutPackage()));
    }

    // Check if the caret is on the parent type
    if (superClass.getLineNumber() > 0 && superClass.getColumnNumber() > 0) {
        if (findingName.equals(superClass.getName())) {
            occurrences.add(new FakeASTNode(superClass, superClass.getNameWithoutPackage()));
        }
    }

    // Check all implemented interfaces
    for (ClassNode interfaceNode : interfaces) {
        if (interfaceNode.getLineNumber() > 0 && interfaceNode.getColumnNumber() > 0) {
            if (findingName.equals(interfaceNode.getName())) {
                occurrences.add(new FakeASTNode(interfaceNode, interfaceNode.getNameWithoutPackage()));
            }
        }
    }

    // Check all class level annotations
    for (AnnotationNode annotation : visitedNode.getAnnotations(findingNode)) {
        addAnnotationOccurrences(annotation, findingNode);
    }
}
 
Example 12
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode getSerializeClass(ClassNode alias) {
    List<AnnotationNode> annotations = alias.getAnnotations(ClassHelper.make(AnnotationCollector.class));
    if (!annotations.isEmpty()) {
        AnnotationNode annotationNode = annotations.get(0);
        Expression member = annotationNode.getMember("serializeClass");
        if (member instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) member;
            if (!ce.getType().getName().equals(AnnotationCollector.class.getName())) {
                alias = ce.getType();
            }
        }
    }
    return alias;
}
 
Example 13
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<AnnotationNode> getTargetListFromAnnotations(ClassNode alias) {
    List<AnnotationNode> annotations = alias.getAnnotations();
    if (annotations.size() < 2) {
        return Collections.emptyList();
    }
    List<AnnotationNode> ret = new ArrayList<>(annotations.size());
    for (AnnotationNode an : annotations) {
        ClassNode type = an.getClassNode();
        if (type.getName().equals(AnnotationCollector.class.getName()) || "java.lang.annotation".equals(type.getPackageName())) continue;
        AnnotationNode toAdd = new AnnotationNode(type);
        copyMembers(an, toAdd);
        ret.add(toAdd);
    }
    return ret;
}
 
Example 14
Source File: ImmutablePropertyUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean hasImmutableAnnotation(ClassNode type) {
    List<AnnotationNode> annotations = type.getAnnotations();
    for (AnnotationNode next : annotations) {
        String name = next.getClassNode().getName();
        if (matchingMarkerName(name)) return true;
    }
    return false;
}
 
Example 15
Source File: ImmutableASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
static boolean makeImmutable(ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
    AnnotationNode annoImmutable = annotations.isEmpty() ? null : annotations.get(0);
    return annoImmutable != null;
}
 
Example 16
Source File: NullCheckASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static boolean hasIncludeGenerated(ClassNode cNode) {
    List<AnnotationNode> annotations = cNode.getAnnotations(NULL_CHECK_TYPE);
    if (annotations.isEmpty()) return false;
    return hasIncludeGenerated(annotations.get(0));
}
 
Example 17
Source File: Traits.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the specified class node is annotated with the {@link Trait} interface.
 * @param cNode a class node
 * @return true if the specified class node is annotated with the {@link Trait} interface.
 */
public static boolean isAnnotatedWithTrait(final ClassNode cNode) {
    List<AnnotationNode> traitAnn = cNode.getAnnotations(Traits.TRAIT_CLASSNODE);
    return traitAnn != null && !traitAnn.isEmpty();
}