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.TokenRange; |
24 | import com.github.javaparser.ast.*; |
25 | import com.github.javaparser.ast.expr.AnnotationExpr; |
26 | import com.github.javaparser.ast.expr.SimpleName; |
27 | import com.github.javaparser.ast.nodeTypes.NodeWithExtends; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithImplements; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters; |
30 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAbstractModifier; |
31 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier; |
32 | import com.github.javaparser.ast.observer.ObservableProperty; |
33 | import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt; |
34 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
35 | import com.github.javaparser.ast.type.TypeParameter; |
36 | import com.github.javaparser.ast.visitor.CloneVisitor; |
37 | import com.github.javaparser.ast.visitor.GenericVisitor; |
38 | import com.github.javaparser.ast.visitor.VoidVisitor; |
39 | import com.github.javaparser.metamodel.ClassOrInterfaceDeclarationMetaModel; |
40 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
41 | import com.github.javaparser.resolution.Resolvable; |
42 | import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration; |
43 | import java.util.Optional; |
44 | import java.util.function.Consumer; |
45 | import static com.github.javaparser.utils.Utils.assertNotNull; |
46 | import com.github.javaparser.ast.Node; |
47 | import com.github.javaparser.ast.Generated; |
48 | |
49 | /** |
50 | * A definition of a class or interface.<br>{@code class X { ... }} |
51 | * |
52 | * @author Julio Vilmar Gesser |
53 | */ |
54 | public class ClassOrInterfaceDeclaration extends TypeDeclaration<ClassOrInterfaceDeclaration> implements NodeWithImplements<ClassOrInterfaceDeclaration>, NodeWithExtends<ClassOrInterfaceDeclaration>, NodeWithTypeParameters<ClassOrInterfaceDeclaration>, NodeWithAbstractModifier<ClassOrInterfaceDeclaration>, NodeWithFinalModifier<ClassOrInterfaceDeclaration>, Resolvable<ResolvedReferenceTypeDeclaration> { |
55 | |
56 | private boolean isInterface; |
57 | |
58 | private NodeList<TypeParameter> typeParameters; |
59 | |
60 | // Can contain more than one item if this is an interface |
61 | private NodeList<ClassOrInterfaceType> extendedTypes; |
62 | |
63 | private NodeList<ClassOrInterfaceType> implementedTypes; |
64 | |
65 | public ClassOrInterfaceDeclaration() { |
66 | this(null, new NodeList<>(), new NodeList<>(), false, new SimpleName(), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>()); |
67 | } |
68 | |
69 | public ClassOrInterfaceDeclaration(final NodeList<Modifier> modifiers, final boolean isInterface, final String name) { |
70 | this(null, modifiers, new NodeList<>(), isInterface, new SimpleName(name), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>()); |
71 | } |
72 | |
73 | @AllFieldsConstructor |
74 | public ClassOrInterfaceDeclaration(final NodeList<Modifier> modifiers, final NodeList<AnnotationExpr> annotations, final boolean isInterface, final SimpleName name, final NodeList<TypeParameter> typeParameters, final NodeList<ClassOrInterfaceType> extendedTypes, final NodeList<ClassOrInterfaceType> implementedTypes, final NodeList<BodyDeclaration<?>> members) { |
75 | this(null, modifiers, annotations, isInterface, name, typeParameters, extendedTypes, implementedTypes, members); |
76 | } |
77 | |
78 | /** |
79 | * This constructor is used by the parser and is considered private. |
80 | */ |
81 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
82 | public ClassOrInterfaceDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, boolean isInterface, SimpleName name, NodeList<TypeParameter> typeParameters, NodeList<ClassOrInterfaceType> extendedTypes, NodeList<ClassOrInterfaceType> implementedTypes, NodeList<BodyDeclaration<?>> members) { |
83 | super(tokenRange, modifiers, annotations, name, members); |
84 | setInterface(isInterface); |
85 | setTypeParameters(typeParameters); |
86 | setExtendedTypes(extendedTypes); |
87 | setImplementedTypes(implementedTypes); |
88 | customInitialization(); |
89 | } |
90 | |
91 | @Override |
92 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
93 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
94 | return v.visit(this, arg); |
95 | } |
96 | |
97 | @Override |
98 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
99 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
100 | v.visit(this, arg); |
101 | } |
102 | |
103 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
104 | public NodeList<ClassOrInterfaceType> getExtendedTypes() { |
105 | return extendedTypes; |
106 | } |
107 | |
108 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
109 | public NodeList<ClassOrInterfaceType> getImplementedTypes() { |
110 | return implementedTypes; |
111 | } |
112 | |
113 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
114 | public NodeList<TypeParameter> getTypeParameters() { |
115 | return typeParameters; |
116 | } |
117 | |
118 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
119 | public boolean isInterface() { |
120 | return isInterface; |
121 | } |
122 | |
123 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
124 | public ClassOrInterfaceDeclaration setExtendedTypes(final NodeList<ClassOrInterfaceType> extendedTypes) { |
125 | assertNotNull(extendedTypes); |
126 | if (extendedTypes == this.extendedTypes) { |
127 | return (ClassOrInterfaceDeclaration) this; |
128 | } |
129 | notifyPropertyChange(ObservableProperty.EXTENDED_TYPES, this.extendedTypes, extendedTypes); |
130 | if (this.extendedTypes != null) |
131 | this.extendedTypes.setParentNode(null); |
132 | this.extendedTypes = extendedTypes; |
133 | setAsParentNodeOf(extendedTypes); |
134 | return this; |
135 | } |
136 | |
137 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
138 | public ClassOrInterfaceDeclaration setImplementedTypes(final NodeList<ClassOrInterfaceType> implementedTypes) { |
139 | assertNotNull(implementedTypes); |
140 | if (implementedTypes == this.implementedTypes) { |
141 | return (ClassOrInterfaceDeclaration) this; |
142 | } |
143 | notifyPropertyChange(ObservableProperty.IMPLEMENTED_TYPES, this.implementedTypes, implementedTypes); |
144 | if (this.implementedTypes != null) |
145 | this.implementedTypes.setParentNode(null); |
146 | this.implementedTypes = implementedTypes; |
147 | setAsParentNodeOf(implementedTypes); |
148 | return this; |
149 | } |
150 | |
151 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
152 | public ClassOrInterfaceDeclaration setInterface(final boolean isInterface) { |
153 | if (isInterface == this.isInterface) { |
154 | return (ClassOrInterfaceDeclaration) this; |
155 | } |
156 | notifyPropertyChange(ObservableProperty.INTERFACE, this.isInterface, isInterface); |
157 | this.isInterface = isInterface; |
158 | return this; |
159 | } |
160 | |
161 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
162 | public ClassOrInterfaceDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) { |
163 | assertNotNull(typeParameters); |
164 | if (typeParameters == this.typeParameters) { |
165 | return (ClassOrInterfaceDeclaration) this; |
166 | } |
167 | notifyPropertyChange(ObservableProperty.TYPE_PARAMETERS, this.typeParameters, typeParameters); |
168 | if (this.typeParameters != null) |
169 | this.typeParameters.setParentNode(null); |
170 | this.typeParameters = typeParameters; |
171 | setAsParentNodeOf(typeParameters); |
172 | return this; |
173 | } |
174 | |
175 | @Override |
176 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
177 | public boolean remove(Node node) { |
178 | if (node == null) |
179 | return false; |
180 | for (int i = 0; i < extendedTypes.size(); i++) { |
181 | if (extendedTypes.get(i) == node) { |
182 | extendedTypes.remove(i); |
183 | return true; |
184 | } |
185 | } |
186 | for (int i = 0; i < implementedTypes.size(); i++) { |
187 | if (implementedTypes.get(i) == node) { |
188 | implementedTypes.remove(i); |
189 | return true; |
190 | } |
191 | } |
192 | for (int i = 0; i < typeParameters.size(); i++) { |
193 | if (typeParameters.get(i) == node) { |
194 | typeParameters.remove(i); |
195 | return true; |
196 | } |
197 | } |
198 | return super.remove(node); |
199 | } |
200 | |
201 | /** |
202 | * @return is this class's parent a LocalClassDeclarationStmt ? |
203 | */ |
204 | public boolean isLocalClassDeclaration() { |
205 | return getParentNode().map(p -> p instanceof LocalClassDeclarationStmt).orElse(false); |
206 | } |
207 | |
208 | @Override |
209 | public Optional<String> getFullyQualifiedName() { |
210 | if (isLocalClassDeclaration()) { |
211 | return Optional.empty(); |
212 | } |
213 | return super.getFullyQualifiedName(); |
214 | } |
215 | |
216 | /** |
217 | * @return is this an inner class? |
218 | * NOTE: many people are confused over terminology. Refer to https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html . |
219 | */ |
220 | public boolean isInnerClass() { |
221 | return isNestedType() && !isInterface && !isStatic(); |
222 | } |
223 | |
224 | @Override |
225 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
226 | public ClassOrInterfaceDeclaration clone() { |
227 | return (ClassOrInterfaceDeclaration) accept(new CloneVisitor(), null); |
228 | } |
229 | |
230 | @Override |
231 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
232 | public ClassOrInterfaceDeclarationMetaModel getMetaModel() { |
233 | return JavaParserMetaModel.classOrInterfaceDeclarationMetaModel; |
234 | } |
235 | |
236 | @Override |
237 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
238 | public boolean replace(Node node, Node replacementNode) { |
239 | if (node == null) |
240 | return false; |
241 | for (int i = 0; i < extendedTypes.size(); i++) { |
242 | if (extendedTypes.get(i) == node) { |
243 | extendedTypes.set(i, (ClassOrInterfaceType) replacementNode); |
244 | return true; |
245 | } |
246 | } |
247 | for (int i = 0; i < implementedTypes.size(); i++) { |
248 | if (implementedTypes.get(i) == node) { |
249 | implementedTypes.set(i, (ClassOrInterfaceType) replacementNode); |
250 | return true; |
251 | } |
252 | } |
253 | for (int i = 0; i < typeParameters.size(); i++) { |
254 | if (typeParameters.get(i) == node) { |
255 | typeParameters.set(i, (TypeParameter) replacementNode); |
256 | return true; |
257 | } |
258 | } |
259 | return super.replace(node, replacementNode); |
260 | } |
261 | |
262 | @Override |
263 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
264 | public boolean isClassOrInterfaceDeclaration() { |
265 | return true; |
266 | } |
267 | |
268 | @Override |
269 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
270 | public ClassOrInterfaceDeclaration asClassOrInterfaceDeclaration() { |
271 | return this; |
272 | } |
273 | |
274 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
275 | public void ifClassOrInterfaceDeclaration(Consumer<ClassOrInterfaceDeclaration> action) { |
276 | action.accept(this); |
277 | } |
278 | |
279 | @Override |
280 | public ResolvedReferenceTypeDeclaration resolve() { |
281 | return getSymbolResolver().resolveDeclaration(this, ResolvedReferenceTypeDeclaration.class); |
282 | } |
283 | |
284 | @Override |
285 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
286 | public Optional<ClassOrInterfaceDeclaration> toClassOrInterfaceDeclaration() { |
287 | return Optional.of(this); |
288 | } |
289 | } |
290 |
Members