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 | package com.github.javaparser.ast.body; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
27 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
28 | import com.github.javaparser.ast.observer.ObservableProperty; |
29 | import static com.github.javaparser.utils.Utils.assertNotNull; |
30 | import com.github.javaparser.ast.visitor.CloneVisitor; |
31 | import com.github.javaparser.metamodel.BodyDeclarationMetaModel; |
32 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
33 | import com.github.javaparser.TokenRange; |
34 | import com.github.javaparser.ast.Generated; |
35 | import java.util.function.Consumer; |
36 | import static com.github.javaparser.utils.CodeGenerationUtils.f; |
37 | import java.util.Optional; |
38 | |
39 | /** |
40 | * Any declaration that can appear between the { and } of a class, interface, or enum. |
41 | * |
42 | * @author Julio Vilmar Gesser |
43 | */ |
44 | public abstract class BodyDeclaration<T extends BodyDeclaration<?>> extends Node implements NodeWithAnnotations<T> { |
45 | |
46 | private NodeList<AnnotationExpr> annotations; |
47 | |
48 | public BodyDeclaration() { |
49 | this(null, new NodeList<>()); |
50 | } |
51 | |
52 | @AllFieldsConstructor |
53 | public BodyDeclaration(NodeList<AnnotationExpr> annotations) { |
54 | this(null, annotations); |
55 | } |
56 | |
57 | /** |
58 | * This constructor is used by the parser and is considered private. |
59 | */ |
60 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
61 | public BodyDeclaration(TokenRange tokenRange, NodeList<AnnotationExpr> annotations) { |
62 | super(tokenRange); |
63 | setAnnotations(annotations); |
64 | customInitialization(); |
65 | } |
66 | |
67 | protected BodyDeclaration(TokenRange range) { |
68 | this(range, new NodeList<>()); |
69 | } |
70 | |
71 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
72 | public NodeList<AnnotationExpr> getAnnotations() { |
73 | return annotations; |
74 | } |
75 | |
76 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
77 | @SuppressWarnings("unchecked") |
78 | public T setAnnotations(final NodeList<AnnotationExpr> annotations) { |
79 | assertNotNull(annotations); |
80 | if (annotations == this.annotations) { |
81 | return (T) this; |
82 | } |
83 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
84 | if (this.annotations != null) |
85 | this.annotations.setParentNode(null); |
86 | this.annotations = annotations; |
87 | setAsParentNodeOf(annotations); |
88 | return (T) this; |
89 | } |
90 | |
91 | @Override |
92 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
93 | public boolean remove(Node node) { |
94 | if (node == null) |
95 | return false; |
96 | for (int i = 0; i < annotations.size(); i++) { |
97 | if (annotations.get(i) == node) { |
98 | annotations.remove(i); |
99 | return true; |
100 | } |
101 | } |
102 | return super.remove(node); |
103 | } |
104 | |
105 | @Override |
106 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
107 | public BodyDeclaration<?> clone() { |
108 | return (BodyDeclaration<?>) accept(new CloneVisitor(), null); |
109 | } |
110 | |
111 | @Override |
112 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
113 | public BodyDeclarationMetaModel getMetaModel() { |
114 | return JavaParserMetaModel.bodyDeclarationMetaModel; |
115 | } |
116 | |
117 | @Override |
118 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
119 | public boolean replace(Node node, Node replacementNode) { |
120 | if (node == null) |
121 | return false; |
122 | for (int i = 0; i < annotations.size(); i++) { |
123 | if (annotations.get(i) == node) { |
124 | annotations.set(i, (AnnotationExpr) replacementNode); |
125 | return true; |
126 | } |
127 | } |
128 | return super.replace(node, replacementNode); |
129 | } |
130 | |
131 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
132 | public boolean isAnnotationDeclaration() { |
133 | return false; |
134 | } |
135 | |
136 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
137 | public AnnotationDeclaration asAnnotationDeclaration() { |
138 | throw new IllegalStateException(f("%s is not an AnnotationDeclaration", this)); |
139 | } |
140 | |
141 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
142 | public boolean isAnnotationMemberDeclaration() { |
143 | return false; |
144 | } |
145 | |
146 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
147 | public AnnotationMemberDeclaration asAnnotationMemberDeclaration() { |
148 | throw new IllegalStateException(f("%s is not an AnnotationMemberDeclaration", this)); |
149 | } |
150 | |
151 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
152 | public boolean isCallableDeclaration() { |
153 | return false; |
154 | } |
155 | |
156 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
157 | public CallableDeclaration asCallableDeclaration() { |
158 | throw new IllegalStateException(f("%s is not an CallableDeclaration", this)); |
159 | } |
160 | |
161 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
162 | public boolean isClassOrInterfaceDeclaration() { |
163 | return false; |
164 | } |
165 | |
166 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
167 | public ClassOrInterfaceDeclaration asClassOrInterfaceDeclaration() { |
168 | throw new IllegalStateException(f("%s is not an ClassOrInterfaceDeclaration", this)); |
169 | } |
170 | |
171 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
172 | public boolean isConstructorDeclaration() { |
173 | return false; |
174 | } |
175 | |
176 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
177 | public ConstructorDeclaration asConstructorDeclaration() { |
178 | throw new IllegalStateException(f("%s is not an ConstructorDeclaration", this)); |
179 | } |
180 | |
181 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
182 | public boolean isEnumConstantDeclaration() { |
183 | return false; |
184 | } |
185 | |
186 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
187 | public EnumConstantDeclaration asEnumConstantDeclaration() { |
188 | throw new IllegalStateException(f("%s is not an EnumConstantDeclaration", this)); |
189 | } |
190 | |
191 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
192 | public boolean isEnumDeclaration() { |
193 | return false; |
194 | } |
195 | |
196 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
197 | public EnumDeclaration asEnumDeclaration() { |
198 | throw new IllegalStateException(f("%s is not an EnumDeclaration", this)); |
199 | } |
200 | |
201 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
202 | public boolean isFieldDeclaration() { |
203 | return false; |
204 | } |
205 | |
206 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
207 | public FieldDeclaration asFieldDeclaration() { |
208 | throw new IllegalStateException(f("%s is not an FieldDeclaration", this)); |
209 | } |
210 | |
211 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
212 | public boolean isInitializerDeclaration() { |
213 | return false; |
214 | } |
215 | |
216 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
217 | public InitializerDeclaration asInitializerDeclaration() { |
218 | throw new IllegalStateException(f("%s is not an InitializerDeclaration", this)); |
219 | } |
220 | |
221 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
222 | public boolean isMethodDeclaration() { |
223 | return false; |
224 | } |
225 | |
226 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
227 | public MethodDeclaration asMethodDeclaration() { |
228 | throw new IllegalStateException(f("%s is not an MethodDeclaration", this)); |
229 | } |
230 | |
231 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
232 | public boolean isTypeDeclaration() { |
233 | return false; |
234 | } |
235 | |
236 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
237 | public TypeDeclaration asTypeDeclaration() { |
238 | throw new IllegalStateException(f("%s is not an TypeDeclaration", this)); |
239 | } |
240 | |
241 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
242 | public void ifAnnotationDeclaration(Consumer<AnnotationDeclaration> action) { |
243 | } |
244 | |
245 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
246 | public void ifAnnotationMemberDeclaration(Consumer<AnnotationMemberDeclaration> action) { |
247 | } |
248 | |
249 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
250 | public void ifCallableDeclaration(Consumer<CallableDeclaration> action) { |
251 | } |
252 | |
253 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
254 | public void ifClassOrInterfaceDeclaration(Consumer<ClassOrInterfaceDeclaration> action) { |
255 | } |
256 | |
257 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
258 | public void ifConstructorDeclaration(Consumer<ConstructorDeclaration> action) { |
259 | } |
260 | |
261 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
262 | public void ifEnumConstantDeclaration(Consumer<EnumConstantDeclaration> action) { |
263 | } |
264 | |
265 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
266 | public void ifEnumDeclaration(Consumer<EnumDeclaration> action) { |
267 | } |
268 | |
269 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
270 | public void ifFieldDeclaration(Consumer<FieldDeclaration> action) { |
271 | } |
272 | |
273 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
274 | public void ifInitializerDeclaration(Consumer<InitializerDeclaration> action) { |
275 | } |
276 | |
277 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
278 | public void ifMethodDeclaration(Consumer<MethodDeclaration> action) { |
279 | } |
280 | |
281 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
282 | public void ifTypeDeclaration(Consumer<TypeDeclaration> action) { |
283 | } |
284 | |
285 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
286 | public Optional<AnnotationDeclaration> toAnnotationDeclaration() { |
287 | return Optional.empty(); |
288 | } |
289 | |
290 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
291 | public Optional<AnnotationMemberDeclaration> toAnnotationMemberDeclaration() { |
292 | return Optional.empty(); |
293 | } |
294 | |
295 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
296 | public Optional<CallableDeclaration> toCallableDeclaration() { |
297 | return Optional.empty(); |
298 | } |
299 | |
300 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
301 | public Optional<ClassOrInterfaceDeclaration> toClassOrInterfaceDeclaration() { |
302 | return Optional.empty(); |
303 | } |
304 | |
305 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
306 | public Optional<ConstructorDeclaration> toConstructorDeclaration() { |
307 | return Optional.empty(); |
308 | } |
309 | |
310 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
311 | public Optional<EnumConstantDeclaration> toEnumConstantDeclaration() { |
312 | return Optional.empty(); |
313 | } |
314 | |
315 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
316 | public Optional<EnumDeclaration> toEnumDeclaration() { |
317 | return Optional.empty(); |
318 | } |
319 | |
320 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
321 | public Optional<FieldDeclaration> toFieldDeclaration() { |
322 | return Optional.empty(); |
323 | } |
324 | |
325 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
326 | public Optional<InitializerDeclaration> toInitializerDeclaration() { |
327 | return Optional.empty(); |
328 | } |
329 | |
330 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
331 | public Optional<MethodDeclaration> toMethodDeclaration() { |
332 | return Optional.empty(); |
333 | } |
334 | |
335 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
336 | public Optional<TypeDeclaration> toTypeDeclaration() { |
337 | return Optional.empty(); |
338 | } |
339 | } |
340 |
Members