/
ClassOrInterfaceDeclaration.java
262 lines (231 loc) · 10.8 KB
/
ClassOrInterfaceDeclaration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.ast.body;
import com.github.javaparser.Range;
import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithExtends;
import com.github.javaparser.ast.nodeTypes.NodeWithImplements;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
import com.github.javaparser.ast.nodeTypes.modifiers.*;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.util.*;
import static com.github.javaparser.utils.Utils.assertNotNull;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.ClassOrInterfaceDeclarationMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
/**
* A definition of a class or interface.<br/><code>class X { ... }</code>
*
* @author Julio Vilmar Gesser
*/
public final class ClassOrInterfaceDeclaration extends TypeDeclaration<ClassOrInterfaceDeclaration> implements NodeWithImplements<ClassOrInterfaceDeclaration>, NodeWithExtends<ClassOrInterfaceDeclaration>, NodeWithTypeParameters<ClassOrInterfaceDeclaration>, NodeWithAbstractModifier<ClassOrInterfaceDeclaration>, NodeWithFinalModifier<ClassOrInterfaceDeclaration> {
private boolean isInterface;
private NodeList<TypeParameter> typeParameters;
// Can contain more than one item if this is an interface
private NodeList<ClassOrInterfaceType> extendedTypes;
private NodeList<ClassOrInterfaceType> implementedTypes;
public ClassOrInterfaceDeclaration() {
this(null, EnumSet.noneOf(Modifier.class), new NodeList<>(), false, new SimpleName(), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>());
}
public ClassOrInterfaceDeclaration(final EnumSet<Modifier> modifiers, final boolean isInterface, final String name) {
this(null, modifiers, new NodeList<>(), isInterface, new SimpleName(name), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>());
}
@AllFieldsConstructor
public ClassOrInterfaceDeclaration(final EnumSet<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) {
this(null, modifiers, annotations, isInterface, name, typeParameters, extendedTypes, implementedTypes, members);
}
public ClassOrInterfaceDeclaration(Range range, final EnumSet<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) {
super(range, annotations, modifiers, name, members);
setInterface(isInterface);
setTypeParameters(typeParameters);
setExtendedTypes(extendedTypes);
setImplementedTypes(implementedTypes);
}
@Override
public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
return v.visit(this, arg);
}
@Override
public <A> void accept(final VoidVisitor<A> v, final A arg) {
v.visit(this, arg);
}
@Override
public NodeList<ClassOrInterfaceType> getExtendedTypes() {
return extendedTypes;
}
@Override
public NodeList<ClassOrInterfaceType> getImplementedTypes() {
return implementedTypes;
}
public NodeList<TypeParameter> getTypeParameters() {
return typeParameters;
}
public boolean isInterface() {
return isInterface;
}
@Override
public ClassOrInterfaceDeclaration setExtendedTypes(final NodeList<ClassOrInterfaceType> extendedTypes) {
assertNotNull(extendedTypes);
notifyPropertyChange(ObservableProperty.EXTENDED_TYPES, this.extendedTypes, extendedTypes);
if (this.extendedTypes != null)
this.extendedTypes.setParentNode(null);
this.extendedTypes = extendedTypes;
setAsParentNodeOf(extendedTypes);
return this;
}
@Override
public ClassOrInterfaceDeclaration setImplementedTypes(final NodeList<ClassOrInterfaceType> implementedTypes) {
assertNotNull(implementedTypes);
notifyPropertyChange(ObservableProperty.IMPLEMENTED_TYPES, this.implementedTypes, implementedTypes);
if (this.implementedTypes != null)
this.implementedTypes.setParentNode(null);
this.implementedTypes = implementedTypes;
setAsParentNodeOf(implementedTypes);
return this;
}
public ClassOrInterfaceDeclaration setInterface(final boolean isInterface) {
notifyPropertyChange(ObservableProperty.INTERFACE, this.isInterface, isInterface);
this.isInterface = isInterface;
return this;
}
@Override
public ClassOrInterfaceDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) {
assertNotNull(typeParameters);
notifyPropertyChange(ObservableProperty.TYPE_PARAMETERS, this.typeParameters, typeParameters);
if (this.typeParameters != null)
this.typeParameters.setParentNode(null);
this.typeParameters = typeParameters;
setAsParentNodeOf(typeParameters);
return this;
}
/**
* Try to find a {@link ConstructorDeclaration} with no parameters by its name
*
* @return the methods found (multiple in case of polymorphism)
*/
public Optional<ConstructorDeclaration> getDefaultConstructor() {
return getMembers().stream().filter(bd -> bd instanceof ConstructorDeclaration).map(bd -> (ConstructorDeclaration) bd).filter(cd -> cd.getParameters().isEmpty()).findFirst();
}
/**
* Adds a constructor to this
*
* @param modifiers the modifiers like {@link Modifier#PUBLIC}
* @return the {@link MethodDeclaration} created
*/
public ConstructorDeclaration addConstructor(Modifier... modifiers) {
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration();
constructorDeclaration.setModifiers(Arrays.stream(modifiers)
.collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
constructorDeclaration.setName(getName());
getMembers().add(constructorDeclaration);
return constructorDeclaration;
}
/**
* Find all constructors for this class.
*
* @return the constructors found. This list is immutable.
*/
public List<ConstructorDeclaration> getConstructors() {
return unmodifiableList(getMembers().stream()
.filter(m -> m instanceof ConstructorDeclaration)
.map(m -> (ConstructorDeclaration) m)
.collect(toList()));
}
/**
* Try to find a {@link MethodDeclaration} by its parameters types
*
* @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
* foo(Map<Integer,String> myMap,int number)
* @return the methods found (multiple in case of overloading)
*/
public Optional<ConstructorDeclaration> getConstructorByParameterTypes(String... paramTypes) {
return getConstructors().stream()
.filter(m -> m.hasParametersOfType(paramTypes))
.findFirst();
}
/**
* Try to find a {@link MethodDeclaration} by its parameters types
*
* @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
* foo(Map<Integer,String> myMap,int number)
* @return the methods found (multiple in case of overloading)
*/
public Optional<ConstructorDeclaration> getConstructorByParameterTypes(Class<?>... paramTypes) {
return getConstructors().stream()
.filter(m -> m.hasParametersOfType(paramTypes))
.findFirst();
}
@Override
public List<NodeList<?>> getNodeLists() {
return Arrays.asList(getExtendedTypes(), getImplementedTypes(), getTypeParameters(), getMembers(), getAnnotations());
}
@Override
public boolean remove(Node node) {
if (node == null)
return false;
for (int i = 0; i < extendedTypes.size(); i++) {
if (extendedTypes.get(i) == node) {
extendedTypes.remove(i);
return true;
}
}
for (int i = 0; i < implementedTypes.size(); i++) {
if (implementedTypes.get(i) == node) {
implementedTypes.remove(i);
return true;
}
}
for (int i = 0; i < typeParameters.size(); i++) {
if (typeParameters.get(i) == node) {
typeParameters.remove(i);
return true;
}
}
return super.remove(node);
}
/**
* @return is this class's parent a LocalClassDeclarationStmt ?
*/
public boolean isLocalClassDeclaration() {
return getParentNode().map(p -> p instanceof LocalClassDeclarationStmt).orElse(false);
}
@Override
public ClassOrInterfaceDeclaration clone() {
return (ClassOrInterfaceDeclaration) accept(new CloneVisitor(), null);
}
@Override
public ClassOrInterfaceDeclarationMetaModel getMetaModel() {
return JavaParserMetaModel.classOrInterfaceDeclarationMetaModel;
}
}