MCPcopy Create free account
hub / github.com/apache/groovy / AnnotatedNode

Class AnnotatedNode

src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java:31–196  ·  view source on GitHub ↗

Base class for any AST node which is capable of being annotated

Source from the content-addressed store, hash-verified

29 * Base class for any AST node which is capable of being annotated
30 */
31public class AnnotatedNode extends ASTNode implements GroovydocHolder<AnnotatedNode> {
32 private List<AnnotationNode> annotations = Collections.emptyList();
33 private ClassNode declaringClass;
34 private boolean synthetic;
35
36 /**
37 * Returns all annotations attached to this AST node.
38 * Annotations are runtime-visible or source-level metadata attached to language elements.
39 *
40 * @return list of {@link AnnotationNode}, or empty list if none
41 */
42 public List<AnnotationNode> getAnnotations() {
43 return annotations;
44 }
45
46 /**
47 * Returns annotations of a specific type attached to this AST node.
48 * Filters the annotation list by the provided type.
49 *
50 * @param type the annotation type to filter by
51 * @return list of matching {@link AnnotationNode}, or empty list if none
52 */
53 public List<AnnotationNode> getAnnotations(final ClassNode type) {
54 List<AnnotationNode> annotations = new ArrayList<>();
55 for (AnnotationNode node : getAnnotations()) {
56 if (type.equals(node.getClassNode())) {
57 annotations.add(node);
58 }
59 }
60 return annotations;
61 }
62
63 /**
64 * Adds a new annotation of the specified type to this node.
65 * Creates an {@link AnnotationNode} and attaches it.
66 *
67 * @param type the annotation type as a {@link ClassNode}
68 * @return the newly created {@link AnnotationNode}
69 */
70 public AnnotationNode addAnnotation(final ClassNode type) {
71 AnnotationNode node = new AnnotationNode(type);
72 addAnnotation(node);
73 return node;
74 }
75
76 /**
77 * Attaches an annotation node to this AST node.
78 * Does nothing if the annotation is null. Lazily initializes the annotations list.
79 *
80 * @param annotation the {@link AnnotationNode} to attach
81 */
82 public void addAnnotation(AnnotationNode annotation) {
83 if (annotation != null) {
84 if (annotations == Collections.EMPTY_LIST) {
85 annotations = new ArrayList<>(3);
86 }
87 annotations.add(annotation);
88 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected