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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import lombok.core.AST.Kind;
/**
* An instance of this class wraps an Eclipse/javac internal node object.
*
* @param A Type of our owning AST.
* @param L self-type.
* @param N The common type of all AST nodes in the internal representation of the target platform.
* For example, JCTree for javac, and ASTNode for Eclipse.
*/
public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A, L, N>, N> {
protected final A ast;
protected final Kind kind;
protected final N node;
protected final List<L> children;
protected L parent;
/** This flag has no specified meaning; you can set and retrieve it.
*
* In practice, for annotation nodes it means: Some AnnotationHandler finished whatever changes were required,
* and for all other nodes it means: This node was made by a lombok operation.
*/
protected boolean handled;
/** structurally significant are those nodes that can be annotated in java 1.6 or are method-like toplevels,
* so fields, local declarations, method arguments, methods, types, the Compilation Unit itself, and initializers. */
protected boolean isStructurallySignificant;
/**
* Creates a new Node object that represents the provided node.
*
* @param ast The owning AST - this node is part of this AST's tree of nodes.
* @param node The AST object in the target parser's own internal AST tree that this node object will represent.
* @param children A list of child nodes. Passing in null results in the children list being empty, not null.
* @param kind The kind of node represented by this object.
*/
@SuppressWarnings("unchecked")
protected LombokNode(A ast, N node, List<L> children, Kind kind) {
this.ast = ast;
this.kind = kind;
this.node = node;
this.children = children == null ? new ArrayList<L>() : children;
for (L child : this.children) {
child.parent = (L) this;
if (!child.isStructurallySignificant)
child.isStructurallySignificant = calculateIsStructurallySignificant(node);
}
this.isStructurallySignificant = calculateIsStructurallySignificant(null);
}
/** {@inheritDoc} */
@Override public String toString() {
return String.format("NODE %s (%s) %s%s",
kind, node == null ? "(NULL)" : node.getClass(), handled ? "[HANDLED]" : "", node == null ? "" : node);
}
/**
* Convenient shortcut to the owning ast object's {@code getPackageDeclaration} method.
*
* @see AST#getPackageDeclaration()
*/
public String getPackageDeclaration() {
return ast.getPackageDeclaration();
}
/**
* Convenient shortcut to the owning ast object's {@code getImportStatements} method.
*
* @see AST#getImportStatements()
*/
public Collection<String> getImportStatements() {
return ast.getImportStatements();
}
/**
* See {@link #isStructurallySignificant}.
*/
protected abstract boolean calculateIsStructurallySignificant(N parent);
/**
* Convenient shortcut to the owning ast object's get method.
*
* @see AST#get(Object)
*/
public L getNodeFor(N obj) {
return ast.get(obj);
}
/**
* @return The javac/Eclipse internal AST object wrapped by this LombokNode object.
*/
public N get() {
return node;
}
/**
* Replaces the AST node represented by this node object with the provided node. This node must
* have a parent, obviously, for this to work.
*
* Also affects the underlying (Eclipse/javac) AST.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public L replaceWith(N newN, Kind newNodeKind) {
ast.setChanged();
L newNode = ast.buildTree(newN, newNodeKind);
newNode.parent = parent;
for (int i = 0; i < parent.children.size(); i++) {
if (parent.children.get(i) == this) ((List)parent.children).set(i, newNode);
}
parent.replaceChildNode(get(), newN);
return newNode;
}
/**
* Replaces the stated node with a new one. The old node must be a direct child of this node.
*
* Also affects the underlying (Eclipse/javac) AST.
*/
public void replaceChildNode(N oldN, N newN) {
ast.setChanged();
ast.replaceStatementInNode(get(), oldN, newN);
}
public Kind getKind() {
return kind;
}
/**
* Return the name of your type (simple name), method, field, or local variable. Return null if this
* node doesn't really have a name, such as initializers, while statements, etc.
*/
public abstract String getName();
/** Returns the structurally significant node that encloses this one.
*
* @see #isStructurallySignificant()
*/
public L up() {
L result = parent;
while (result != null && !result.isStructurallySignificant) result = result.parent;
return result;
}
/**
* {@code @Foo int x, y;} is stored in both javac and ecj as 2 FieldDeclarations, both with the same annotation as child.
* The normal {@code up()} method can't handle having multiple parents, but this one can.
*/
public Collection<L> upFromAnnotationToFields() {
if (getKind() != Kind.ANNOTATION) return Collections.emptyList();
L field = up();
if (field == null || field.getKind() != Kind.FIELD) return Collections.emptyList();
L type = field.up();
if (type == null || type.getKind() != Kind.TYPE) return Collections.emptyList();
List<L> fields = new ArrayList<L>();
for (L potentialField : type.down()) {
if (potentialField.getKind() != Kind.FIELD) continue;
if (fieldContainsAnnotation(potentialField.get(), get())) fields.add(potentialField);
}
return fields;
}
/**
* Return {@code true} if the annotation is attached to the field.
*/
protected abstract boolean fieldContainsAnnotation(N field, N annotation);
/**
* Returns the direct parent node in the AST tree of this node. For example, a local variable declaration's
* direct parent can be e.g. an If block, but its {@code up()} {@code LombokNode} is the {@code Method} that contains it.
*/
public L directUp() {
return parent;
}
/**
* Returns all children nodes.
*
* A copy is created, so changing the list has no effect. Also, while iterating through this list,
* you may add, remove, or replace children without causing {@code ConcurrentModificationException}s.
*/
public Collection<L> down() {
return new ArrayList<L>(children);
}
/**
* returns the value of the 'handled' flag.
*
* @see #handled
*/
public boolean isHandled() {
return handled;
}
/**
* Sets the handled flag, then returns itself for chaining.
*
* @see #handled
*/
@SuppressWarnings("unchecked")
public L setHandled() {
this.handled = true;
return (L)this;
}
/**
* Convenient shortcut to the owning ast object's top method.
*
* @see AST#top()
*/
public L top() {
return ast.top();
}
/**
* Convenient shortcut to the owning ast object's getFileName method.
*
* @see AST#getFileName()
*/
public String getFileName() {
return ast.getFileName();
}
/**
* Adds the stated node as a direct child of this node.
*
* Does not change the underlying (javac/Eclipse) AST, only the wrapped view.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public L add(N newChild, Kind newChildKind) {
ast.setChanged();
L n = ast.buildTree(newChild, newChildKind);
if (n == null) return null;
n.parent = (L) this;
((List)children).add(n);
return n;
}
/**
* Reparses the AST node represented by this node. Any existing nodes that occupy a different space in the AST are rehomed, any
* nodes that no longer exist are removed, and new nodes are created.
*
* Careful - the node you call this on must not itself have been removed or rehomed - it rebuilds <i>all children</i>.
*/
public void rebuild() {
Map<N, L> oldNodes = new IdentityHashMap<N, L>();
gatherAndRemoveChildren(oldNodes);
L newNode = ast.buildTree(get(), kind);
ast.setChanged();
ast.replaceNewWithExistingOld(oldNodes, newNode);
}
@SuppressWarnings("unchecked")
private void gatherAndRemoveChildren(Map<N, L> map) {
for (L child : children) child.gatherAndRemoveChildren(map);
ast.identityDetector.remove(get());
map.put(get(), (L) this);
children.clear();
ast.getNodeMap().remove(get());
}
/**
* Removes the stated node, which must be a direct child of this node, from the AST.
*
* Does not change the underlying (javac/Eclipse) AST, only the wrapped view.
*/
public void removeChild(L child) {
ast.setChanged();
children.remove(child);
}
/**
* Sets the handled flag on this node, and all child nodes, then returns itself, for chaining.
*
* @see #handled
*/
@SuppressWarnings("unchecked")
public L recursiveSetHandled() {
this.handled = true;
for (L child : children) child.recursiveSetHandled();
return (L) this;
}
/** Generate a compiler error on this node. */
public abstract void addError(String message);
/** Generate a compiler warning on this node. */
public abstract void addWarning(String message);
/**
* Structurally significant means: LocalDeclaration, TypeDeclaration, MethodDeclaration, ConstructorDeclaration,
* FieldDeclaration, Initializer, and CompilationUnitDeclaration.
* The rest is e.g. if statements, while loops, etc.
*/
public boolean isStructurallySignificant() {
return isStructurallySignificant;
}
}
|