JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/nodeTypes/NodeWithAnnotations.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.ast.nodeTypes;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.NodeList;
26import com.github.javaparser.ast.expr.*;
27
28import java.lang.annotation.Annotation;
29import java.util.Optional;
30
31import static com.github.javaparser.StaticJavaParser.parseExpression;
32import static com.github.javaparser.StaticJavaParser.parseName;
33
34/**
35 * A node that can be annotated.
36 *
37 * @author Federico Tomassetti
38 * @since July 2014
39 */
40public interface NodeWithAnnotations<N extends Node> {
41    NodeList<AnnotationExprgetAnnotations();
42
43    N setAnnotations(NodeList<AnnotationExprannotations);
44
45    void tryAddImportToParentCompilationUnit(Class<?> clazz);
46
47    default AnnotationExpr getAnnotation(int i) {
48        return getAnnotations().get(i);
49    }
50
51    @SuppressWarnings("unchecked")
52    default N setAnnotation(int iAnnotationExpr element) {
53        getAnnotations().set(ielement);
54        return (N) this;
55    }
56
57    @SuppressWarnings("unchecked")
58    default N addAnnotation(AnnotationExpr element) {
59        getAnnotations().add(element);
60        return (N) this;
61    }
62
63    /**
64     * Annotates this
65     *
66     * @param name the name of the annotation
67     * @return this
68     */
69    @SuppressWarnings("unchecked")
70    default N addAnnotation(String name) {
71        NormalAnnotationExpr annotation = new NormalAnnotationExpr(
72                parseName(name), new NodeList<>());
73        addAnnotation(annotation);
74        return (N) this;
75    }
76
77    /**
78     * Annotates this
79     *
80     * @param name the name of the annotation
81     * @return the {@link NormalAnnotationExpr} added
82     */
83    @SuppressWarnings("unchecked")
84    default NormalAnnotationExpr addAndGetAnnotation(String name) {
85        NormalAnnotationExpr annotation = new NormalAnnotationExpr(
86                parseName(name), new NodeList<>());
87        addAnnotation(annotation);
88        return annotation;
89    }
90
91    /**
92     * Annotates this node and automatically add the import
93     *
94     * @param clazz the class of the annotation
95     * @return this
96     */
97    default N addAnnotation(Class<? extends Annotationclazz) {
98        tryAddImportToParentCompilationUnit(clazz);
99        return addAnnotation(clazz.getSimpleName());
100    }
101
102    /**
103     * Annotates this node and automatically add the import
104     *
105     * @param clazz the class of the annotation
106     * @return the {@link NormalAnnotationExpr} added
107     */
108    default NormalAnnotationExpr addAndGetAnnotation(Class<? extends Annotationclazz) {
109        tryAddImportToParentCompilationUnit(clazz);
110        return addAndGetAnnotation(clazz.getSimpleName());
111    }
112
113    /**
114     * Annotates this with a marker annotation
115     *
116     * @param name the name of the annotation
117     * @return this
118     */
119    @SuppressWarnings("unchecked")
120    default N addMarkerAnnotation(String name) {
121        MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr(
122                parseName(name));
123        addAnnotation(markerAnnotationExpr);
124        return (N) this;
125    }
126
127    /**
128     * Annotates this with a marker annotation and automatically add the import
129     *
130     * @param clazz the class of the annotation
131     * @return this
132     */
133    default N addMarkerAnnotation(Class<? extends Annotationclazz) {
134        tryAddImportToParentCompilationUnit(clazz);
135        return addMarkerAnnotation(clazz.getSimpleName());
136    }
137
138    /**
139     * Annotates this with a single member annotation
140     *
141     * @param name the name of the annotation
142     * @param expression the part between ()
143     * @return this
144     */
145    @SuppressWarnings("unchecked")
146    default N addSingleMemberAnnotation(String nameExpression expression) {
147        SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr(
148                parseName(name), expression);
149        return addAnnotation(singleMemberAnnotationExpr);
150    }
151
152    /**
153     * Annotates this with a single member annotation
154     *
155     * @param clazz the class of the annotation
156     * @param expression the part between ()
157     * @return this
158     */
159    default N addSingleMemberAnnotation(Class<? extends AnnotationclazzExpression expression) {
160        tryAddImportToParentCompilationUnit(clazz);
161        return addSingleMemberAnnotation(clazz.getSimpleName(), expression);
162    }
163
164    /**
165     * Annotates this with a single member annotation
166     *
167     * @param name the name of the annotation
168     * @param value the value, don't forget to add \"\" for a string value
169     * @return this
170     */
171    default N addSingleMemberAnnotation(String nameString value) {
172        return addSingleMemberAnnotation(nameparseExpression(value));
173    }
174
175    /**
176     * Annotates this with a single member annotation and automatically add the import
177     *
178     * @param clazz the class of the annotation
179     * @param value the value, don't forget to add \"\" for a string value
180     * @return this
181     */
182    default N addSingleMemberAnnotation(Class<? extends Annotationclazz,
183                                        String value) {
184        tryAddImportToParentCompilationUnit(clazz);
185        return addSingleMemberAnnotation(clazz.getSimpleName(), value);
186    }
187
188    /**
189     * Check whether an annotation with this name is present on this element
190     *
191     * @param annotationName the name of the annotation
192     * @return true if found, false if not
193     */
194    default boolean isAnnotationPresent(String annotationName) {
195        return getAnnotations().stream().anyMatch(a -> a.getName().getIdentifier().equals(annotationName));
196    }
197
198    /**
199     * Check whether an annotation with this class is present on this element
200     *
201     * @param annotationClass the class of the annotation
202     * @return true if found, false if not
203     */
204    default boolean isAnnotationPresent(Class<? extends AnnotationannotationClass) {
205        return isAnnotationPresent(annotationClass.getSimpleName());
206    }
207
208    /**
209     * Try to find an annotation by its name
210     *
211     * @param annotationName the name of the annotation
212     */
213    default Optional<AnnotationExprgetAnnotationByName(String annotationName) {
214        return getAnnotations().stream().filter(a -> a.getName().getIdentifier().equals(annotationName)).findFirst();
215    }
216
217    /**
218     * Try to find an annotation by its class
219     *
220     * @param annotationClass the class of the annotation
221     */
222    default Optional<AnnotationExprgetAnnotationByClass(Class<? extends AnnotationannotationClass) {
223        return getAnnotationByName(annotationClass.getSimpleName());
224    }
225}
226
MembersX
NodeWithAnnotations:addMarkerAnnotation:Block:markerAnnotationExpr
NodeWithAnnotations:getAnnotation
NodeWithAnnotations:setAnnotation
NodeWithAnnotations:tryAddImportToParentCompilationUnit
NodeWithAnnotations:addAnnotation
NodeWithAnnotations:isAnnotationPresent
NodeWithAnnotations:addAnnotation:Block:annotation
NodeWithAnnotations:addMarkerAnnotation
NodeWithAnnotations:addAndGetAnnotation:Block:annotation
NodeWithAnnotations:getAnnotations
NodeWithAnnotations:addAndGetAnnotation
NodeWithAnnotations:getAnnotationByClass
NodeWithAnnotations:addSingleMemberAnnotation
NodeWithAnnotations:getAnnotationByName
NodeWithAnnotations:addSingleMemberAnnotation:Block:singleMemberAnnotationExpr
NodeWithAnnotations:setAnnotations
Members
X