1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2022 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.lang.reflect.Constructor; |
18 | import java.lang.reflect.InvocationTargetException; |
19 | import java.util.ArrayList; |
20 | import java.util.Collections; |
21 | import java.util.HashMap; |
22 | import java.util.List; |
23 | import java.util.Map; |
24 | import java.util.StringTokenizer; |
25 | |
26 | import org.eclipse.core.runtime.IProgressMonitor; |
27 | import org.eclipse.jdt.core.IClassFile; |
28 | import org.eclipse.jdt.core.ICompilationUnit; |
29 | import org.eclipse.jdt.core.IJavaProject; |
30 | import org.eclipse.jdt.core.JavaCore; |
31 | import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
32 | import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
33 | import org.eclipse.jdt.internal.compiler.parser.Scanner; |
34 | |
35 | /** |
36 | * Umbrella owner and abstract syntax tree node factory. |
37 | * An <code>AST</code> instance serves as the common owner of any number of |
38 | * AST nodes, and as the factory for creating new AST nodes owned by that |
39 | * instance. |
40 | * <p> |
41 | * Abstract syntax trees may be hand constructed by clients, using the |
42 | * <code>new<i>TYPE</i></code> factory methods to create new nodes, and the |
43 | * various <code>set<i>CHILD</i></code> methods |
44 | * (see {@link org.eclipse.jdt.core.dom.ASTNode ASTNode} and its subclasses) |
45 | * to connect them together. |
46 | * </p> |
47 | * <p> |
48 | * Each AST node belongs to a unique AST instance, called the owning AST. |
49 | * The children of an AST node always have the same owner as their parent node. |
50 | * If a node from one AST is to be added to a different AST, the subtree must |
51 | * be cloned first to ensures that the added nodes have the correct owning AST. |
52 | * </p> |
53 | * <p> |
54 | * There can be any number of AST nodes owned by a single AST instance that are |
55 | * unparented. Each of these nodes is the root of a separate little tree of |
56 | * nodes. |
57 | * The method <code>ASTNode.getRoot()</code> navigates from any node to the root |
58 | * of the tree that it is contained in. Ordinarily, an AST instance has one main |
59 | * tree (rooted at a <code>CompilationUnit</code>), with newly-created nodes |
60 | * appearing |
61 | * as additional roots until they are parented somewhere under the main tree. |
62 | * One can navigate from any node to its AST instance, but not conversely. |
63 | * </p> |
64 | * <p> |
65 | * The class {@link ASTParser} parses a string |
66 | * containing a Java source code and returns an abstract syntax tree |
67 | * for it. The resulting nodes carry source ranges relating the node back to |
68 | * the original source characters. |
69 | * </p> |
70 | * <p> |
71 | * Compilation units created by <code>ASTParser</code> from a |
72 | * source document can be serialized after arbitrary modifications |
73 | * with minimal loss of original formatting. Here is an example: |
74 | * </p> |
75 | * |
76 | * <pre> |
77 | * Document doc = new Document("import java.util.List;\nclass X {}\n"); |
78 | * ASTParser parser = ASTParser.newParser(AST.JLS3); |
79 | * parser.setSource(doc.get().toCharArray()); |
80 | * CompilationUnit cu = (CompilationUnit) parser.createAST(null); |
81 | * cu.recordModifications(); |
82 | * AST ast = cu.getAST(); |
83 | * ImportDeclaration id = ast.newImportDeclaration(); |
84 | * id.setName(ast.newName(new String[] {"java", "util", "Set"}); |
85 | * cu.imports().add(id); // add import declaration at end |
86 | * TextEdit edits = cu.rewrite(document, null); |
87 | * UndoEdit undo = edits.apply(document); |
88 | * </pre> |
89 | * <p> |
90 | * See also {@link org.eclipse.jdt.core.dom.rewrite.ASTRewrite} for |
91 | * an alternative way to describe and serialize changes to a |
92 | * read-only AST. |
93 | * </p> |
94 | * <p> |
95 | * Clients may create instances of this class using |
96 | * {@link #newAST(int, boolean)}, |
97 | * but this class is not intended to be subclassed. |
98 | * </p> |
99 | * |
100 | * @see ASTParser |
101 | * @see ASTNode |
102 | * @since 2.0 |
103 | * @noinstantiate This class is not intended to be instantiated by clients. |
104 | */ |
105 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
106 | public final class AST { |
107 | /** |
108 | * new Class[] {AST.class} |
109 | * |
110 | * @since 3.0 |
111 | */ |
112 | private static final Class[] AST_CLASS = new Class[] { AST.class }; |
113 | |
114 | /** |
115 | * Constant for indicating the AST API that handles JLS2. |
116 | * <p> |
117 | * This API is capable of handling all constructs |
118 | * in the Java language as described in the Java Language |
119 | * Specification, Second Edition (JLS2). |
120 | * JLS2 is a superset of all earlier versions of the |
121 | * Java language, and the JLS2 API can be used to manipulate |
122 | * programs written in all versions of the Java language |
123 | * up to and including J2SE 1.4. |
124 | * </p> |
125 | * |
126 | * @since 3.0 |
127 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
128 | */ |
129 | public static final int JLS2 = 2; |
130 | |
131 | /** |
132 | * Internal synonym for {@link #JLS2}. Use to alleviate |
133 | * deprecation warnings. |
134 | * |
135 | * @since 3.1 |
136 | */ |
137 | /* package */ static final int JLS2_INTERNAL = JLS2; |
138 | |
139 | /** |
140 | * Constant for indicating the AST API that handles JLS3. |
141 | * <p> |
142 | * This API is capable of handling all constructs in the |
143 | * Java language as described in the Java Language |
144 | * Specification, Third Edition (JLS3). |
145 | * JLS3 is a superset of all earlier versions of the |
146 | * Java language, and the JLS3 API can be used to manipulate |
147 | * programs written in all versions of the Java language |
148 | * up to and including J2SE 5 (aka JDK 1.5). |
149 | * </p> |
150 | * |
151 | * @since 3.1 |
152 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
153 | */ |
154 | public static final int JLS3 = 3; |
155 | |
156 | /** |
157 | * Internal synonym for {@link #JLS3}. Use to alleviate |
158 | * deprecation warnings. |
159 | * |
160 | * @since 3.8 |
161 | */ |
162 | /* package */ static final int JLS3_INTERNAL = JLS3; |
163 | |
164 | /** |
165 | * Constant for indicating the AST API that handles JLS4 (aka JLS7). |
166 | * <p> |
167 | * This API is capable of handling all constructs in the |
168 | * Java language as described in the Java Language |
169 | * Specification, Java SE 7 Edition (JLS7) as specified by JSR336. |
170 | * JLS4 is a superset of all earlier versions of the |
171 | * Java language, and the JLS4 API can be used to manipulate |
172 | * programs written in all versions of the Java language |
173 | * up to and including Java SE 7 (aka JDK 1.7). |
174 | * </p> |
175 | * |
176 | * @since 3.7.1 |
177 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
178 | */ |
179 | public static final int JLS4 = 4; |
180 | |
181 | /** |
182 | * Internal synonym for {@link #JLS4}. Use to alleviate |
183 | * deprecation warnings. |
184 | * |
185 | * @since 3.10 |
186 | */ |
187 | /* package */ static final int JLS4_INTERNAL = JLS4; |
188 | |
189 | /** |
190 | * Constant for indicating the AST API that handles JLS8. |
191 | * <p> |
192 | * This API is capable of handling all constructs in the |
193 | * Java language as described in the Java Language |
194 | * Specification, Java SE 8 Edition (JLS8) as specified by JSR337. |
195 | * JLS8 is a superset of all earlier versions of the |
196 | * Java language, and the JLS8 API can be used to manipulate |
197 | * programs written in all versions of the Java language |
198 | * up to and including Java SE 8 (aka JDK 1.8). |
199 | * </p> |
200 | * |
201 | * @since 3.10 |
202 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
203 | */ |
204 | public static final int JLS8 = 8; |
205 | |
206 | /** |
207 | * Internal synonym for {@link #JLS8}. Use to alleviate |
208 | * deprecation warnings. |
209 | * |
210 | * @since 3.14 |
211 | */ |
212 | /* package */ static final int JLS8_INTERNAL = JLS8; |
213 | |
214 | /** |
215 | * Constant for indicating the AST API that handles JLS9. |
216 | * <p> |
217 | * This API is capable of handling all constructs in the |
218 | * Java language as described in the Java Language |
219 | * Specification, Java SE 9 Edition (JLS9). |
220 | * JLS9 is a superset of all earlier versions of the |
221 | * Java language, and the JLS9 API can be used to manipulate |
222 | * programs written in all versions of the Java language |
223 | * up to and including Java SE 9 (aka JDK 9). |
224 | * </p> |
225 | * |
226 | * @since 3.14 |
227 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
228 | */ |
229 | public static final int JLS9 = 9; |
230 | |
231 | /** |
232 | * Internal synonym for {@link #JLS9}. Use to alleviate |
233 | * deprecation warnings once JLS9 is deprecated |
234 | * |
235 | * @since 3.14 |
236 | */ |
237 | /* package */ static final int JLS9_INTERNAL = JLS9; |
238 | |
239 | /** |
240 | * Constant for indicating the AST API that handles JLS10. |
241 | * <p> |
242 | * This API is capable of handling all constructs in the |
243 | * Java language as described in the Java Language |
244 | * Specification, Java SE 10 Edition (JLS10). |
245 | * JLS10 is a superset of all earlier versions of the |
246 | * Java language, and the JLS10 API can be used to manipulate |
247 | * programs written in all versions of the Java language |
248 | * up to and including Java SE 10 (aka JDK 10). |
249 | * </p> |
250 | * |
251 | * @since 3.14 |
252 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
253 | */ |
254 | public static final int JLS10 = 10; |
255 | |
256 | /** |
257 | * Internal synonym for {@link #JLS10}. Use to alleviate |
258 | * deprecation warnings once JLS10 is deprecated |
259 | * |
260 | * @since 3.14 |
261 | */ |
262 | /* package */ static final int JLS10_INTERNAL = JLS10; |
263 | |
264 | /** |
265 | * Constant for indicating the AST API that handles JLS11. |
266 | * <p> |
267 | * This API is capable of handling all constructs in the |
268 | * Java language as described in the Java Language |
269 | * Specification, Java SE 11 Edition (JLS11). |
270 | * JLS11 is a superset of all earlier versions of the |
271 | * Java language, and the JLS11 API can be used to manipulate |
272 | * programs written in all versions of the Java language |
273 | * up to and including Java SE 11 (aka JDK 11). |
274 | * </p> |
275 | * |
276 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
277 | * @since 3.16 |
278 | */ |
279 | public static final int JLS11 = 11; |
280 | |
281 | /** |
282 | * Internal synonym for {@link #JLS11}. Use to alleviate |
283 | * deprecation warnings once JLS11 is deprecated |
284 | * |
285 | * @since 3.14 |
286 | */ |
287 | /* package */ static final int JLS11_INTERNAL = JLS11; |
288 | |
289 | /** |
290 | * Constant for indicating the AST API that handles JLS12. |
291 | * <p> |
292 | * This API is capable of handling all constructs in the |
293 | * Java language as described in the Java Language |
294 | * Specification, Java SE 12 Edition (JLS12). |
295 | * JLS12 is a superset of all earlier versions of the |
296 | * Java language, and the JLS12 API can be used to manipulate |
297 | * programs written in all versions of the Java language |
298 | * up to and including Java SE 12 (aka JDK 12). |
299 | * </p> |
300 | * |
301 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
302 | * @since 3.18 |
303 | */ |
304 | public static final int JLS12 = 12; |
305 | /** |
306 | * Internal synonym for {@link #JLS12}. Use to alleviate |
307 | * deprecation warnings once JLS12 is deprecated |
308 | * |
309 | * @since 3.18 |
310 | */ |
311 | static final int JLS12_INTERNAL = JLS12; |
312 | |
313 | /** |
314 | * Constant for indicating the AST API that handles JLS13. |
315 | * <p> |
316 | * This API is capable of handling all constructs in the |
317 | * Java language as described in the Java Language |
318 | * Specification, Java SE 13 Edition (JLS13). |
319 | * JLS13 is a superset of all earlier versions of the |
320 | * Java language, and the JLS13 API can be used to manipulate |
321 | * programs written in all versions of the Java language |
322 | * up to and including Java SE 13 (aka JDK 13). |
323 | * </p> |
324 | * |
325 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
326 | * @since 3.20 |
327 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
328 | */ |
329 | public static final int JLS13 = 13; |
330 | |
331 | /** |
332 | * Internal synonym for {@link #JLS13}. Use to alleviate |
333 | * deprecation warnings once JLS13 is deprecated |
334 | * |
335 | * @since 3.20 |
336 | */ |
337 | static final int JLS13_INTERNAL = JLS13; |
338 | |
339 | /** |
340 | * Constant for indicating the AST API that handles JLS14. |
341 | * <p> |
342 | * This API is capable of handling all constructs in the |
343 | * Java language as described in the Java Language |
344 | * Specification, Java SE 14 Edition (JLS14). |
345 | * JLS14 is a superset of all earlier versions of the |
346 | * Java language, and the JLS14 API can be used to manipulate |
347 | * programs written in all versions of the Java language |
348 | * up to and including Java SE 14(aka JDK 14). |
349 | * </p> |
350 | * |
351 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
352 | * @since 3.22 |
353 | */ |
354 | public static final int JLS14 = 14; |
355 | |
356 | /** |
357 | * Internal synonym for {@link #JLS14}. Use to alleviate |
358 | * deprecation warnings once JLS14 is deprecated |
359 | * |
360 | * @since 3.22 |
361 | */ |
362 | static final int JLS14_INTERNAL = JLS14; |
363 | |
364 | /** |
365 | * Constant for indicating the AST API that handles JLS15. |
366 | * <p> |
367 | * This API is capable of handling all constructs in the |
368 | * Java language as described in the Java Language |
369 | * Specification, Java SE 15 Edition (JLS15). |
370 | * JLS15 is a superset of all earlier versions of the |
371 | * Java language, and the JLS15 API can be used to manipulate |
372 | * programs written in all versions of the Java language |
373 | * up to and including Java SE 15(aka JDK 15). |
374 | * </p> |
375 | * |
376 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
377 | * @since 3.24 |
378 | */ |
379 | public static final int JLS15 = 15; |
380 | /** |
381 | * Constant for indicating the AST API that handles JLS16. |
382 | * <p> |
383 | * This API is capable of handling all constructs in the |
384 | * Java language as described in the Java Language |
385 | * Specification, Java SE 16 Edition (JLS16). |
386 | * JLS16 is a superset of all earlier versions of the |
387 | * Java language, and the JLS16 API can be used to manipulate |
388 | * programs written in all versions of the Java language |
389 | * up to and including Java SE 16(aka JDK 16). |
390 | * </p> |
391 | * |
392 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
393 | * @since 3.26 |
394 | */ |
395 | public static final int JLS16 = 16; |
396 | /** |
397 | * Constant for indicating the AST API that handles JLS17. |
398 | * <p> |
399 | * This API is capable of handling all constructs in the |
400 | * Java language as described in the Java Language |
401 | * Specification, Java SE 17 Edition (JLS17). |
402 | * JLS17 is a superset of all earlier versions of the |
403 | * Java language, and the JLS17 API can be used to manipulate |
404 | * programs written in all versions of the Java language |
405 | * up to and including Java SE 17(aka JDK 17). |
406 | * </p> |
407 | * |
408 | * @deprecated Clients should use the {@link #getJLSLatest()} AST API instead. |
409 | * @since 3.28 |
410 | */ |
411 | public static final int JLS17 = 17; |
412 | /** |
413 | * Constant for indicating the AST API that handles JLS17. |
414 | * <p> |
415 | * This API is capable of handling all constructs in the |
416 | * Java language as described in the Java Language |
417 | * Specification, Java SE 18 Edition (JLS18). |
418 | * JLS18 is a superset of all earlier versions of the |
419 | * Java language, and the JLS18 API can be used to manipulate |
420 | * programs written in all versions of the Java language |
421 | * up to and including Java SE 18(aka JDK 18). |
422 | * </p> |
423 | * |
424 | * @since 3.30 |
425 | */ |
426 | public static final int JLS18 = 18; |
427 | |
428 | /** |
429 | * Internal synonym for {@link #JLS15}. Use to alleviate |
430 | * deprecation warnings once JLS15 is deprecated |
431 | */ |
432 | static final int JLS15_INTERNAL = JLS15; |
433 | /** |
434 | * Internal synonym for {@link #JLS16}. Use to alleviate |
435 | * deprecation warnings once JLS16 is deprecated |
436 | */ |
437 | static final int JLS16_INTERNAL = JLS16; |
438 | /** |
439 | * Internal synonym for {@link #JLS17}. Use to alleviate |
440 | * deprecation warnings once JLS17 is deprecated |
441 | */ |
442 | static final int JLS17_INTERNAL = JLS17; |
443 | /** |
444 | * Internal synonym for {@link #JLS18}. Use to alleviate |
445 | * deprecation warnings once JLS18 is deprecated |
446 | */ |
447 | static final int JLS18_INTERNAL = JLS18; |
448 | /** |
449 | * Internal property for latest supported JLS level |
450 | * This provides the latest JLS level. |
451 | */ |
452 | private static final int JLS_INTERNAL_Latest = JLS18; |
453 | |
454 | /** |
455 | * @since 3.26 |
456 | * This provides the latest JLS level. |
457 | * @deprecated use {@link #getJLSLatest()} |
458 | */ |
459 | public static final int JLS_Latest = JLS18; |
460 | |
461 | /* |
462 | * Must not collide with a value for ICompilationUnit constants |
463 | */ |
464 | static final int RESOLVED_BINDINGS = 0x80000000; |
465 | |
466 | private static Map<String, Long> jdkLevelMap = getLevelMapTable(); |
467 | |
468 | private static Map<String, Integer> apiLevelMap = getApiLevelMapTable(); |
469 | |
470 | /** |
471 | * Internal method. |
472 | * <p> |
473 | * This method converts the given internal compiler AST for the given source |
474 | * string |
475 | * into a compilation unit. This method is not intended to be called by clients. |
476 | * </p> |
477 | * |
478 | * @param level the API level; one of the <code>JLS*</code> |
479 | * level constants |
480 | * @param compilationUnitDeclaration an internal AST node for a compilation unit |
481 | * declaration |
482 | * @param source the string of the Java compilation unit |
483 | * @param options compiler options |
484 | * @param workingCopy the working copy that the AST is created |
485 | * from |
486 | * @param monitor the progress monitor used to report |
487 | * progress and request cancellation, |
488 | * or <code>null</code> if none |
489 | * @param isResolved whether the given compilation unit |
490 | * declaration is resolved |
491 | * @return the compilation unit node |
492 | * @deprecated Use org.eclipse.jdt.core.dom.AST.convertCompilationUnit(int, |
493 | * CompilationUnitDeclaration, Map, boolean, CompilationUnit, int, |
494 | * IProgressMonitor) instead |
495 | * @noreference This method is not intended to be referenced by clients. |
496 | */ |
497 | public static CompilationUnit convertCompilationUnit( |
498 | int level, |
499 | org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, |
500 | char[] source, |
501 | Map options, |
502 | boolean isResolved, |
503 | org.eclipse.jdt.internal.core.CompilationUnit workingCopy, |
504 | int reconcileFlags, |
505 | IProgressMonitor monitor) { |
506 | return null; |
507 | } |
508 | |
509 | /** |
510 | * Internal method. |
511 | * <p> |
512 | * This method converts the given internal compiler AST for the given source |
513 | * string |
514 | * into a compilation unit. This method is not intended to be called by clients. |
515 | * </p> |
516 | * |
517 | * @param level the API level; one of the <code>JLS*</code> |
518 | * level constants |
519 | * @param compilationUnitDeclaration an internal AST node for a compilation unit |
520 | * declaration |
521 | * @param options compiler options |
522 | * @param workingCopy the working copy that the AST is created |
523 | * from |
524 | * @param monitor the progress monitor used to report |
525 | * progress and request cancellation, |
526 | * or <code>null</code> if none |
527 | * @param isResolved whether the given compilation unit |
528 | * declaration is resolved |
529 | * @return the compilation unit node |
530 | * @since 3.4 |
531 | * @noreference This method is not intended to be referenced by clients. |
532 | */ |
533 | public static CompilationUnit convertCompilationUnit( |
534 | int level, |
535 | org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, |
536 | Map options, |
537 | boolean isResolved, |
538 | org.eclipse.jdt.internal.core.CompilationUnit workingCopy, |
539 | int reconcileFlags, |
540 | IProgressMonitor monitor) { |
541 | |
542 | ASTConverter converter = new ASTConverter(options, isResolved, monitor); |
543 | AST ast = AST.newAST(level, JavaCore.ENABLED.equals(options.get(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES))); |
544 | String sourceModeSetting = (String) options.get(JavaCore.COMPILER_SOURCE); |
545 | long sourceLevel = CompilerOptions.versionToJdkLevel(sourceModeSetting); |
546 | if (sourceLevel == 0) { |
547 | // unknown sourceModeSetting |
548 | sourceLevel = ClassFileConstants.JDK1_3; |
549 | } |
550 | ast.scanner.sourceLevel = sourceLevel; |
551 | String compliance = (String) options.get(JavaCore.COMPILER_COMPLIANCE); |
552 | long complianceLevel = CompilerOptions.versionToJdkLevel(compliance); |
553 | if (complianceLevel == 0) { |
554 | // unknown sourceModeSetting |
555 | complianceLevel = sourceLevel; |
556 | } |
557 | ast.scanner.complianceLevel = complianceLevel; |
558 | ast.scanner.previewEnabled = JavaCore.ENABLED.equals(options.get(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES)); |
559 | int savedDefaultNodeFlag = ast.getDefaultNodeFlag(); |
560 | ast.setDefaultNodeFlag(ASTNode.ORIGINAL); |
561 | BindingResolver resolver = null; |
562 | if (isResolved) { |
563 | resolver = new DefaultBindingResolver(compilationUnitDeclaration.scope, workingCopy.owner, |
564 | new DefaultBindingResolver.BindingTables(), false, true); |
565 | ((DefaultBindingResolver) resolver).isRecoveringBindings = (reconcileFlags |
566 | & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0; |
567 | ast.setFlag(AST.RESOLVED_BINDINGS); |
568 | } else { |
569 | resolver = new BindingResolver(); |
570 | } |
571 | ast.setFlag(reconcileFlags); |
572 | ast.setBindingResolver(resolver); |
573 | converter.setAST(ast); |
574 | |
575 | CompilationUnit unit = converter.convert(compilationUnitDeclaration, workingCopy.getContents()); |
576 | unit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions()); |
577 | unit.setTypeRoot(workingCopy.originalFromClone()); |
578 | ast.setDefaultNodeFlag(savedDefaultNodeFlag); |
579 | return unit; |
580 | } |
581 | |
582 | /** |
583 | * Creates a new Java abstract syntax tree |
584 | * (AST) following the specified set of API rules. |
585 | * <p> |
586 | * Clients should use this method specifying {@link #JLS12} as the |
587 | * AST level in all cases, even when dealing with source of earlier JDK versions |
588 | * like 1.3 or 1.4. |
589 | * </p> |
590 | * |
591 | * @param level the API level; one of the <code>JLS*</code> level constants |
592 | * @return new AST instance following the specified set of API rules. |
593 | * @exception IllegalArgumentException if: |
594 | * <ul> |
595 | * <li>the API level is not one of the |
596 | * <code>JLS*</code> level constants</li> |
597 | * </ul> |
598 | * @deprecated Clients should port their code to use the latest JLS* AST API and |
599 | * call |
600 | * {@link #newAST(int, boolean) AST.newAST(AST.JLS12, false)} |
601 | * instead of using this constructor. |
602 | * @since 3.0 |
603 | */ |
604 | public static AST newAST(int level) { |
605 | return new AST(level, false); |
606 | } |
607 | |
608 | /** |
609 | * Creates a new Java abstract syntax tree |
610 | * (AST) following the specified set of API rules. |
611 | * <p> |
612 | * Clients should use this method specifying {@link #JLS12} as the |
613 | * AST level in all cases, even when dealing with source of earlier JDK versions |
614 | * like 1.3 or 1.4. |
615 | * </p> |
616 | * |
617 | * @param level the API level; one of the <code>JLS*</code> level |
618 | * constants |
619 | * @param previewEnabled <code>true</code> if preview feature is enabled else |
620 | * <code>false</code> |
621 | * @return new AST instance following the specified set of API rules. |
622 | * @exception IllegalArgumentException if: |
623 | * <ul> |
624 | * <li>the API level is not one of the |
625 | * <code>JLS*</code> level constants</li> |
626 | * </ul> |
627 | * @since 3.19 |
628 | */ |
629 | public static AST newAST(int level, boolean previewEnabled) { |
630 | return new AST(level, previewEnabled); |
631 | } |
632 | |
633 | /** |
634 | * Creates a new Java abstract syntax tree |
635 | * Following option keys are significant: |
636 | * <ul> |
637 | * <li><code>"org.eclipse.jdt.core.compiler.source"</code> |
638 | * indicates the api level and source compatibility mode (as per |
639 | * <code>JavaCore</code>) - defaults to 1.3 |
640 | * <ul> |
641 | * <li> |
642 | * <code>"1.3"</code> means the source code is as per JDK 1.3 and api level |
643 | * {@link #JLS3}.</li> |
644 | * <li><code>"1.4", "1.5", "1.6", "1.7" "1.8"</code> implies the respective |
645 | * source JDK levels 1.4, 1.5, 1.6, 1.7 and api level {@link #JLS4}.</li> |
646 | * <li><code>"1.8"</code> implies the respective source JDK level 1.8 and api |
647 | * level {@link #JLS8}.</li> |
648 | * <li><code>"9", "10", "11", "12" and "13"</code> implies the respective JDK |
649 | * levels 9, 10, 11, 12 and 13 |
650 | * and api levels {@link #JLS9}, {@link #JLS10}, {@link #JLS11}, {@link #JLS12} |
651 | * and {@link #JLS13}.</li> |
652 | * <li>Additional legal values may be added later.</li> |
653 | * </ul> |
654 | * </li> |
655 | * <li><code>"org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"</code> |
656 | * - |
657 | * indicates whether the preview is enabled or disabled |
658 | * legal values are <code>"enabled"</code> and <code>"disabled"</code> implying |
659 | * preview enabled and disabled respectively. |
660 | * preview enabling has an effect only with the latest ast level. |
661 | * </li> |
662 | * </ul> |
663 | * |
664 | * @param options the table of options |
665 | * @see JavaCore#getDefaultOptions() |
666 | * @since 3.20 |
667 | */ |
668 | public static AST newAST(Map<String, String> options) { |
669 | return new AST(options); |
670 | } |
671 | |
672 | /** |
673 | * Parses the given string as a Java compilation unit and creates and |
674 | * returns a corresponding abstract syntax tree. |
675 | * <p> |
676 | * The returned compilation unit node is the root node of a new AST. |
677 | * Each node in the subtree carries source range(s) information relating back |
678 | * to positions in the given source string (the given source string itself |
679 | * is not remembered with the AST). |
680 | * The source range usually begins at the first character of the first token |
681 | * corresponding to the node; leading whitespace and comments are <b>not</b> |
682 | * included. The source range usually extends through the last character of |
683 | * the last token corresponding to the node; trailing whitespace and |
684 | * comments are <b>not</b> included. There are a handful of exceptions |
685 | * (including compilation units and the various body declarations); the |
686 | * specification for these node type spells out the details. |
687 | * Source ranges nest properly: the source range for a child is always |
688 | * within the source range of its parent, and the source ranges of sibling |
689 | * nodes never overlap. |
690 | * If a syntax error is detected while parsing, the relevant node(s) of the |
691 | * tree will be flagged as <code>MALFORMED</code>. |
692 | * </p> |
693 | * <p> |
694 | * This method does not compute binding information; all |
695 | * <code>resolveBinding</code> |
696 | * methods applied to nodes of the resulting AST return <code>null</code>. |
697 | * </p> |
698 | * |
699 | * @param source the string to be parsed as a Java compilation unit |
700 | * @return the compilation unit node |
701 | * @see ASTNode#getFlags() |
702 | * @see ASTNode#MALFORMED |
703 | * @see ASTNode#getStartPosition() |
704 | * @see ASTNode#getLength() |
705 | * @since 2.0 |
706 | * @deprecated Use {@link ASTParser} instead. |
707 | */ |
708 | public static CompilationUnit parseCompilationUnit(char[] source) { |
709 | if (source == null) { |
710 | throw new IllegalArgumentException(); |
711 | } |
712 | ASTParser c = ASTParser.newParser(AST.JLS2); |
713 | c.setSource(source); |
714 | ASTNode result = c.createAST(null); |
715 | return (CompilationUnit) result; |
716 | } |
717 | |
718 | /** |
719 | * Parses the given string as the hypothetical contents of the named |
720 | * compilation unit and creates and returns a corresponding abstract syntax |
721 | * tree. |
722 | * <p> |
723 | * The returned compilation unit node is the root node of a new AST. |
724 | * Each node in the subtree carries source range(s) information relating back |
725 | * to positions in the given source string (the given source string itself |
726 | * is not remembered with the AST). |
727 | * The source range usually begins at the first character of the first token |
728 | * corresponding to the node; leading whitespace and comments are <b>not</b> |
729 | * included. The source range usually extends through the last character of |
730 | * the last token corresponding to the node; trailing whitespace and |
731 | * comments are <b>not</b> included. There are a handful of exceptions |
732 | * (including compilation units and the various body declarations); the |
733 | * specification for these node type spells out the details. |
734 | * Source ranges nest properly: the source range for a child is always |
735 | * within the source range of its parent, and the source ranges of sibling |
736 | * nodes never overlap. |
737 | * If a syntax error is detected while parsing, the relevant node(s) of the |
738 | * tree will be flagged as <code>MALFORMED</code>. |
739 | * </p> |
740 | * <p> |
741 | * If the given project is not <code>null</code>, the various names |
742 | * and types appearing in the compilation unit can be resolved to "bindings" |
743 | * by calling the <code>resolveBinding</code> methods. These bindings |
744 | * draw connections between the different parts of a program, and |
745 | * generally afford a more powerful vantage point for clients who wish to |
746 | * analyze a program's structure more deeply. These bindings come at a |
747 | * considerable cost in both time and space, however, and should not be |
748 | * requested frivolously. The additional space is not reclaimed until the |
749 | * AST, all its nodes, and all its bindings become garbage. So it is very |
750 | * important to not retain any of these objects longer than absolutely |
751 | * necessary. Bindings are resolved at the time the AST is created. Subsequent |
752 | * modifications to the AST do not affect the bindings returned by |
753 | * <code>resolveBinding</code> methods in any way; these methods return the |
754 | * same binding as before the AST was modified (including modifications |
755 | * that rearrange subtrees by reparenting nodes). |
756 | * If the given project is <code>null</code>, the analysis |
757 | * does not go beyond parsing and building the tree, and all |
758 | * <code>resolveBinding</code> methods return <code>null</code> from the |
759 | * outset. |
760 | * </p> |
761 | * <p> |
762 | * The name of the compilation unit must be supplied for resolving bindings. |
763 | * This name should be suffixed by a dot ('.') followed by one of the |
764 | * {@link JavaCore#getJavaLikeExtensions() Java-like extensions} |
765 | * and match the name of the main |
766 | * (public) class or interface declared in the source. For example, if the |
767 | * source |
768 | * declares a public class named "Foo", the name of the compilation can be |
769 | * "Foo.java". For the purposes of resolving bindings, types declared in the |
770 | * source string hide types by the same name available through the classpath |
771 | * of the given project. |
772 | * </p> |
773 | * |
774 | * @param source the string to be parsed as a Java compilation unit |
775 | * @param unitName the name of the compilation unit that would contain the |
776 | * source |
777 | * string, or <code>null</code> if <code>javaProject</code> is |
778 | * also <code>null</code> |
779 | * @param project the Java project used to resolve names, or |
780 | * <code>null</code> if bindings are not resolved |
781 | * @return the compilation unit node |
782 | * @see ASTNode#getFlags() |
783 | * @see ASTNode#MALFORMED |
784 | * @see ASTNode#getStartPosition() |
785 | * @see ASTNode#getLength() |
786 | * @since 2.0 |
787 | * @deprecated Use {@link ASTParser} instead. |
788 | */ |
789 | public static CompilationUnit parseCompilationUnit( |
790 | char[] source, |
791 | String unitName, |
792 | IJavaProject project) { |
793 | |
794 | if (source == null) { |
795 | throw new IllegalArgumentException(); |
796 | } |
797 | ASTParser astParser = ASTParser.newParser(AST.JLS2); |
798 | astParser.setSource(source); |
799 | astParser.setUnitName(unitName); |
800 | astParser.setProject(project); |
801 | astParser.setResolveBindings(project != null); |
802 | ASTNode result = astParser.createAST(null); |
803 | return (CompilationUnit) result; |
804 | } |
805 | |
806 | /** |
807 | * Parses the source string corresponding to the given Java class file |
808 | * element and creates and returns a corresponding abstract syntax tree. |
809 | * The source string is obtained from the Java model element using |
810 | * <code>IClassFile.getSource()</code>, and is only available for a class |
811 | * files with attached source. |
812 | * <p> |
813 | * The returned compilation unit node is the root node of a new AST. |
814 | * Each node in the subtree carries source range(s) information relating back |
815 | * to positions in the source string (the source string is not remembered |
816 | * with the AST). |
817 | * The source range usually begins at the first character of the first token |
818 | * corresponding to the node; leading whitespace and comments are <b>not</b> |
819 | * included. The source range usually extends through the last character of |
820 | * the last token corresponding to the node; trailing whitespace and |
821 | * comments are <b>not</b> included. There are a handful of exceptions |
822 | * (including compilation units and the various body declarations); the |
823 | * specification for these node type spells out the details. |
824 | * Source ranges nest properly: the source range for a child is always |
825 | * within the source range of its parent, and the source ranges of sibling |
826 | * nodes never overlap. |
827 | * If a syntax error is detected while parsing, the relevant node(s) of the |
828 | * tree will be flagged as <code>MALFORMED</code>. |
829 | * </p> |
830 | * <p> |
831 | * If <code>resolveBindings</code> is <code>true</code>, the various names |
832 | * and types appearing in the compilation unit can be resolved to "bindings" |
833 | * by calling the <code>resolveBinding</code> methods. These bindings |
834 | * draw connections between the different parts of a program, and |
835 | * generally afford a more powerful vantage point for clients who wish to |
836 | * analyze a program's structure more deeply. These bindings come at a |
837 | * considerable cost in both time and space, however, and should not be |
838 | * requested frivolously. The additional space is not reclaimed until the |
839 | * AST, all its nodes, and all its bindings become garbage. So it is very |
840 | * important to not retain any of these objects longer than absolutely |
841 | * necessary. Bindings are resolved at the time the AST is created. Subsequent |
842 | * modifications to the AST do not affect the bindings returned by |
843 | * <code>resolveBinding</code> methods in any way; these methods return the |
844 | * same binding as before the AST was modified (including modifications |
845 | * that rearrange subtrees by reparenting nodes). |
846 | * If <code>resolveBindings</code> is <code>false</code>, the analysis |
847 | * does not go beyond parsing and building the tree, and all |
848 | * <code>resolveBinding</code> methods return <code>null</code> from the |
849 | * outset. |
850 | * </p> |
851 | * |
852 | * @param classFile the Java model class file whose corresponding source |
853 | * code is to be parsed |
854 | * @param resolveBindings <code>true</code> if bindings are wanted, |
855 | * and <code>false</code> if bindings are not of interest |
856 | * @return the compilation unit node |
857 | * @exception IllegalArgumentException if the given Java element does not |
858 | * exist or if its source string cannot be |
859 | * obtained |
860 | * @see ASTNode#getFlags() |
861 | * @see ASTNode#MALFORMED |
862 | * @see ASTNode#getStartPosition() |
863 | * @see ASTNode#getLength() |
864 | * @since 2.1 |
865 | * @deprecated Use {@link ASTParser} instead. |
866 | */ |
867 | public static CompilationUnit parseCompilationUnit( |
868 | IClassFile classFile, |
869 | boolean resolveBindings) { |
870 | |
871 | if (classFile == null) { |
872 | throw new IllegalArgumentException(); |
873 | } |
874 | try { |
875 | ASTParser c = ASTParser.newParser(AST.JLS2); |
876 | c.setSource(classFile); |
877 | c.setResolveBindings(resolveBindings); |
878 | ASTNode result = c.createAST(null); |
879 | return (CompilationUnit) result; |
880 | } catch (IllegalStateException e) { |
881 | // convert ASTParser's complaints into old form |
882 | throw new IllegalArgumentException(e); |
883 | } |
884 | } |
885 | |
886 | /** |
887 | * Parses the source string of the given Java model compilation unit element |
888 | * and creates and returns a corresponding abstract syntax tree. The source |
889 | * string is obtained from the Java model element using |
890 | * <code>ICompilationUnit.getSource()</code>. |
891 | * <p> |
892 | * The returned compilation unit node is the root node of a new AST. |
893 | * Each node in the subtree carries source range(s) information relating back |
894 | * to positions in the source string (the source string is not remembered |
895 | * with the AST). |
896 | * The source range usually begins at the first character of the first token |
897 | * corresponding to the node; leading whitespace and comments are <b>not</b> |
898 | * included. The source range usually extends through the last character of |
899 | * the last token corresponding to the node; trailing whitespace and |
900 | * comments are <b>not</b> included. There are a handful of exceptions |
901 | * (including compilation units and the various body declarations); the |
902 | * specification for these node type spells out the details. |
903 | * Source ranges nest properly: the source range for a child is always |
904 | * within the source range of its parent, and the source ranges of sibling |
905 | * nodes never overlap. |
906 | * If a syntax error is detected while parsing, the relevant node(s) of the |
907 | * tree will be flagged as <code>MALFORMED</code>. |
908 | * </p> |
909 | * <p> |
910 | * If <code>resolveBindings</code> is <code>true</code>, the various names |
911 | * and types appearing in the compilation unit can be resolved to "bindings" |
912 | * by calling the <code>resolveBinding</code> methods. These bindings |
913 | * draw connections between the different parts of a program, and |
914 | * generally afford a more powerful vantage point for clients who wish to |
915 | * analyze a program's structure more deeply. These bindings come at a |
916 | * considerable cost in both time and space, however, and should not be |
917 | * requested frivolously. The additional space is not reclaimed until the |
918 | * AST, all its nodes, and all its bindings become garbage. So it is very |
919 | * important to not retain any of these objects longer than absolutely |
920 | * necessary. Bindings are resolved at the time the AST is created. Subsequent |
921 | * modifications to the AST do not affect the bindings returned by |
922 | * <code>resolveBinding</code> methods in any way; these methods return the |
923 | * same binding as before the AST was modified (including modifications |
924 | * that rearrange subtrees by reparenting nodes). |
925 | * If <code>resolveBindings</code> is <code>false</code>, the analysis |
926 | * does not go beyond parsing and building the tree, and all |
927 | * <code>resolveBinding</code> methods return <code>null</code> from the |
928 | * outset. |
929 | * </p> |
930 | * |
931 | * @param unit the Java model compilation unit whose source code is |
932 | * to be parsed |
933 | * @param resolveBindings <code>true</code> if bindings are wanted, |
934 | * and <code>false</code> if bindings are not of interest |
935 | * @return the compilation unit node |
936 | * @exception IllegalArgumentException if the given Java element does not |
937 | * exist or if its source string cannot be |
938 | * obtained |
939 | * @see ASTNode#getFlags() |
940 | * @see ASTNode#MALFORMED |
941 | * @see ASTNode#getStartPosition() |
942 | * @see ASTNode#getLength() |
943 | * @since 2.0 |
944 | * @deprecated Use {@link ASTParser} instead. |
945 | */ |
946 | public static CompilationUnit parseCompilationUnit( |
947 | ICompilationUnit unit, |
948 | boolean resolveBindings) { |
949 | |
950 | try { |
951 | ASTParser c = ASTParser.newParser(AST.JLS2); |
952 | c.setSource(unit); |
953 | c.setResolveBindings(resolveBindings); |
954 | ASTNode result = c.createAST(null); |
955 | return (CompilationUnit) result; |
956 | } catch (IllegalStateException e) { |
957 | // convert ASTParser's complaints into old form |
958 | throw new IllegalArgumentException(e); |
959 | } |
960 | } |
961 | |
962 | /** |
963 | * Level of AST API supported by this AST. |
964 | * |
965 | * @since 3.0 |
966 | */ |
967 | int apiLevel; |
968 | |
969 | private boolean previewEnabled; |
970 | |
971 | /** |
972 | * Tag bit value. This represents internal state of the tree. |
973 | */ |
974 | private int bits; |
975 | |
976 | /** |
977 | * Default value of <code>flag<code> when a new node is created. |
978 | */ |
979 | private int defaultNodeFlag = 0; |
980 | |
981 | /** |
982 | * When disableEvents > 0, events are not reported and |
983 | * the modification count stays fixed. |
984 | * <p> |
985 | * This mechanism is used in lazy initialization of a node |
986 | * to prevent events from being reported for the modification |
987 | * of the node as well as for the creation of the missing child. |
988 | * </p> |
989 | * |
990 | * @since 3.0 |
991 | */ |
992 | private int disableEvents = 0; |
993 | |
994 | /** |
995 | * The event handler for this AST. |
996 | * Initially an event handler that does not nothing. |
997 | * |
998 | * @since 3.0 |
999 | */ |
1000 | private NodeEventHandler eventHandler = new NodeEventHandler(); |
1001 | |
1002 | /** |
1003 | * Internal object unique to the AST instance. Readers must synchronize on |
1004 | * this object when the modifying instance fields. |
1005 | * |
1006 | * @since 3.0 |
1007 | */ |
1008 | private final Object internalASTLock = new Object(); |
1009 | |
1010 | /** |
1011 | * Internal modification count; initially 0; increases monotonically |
1012 | * <b>by one or more</b> as the AST is successively modified. |
1013 | */ |
1014 | private long modificationCount = 0; |
1015 | |
1016 | /** |
1017 | * Internal original modification count; value is equals to <code> |
1018 | * modificationCount</code> at the end of the parse (<code>ASTParser |
1019 | * </code>). If this ast is not created with a parser then value is 0. |
1020 | * |
1021 | * @since 3.0 |
1022 | */ |
1023 | private long originalModificationCount = 0; |
1024 | |
1025 | /** |
1026 | * The binding resolver for this AST. Initially a binding resolver that |
1027 | * does not resolve names at all. |
1028 | */ |
1029 | private BindingResolver resolver = new BindingResolver(); |
1030 | |
1031 | /** |
1032 | * Internal ast rewriter used to record ast modification when record mode is |
1033 | * enabled. |
1034 | */ |
1035 | InternalASTRewrite rewriter; |
1036 | |
1037 | /** |
1038 | * Java Scanner used to validate preconditions for the creation of specific |
1039 | * nodes |
1040 | * like CharacterLiteral, NumberLiteral, StringLiteral or SimpleName. |
1041 | */ |
1042 | Scanner scanner; |
1043 | |
1044 | /** |
1045 | * new Object[] {this} |
1046 | * |
1047 | * @since 3.0 |
1048 | */ |
1049 | private final Object[] THIS_AST = new Object[] { this }; |
1050 | |
1051 | /** |
1052 | * Creates a new, empty abstract syntax tree using default options. |
1053 | * |
1054 | * @see JavaCore#getDefaultOptions() |
1055 | * @deprecated Clients should port their code to use the latest JLS* AST API and |
1056 | * call |
1057 | * {@link #newAST(int, boolean) AST.newAST(AST.JLS12, false)} |
1058 | * instead of using this constructor. |
1059 | */ |
1060 | public AST() { |
1061 | this(JavaCore.getDefaultOptions()); |
1062 | } |
1063 | |
1064 | /** |
1065 | * Creates a new Java abstract syntax tree |
1066 | * (AST) following the specified set of API rules. |
1067 | * |
1068 | * @param level the API level; one of the <code>JLS*</code> level constants |
1069 | * @since 3.0 |
1070 | */ |
1071 | private AST(int level, boolean previewEnabled) { |
1072 | this.previewEnabled = previewEnabled; |
1073 | switch (level) { |
1074 | case JLS2_INTERNAL: |
1075 | case JLS3_INTERNAL: |
1076 | this.apiLevel = level; |
1077 | // initialize a scanner |
1078 | this.scanner = new Scanner( |
1079 | true /* comment */, |
1080 | true /* whitespace */, |
1081 | false /* nls */, |
1082 | ClassFileConstants.JDK1_3 /* sourceLevel */, |
1083 | ClassFileConstants.JDK1_5 /* complianceLevel */, |
1084 | null/* taskTag */, |
1085 | null/* taskPriorities */, |
1086 | true/* taskCaseSensitive */, |
1087 | false/* isPreviewEnabled */); |
1088 | break; |
1089 | case JLS4_INTERNAL: |
1090 | this.apiLevel = level; |
1091 | // initialize a scanner |
1092 | this.scanner = new Scanner( |
1093 | true /* comment */, |
1094 | true /* whitespace */, |
1095 | false /* nls */, |
1096 | ClassFileConstants.JDK1_7 /* sourceLevel */, |
1097 | ClassFileConstants.JDK1_7 /* complianceLevel */, |
1098 | null/* taskTag */, |
1099 | null/* taskPriorities */, |
1100 | true/* taskCaseSensitive */, |
1101 | false/* isPreviewEnabled */); |
1102 | break; |
1103 | case JLS8_INTERNAL: |
1104 | this.apiLevel = level; |
1105 | // initialize a scanner |
1106 | this.scanner = new Scanner( |
1107 | true /* comment */, |
1108 | true /* whitespace */, |
1109 | false /* nls */, |
1110 | ClassFileConstants.JDK1_8 /* sourceLevel */, |
1111 | ClassFileConstants.JDK1_8 /* complianceLevel */, |
1112 | null/* taskTag */, |
1113 | null/* taskPriorities */, |
1114 | true/* taskCaseSensitive */, |
1115 | false/* isPreviewEnabled */); |
1116 | break; |
1117 | case JLS9_INTERNAL: |
1118 | this.apiLevel = level; |
1119 | // initialize a scanner |
1120 | this.scanner = new Scanner( |
1121 | true /* comment */, |
1122 | true /* whitespace */, |
1123 | false /* nls */, |
1124 | ClassFileConstants.JDK9 /* sourceLevel */, |
1125 | ClassFileConstants.JDK9 /* complianceLevel */, |
1126 | null/* taskTag */, |
1127 | null/* taskPriorities */, |
1128 | true/* taskCaseSensitive */, |
1129 | false/* isPreviewEnabled */); |
1130 | break; |
1131 | case JLS10_INTERNAL: |
1132 | this.apiLevel = level; |
1133 | // initialize a scanner |
1134 | this.scanner = new Scanner( |
1135 | true /* comment */, |
1136 | true /* whitespace */, |
1137 | false /* nls */, |
1138 | ClassFileConstants.JDK10 /* sourceLevel */, |
1139 | ClassFileConstants.JDK10 /* complianceLevel */, |
1140 | null/* taskTag */, |
1141 | null/* taskPriorities */, |
1142 | true/* taskCaseSensitive */, |
1143 | false/* isPreviewEnabled */); |
1144 | break; |
1145 | default: |
1146 | if (level < JLS2_INTERNAL && level > JLS_Latest) { |
1147 | throw new IllegalArgumentException("Unsupported JLS level : " + level); //$NON-NLS-1$ |
1148 | } |
1149 | this.apiLevel = level; |
1150 | // initialize a scanner |
1151 | // As long as the AST levels and ClassFileConstants.MAJOR_VERSION grow |
1152 | // simultaneously, |
1153 | // we can use the offset of +44 to compute the Major version from the given AST |
1154 | // Level |
1155 | long compliance = ClassFileConstants.getComplianceLevelForJavaVersion(level + 44); |
1156 | this.scanner = new Scanner( |
1157 | true /* comment */, |
1158 | true /* whitespace */, |
1159 | false /* nls */, |
1160 | compliance /* sourceLevel */, |
1161 | compliance /* complianceLevel */, |
1162 | null/* taskTag */, |
1163 | null/* taskPriorities */, |
1164 | true/* taskCaseSensitive */, |
1165 | false/* isPreviewEnabled */); |
1166 | break; |
1167 | } |
1168 | } |
1169 | |
1170 | /** |
1171 | * Creates a new Java abstract syntax tree |
1172 | * Following option keys are significant: |
1173 | * <ul> |
1174 | * <li><code>"org.eclipse.jdt.core.compiler.source"</code> |
1175 | * indicates the api level and source compatibility mode (as per |
1176 | * <code>JavaCore</code>) - defaults to 1.3 |
1177 | * <ul> |
1178 | * <li> |
1179 | * <code>"1.3"</code> means the source code is as per JDK 1.3 and api level |
1180 | * {@link #JLS3}.</li> |
1181 | * <li><code>"1.4", "1.5", "1.6", "1.7" "1.8"</code> implies the respective |
1182 | * source JDK levels 1.4, 1.5, 1.6, 1.7 and api level {@link #JLS4}.</li> |
1183 | * <li><code>"1.8"</code> implies the respective source JDK level 1.8 and api |
1184 | * level {@link #JLS8}.</li> |
1185 | * <li><code>"9", "10", "11", "12", "13", "14", "15", "16" and "17"</code> |
1186 | * implies the respective JDK levels 9, 10, 11, 12, 13, 14, 15, 16 and 17 |
1187 | * and api levels {@link #JLS9}, {@link #JLS10}, {@link #JLS11}, {@link #JLS12}, |
1188 | * {@link #JLS13} |
1189 | * {@link #JLS14}, {@link #JLS15}, {@link #JLS16} and {@link #JLS17}.</li> |
1190 | * <li>Additional legal values may be added later.</li> |
1191 | * </ul> |
1192 | * <li><code>"org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"</code> |
1193 | * - |
1194 | * indicates whether the preview is enabled or disabled |
1195 | * legal values are <code>"enabled"</code> and <code>"disabled"</code> implying |
1196 | * preview enabled and disabled respectively. |
1197 | * preview enabling has an effect only with the latest ast level. |
1198 | * </li> |
1199 | * </ul> |
1200 | * |
1201 | * @param options the table of options (key type: <code>String</code>; |
1202 | * value type: <code>String</code>) |
1203 | * @see JavaCore#getDefaultOptions() |
1204 | */ |
1205 | public AST(Map options) { |
1206 | this(apiLevelMap.get(options.get(JavaCore.COMPILER_SOURCE)), |
1207 | JavaCore.ENABLED.equals(options.get(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES))); |
1208 | |
1209 | long sourceLevel; |
1210 | long complianceLevel; |
1211 | switch (this.apiLevel) { |
1212 | case JLS2_INTERNAL: |
1213 | case JLS3_INTERNAL: |
1214 | sourceLevel = ClassFileConstants.JDK1_3; |
1215 | complianceLevel = ClassFileConstants.JDK1_5; |
1216 | break; |
1217 | case JLS4_INTERNAL: |
1218 | sourceLevel = ClassFileConstants.JDK1_7; |
1219 | complianceLevel = ClassFileConstants.JDK1_7; |
1220 | break; |
1221 | default: |
1222 | sourceLevel = AST.jdkLevelMap.get(options.get(JavaCore.COMPILER_SOURCE)); |
1223 | complianceLevel = sourceLevel; |
1224 | } |
1225 | this.scanner = new Scanner( |
1226 | true /* comment */, |
1227 | true /* whitespace */, |
1228 | false /* nls */, |
1229 | sourceLevel /* sourceLevel */, |
1230 | complianceLevel /* complianceLevel */, |
1231 | null/* taskTag */, |
1232 | null/* taskPriorities */, |
1233 | true/* taskCaseSensitive */, |
1234 | this.previewEnabled /* isPreviewEnabled */); |
1235 | } |
1236 | |
1237 | private static Map<String, Long> getLevelMapTable() { |
1238 | Map<String, Long> t = new HashMap<>(); |
1239 | t.put(null, ClassFileConstants.JDK1_2); |
1240 | t.put(JavaCore.VERSION_1_2, ClassFileConstants.JDK1_2); |
1241 | t.put(JavaCore.VERSION_1_3, ClassFileConstants.JDK1_3); |
1242 | t.put(JavaCore.VERSION_1_4, ClassFileConstants.JDK1_4); |
1243 | t.put(JavaCore.VERSION_1_5, ClassFileConstants.JDK1_5); |
1244 | t.put(JavaCore.VERSION_1_6, ClassFileConstants.JDK1_6); |
1245 | t.put(JavaCore.VERSION_1_7, ClassFileConstants.JDK1_7); |
1246 | t.put(JavaCore.VERSION_1_8, ClassFileConstants.JDK1_8); |
1247 | t.put(JavaCore.VERSION_9, ClassFileConstants.JDK9); |
1248 | t.put(JavaCore.VERSION_10, ClassFileConstants.JDK10); |
1249 | t.put(JavaCore.VERSION_11, ClassFileConstants.JDK11); |
1250 | t.put(JavaCore.VERSION_12, ClassFileConstants.JDK12); |
1251 | t.put(JavaCore.VERSION_13, ClassFileConstants.JDK13); |
1252 | t.put(JavaCore.VERSION_14, ClassFileConstants.JDK14); |
1253 | t.put(JavaCore.VERSION_15, ClassFileConstants.JDK15); |
1254 | t.put(JavaCore.VERSION_16, ClassFileConstants.JDK16); |
1255 | t.put(JavaCore.VERSION_17, ClassFileConstants.JDK17); |
1256 | t.put(JavaCore.VERSION_18, ClassFileConstants.JDK18); |
1257 | return Collections.unmodifiableMap(t); |
1258 | } |
1259 | |
1260 | private static Map<String, Integer> getApiLevelMapTable() { |
1261 | Map<String, Integer> t = new HashMap<>(); |
1262 | t.put(null, JLS2_INTERNAL); |
1263 | t.put(JavaCore.VERSION_1_2, JLS2_INTERNAL); |
1264 | t.put(JavaCore.VERSION_1_3, JLS3_INTERNAL); |
1265 | t.put(JavaCore.VERSION_1_4, JLS4_INTERNAL); |
1266 | t.put(JavaCore.VERSION_1_5, JLS4_INTERNAL); |
1267 | t.put(JavaCore.VERSION_1_6, JLS4_INTERNAL); |
1268 | t.put(JavaCore.VERSION_1_7, JLS4_INTERNAL); |
1269 | t.put(JavaCore.VERSION_1_8, JLS8_INTERNAL); |
1270 | t.put(JavaCore.VERSION_9, JLS9_INTERNAL); |
1271 | t.put(JavaCore.VERSION_10, JLS10_INTERNAL); |
1272 | t.put(JavaCore.VERSION_11, JLS11_INTERNAL); |
1273 | t.put(JavaCore.VERSION_12, JLS12_INTERNAL); |
1274 | t.put(JavaCore.VERSION_13, JLS13_INTERNAL); |
1275 | t.put(JavaCore.VERSION_14, JLS14_INTERNAL); |
1276 | t.put(JavaCore.VERSION_15, JLS15_INTERNAL); |
1277 | t.put(JavaCore.VERSION_16, JLS16_INTERNAL); |
1278 | t.put(JavaCore.VERSION_17, JLS17_INTERNAL); |
1279 | t.put(JavaCore.VERSION_18, JLS18_INTERNAL); |
1280 | return Collections.unmodifiableMap(t); |
1281 | } |
1282 | |
1283 | /** |
1284 | * Return the API level supported by this AST. |
1285 | * |
1286 | * @return level the API level; one of the <code>JLS*</code> level constants |
1287 | * declared on <code>AST</code>; assume this set is open-ended |
1288 | * @since 3.0 |
1289 | */ |
1290 | public int apiLevel() { |
1291 | return this.apiLevel; |
1292 | } |
1293 | |
1294 | /** |
1295 | * Creates an unparented node of the given node class |
1296 | * (non-abstract subclass of {@link ASTNode}). |
1297 | * |
1298 | * @param nodeClass AST node class |
1299 | * @return a new unparented node owned by this AST |
1300 | * @exception IllegalArgumentException if <code>nodeClass</code> is |
1301 | * <code>null</code> or is not a concrete |
1302 | * node type class |
1303 | * or is not supported for this AST's API |
1304 | * level |
1305 | * @since 3.0 |
1306 | */ |
1307 | public ASTNode createInstance(Class nodeClass) { |
1308 | if (nodeClass == null) { |
1309 | throw new IllegalArgumentException(); |
1310 | } |
1311 | try { |
1312 | // invoke constructor with signature Foo(AST) |
1313 | Constructor c = nodeClass.getDeclaredConstructor(AST_CLASS); |
1314 | Object result = c.newInstance(this.THIS_AST); |
1315 | return (ASTNode) result; |
1316 | } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) { |
1317 | // all AST node classes have an accessible Foo(AST) constructor |
1318 | // therefore nodeClass is not legit |
1319 | throw new IllegalArgumentException(e); |
1320 | } catch (InvocationTargetException e) { |
1321 | // concrete AST node classes do not die in the constructor |
1322 | // therefore nodeClass is not legit |
1323 | throw new IllegalArgumentException(e.getCause()); |
1324 | } |
1325 | } |
1326 | |
1327 | /** |
1328 | * Creates an unparented node of the given node type. |
1329 | * This convenience method is equivalent to: |
1330 | * |
1331 | * <pre> |
1332 | * createInstance(ASTNode.nodeClassForType(nodeType)) |
1333 | * </pre> |
1334 | * |
1335 | * @param nodeType AST node type, one of the node type |
1336 | * constants declared on {@link ASTNode} |
1337 | * @return a new unparented node owned by this AST |
1338 | * @exception IllegalArgumentException if <code>nodeType</code> is |
1339 | * not a legal AST node type or if it's not |
1340 | * supported for this AST's API level |
1341 | * @since 3.0 |
1342 | */ |
1343 | public ASTNode createInstance(int nodeType) { |
1344 | // nodeClassForType throws IllegalArgumentException if nodeType is bogus |
1345 | Class nodeClass = ASTNode.nodeClassForType(nodeType); |
1346 | return createInstance(nodeClass); |
1347 | } |
1348 | |
1349 | /** |
1350 | * Disable events. |
1351 | * This method is thread-safe for AST readers. |
1352 | * |
1353 | * @see #reenableEvents() |
1354 | * @since 3.0 |
1355 | */ |
1356 | final void disableEvents() { |
1357 | synchronized (this.internalASTLock) { |
1358 | // guard against concurrent access by another reader |
1359 | this.disableEvents++; |
1360 | } |
1361 | // while disableEvents > 0 no events will be reported, and mod count will stay |
1362 | // fixed |
1363 | } |
1364 | |
1365 | /** |
1366 | * Returns the binding resolver for this AST. |
1367 | * |
1368 | * @return the binding resolver for this AST |
1369 | */ |
1370 | BindingResolver getBindingResolver() { |
1371 | return this.resolver; |
1372 | } |
1373 | |
1374 | /** |
1375 | * Returns default node flags of new nodes of this AST. |
1376 | * |
1377 | * @return the default node flags of new nodes of this AST |
1378 | * @since 3.0 |
1379 | */ |
1380 | int getDefaultNodeFlag() { |
1381 | return this.defaultNodeFlag; |
1382 | } |
1383 | |
1384 | /** |
1385 | * Returns the event handler for this AST. |
1386 | * |
1387 | * @return the event handler for this AST |
1388 | * @since 3.0 |
1389 | */ |
1390 | NodeEventHandler getEventHandler() { |
1391 | return this.eventHandler; |
1392 | } |
1393 | |
1394 | /** |
1395 | * Returns true if the ast tree was created with bindings recovery, false |
1396 | * otherwise |
1397 | * |
1398 | * @return true if the ast tree was created with bindings recovery, false |
1399 | * otherwise |
1400 | * @since 3.3 |
1401 | */ |
1402 | public boolean hasBindingsRecovery() { |
1403 | return (this.bits & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0; |
1404 | } |
1405 | |
1406 | /** |
1407 | * Returns true if the ast tree was created with bindings, false otherwise |
1408 | * |
1409 | * @return true if the ast tree was created with bindings, false otherwise |
1410 | * @since 3.3 |
1411 | */ |
1412 | public boolean hasResolvedBindings() { |
1413 | return (this.bits & RESOLVED_BINDINGS) != 0; |
1414 | } |
1415 | |
1416 | /** |
1417 | * Returns true if the ast tree was created with statements recovery, false |
1418 | * otherwise |
1419 | * |
1420 | * @return true if the ast tree was created with statements recovery, false |
1421 | * otherwise |
1422 | * @since 3.3 |
1423 | */ |
1424 | public boolean hasStatementsRecovery() { |
1425 | return (this.bits & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0; |
1426 | } |
1427 | |
1428 | /* |
1429 | * (omit javadoc for this method) |
1430 | * This method is a copy of setName(String[]) that doesn't do any validation. |
1431 | */ |
1432 | Name internalNewName(String[] identifiers) { |
1433 | int count = identifiers.length; |
1434 | if (count == 0) { |
1435 | throw new IllegalArgumentException(); |
1436 | } |
1437 | final SimpleName simpleName = new SimpleName(this); |
1438 | simpleName.internalSetIdentifier(identifiers[0]); |
1439 | Name result = simpleName; |
1440 | for (int i = 1; i < count; i++) { |
1441 | SimpleName name = new SimpleName(this); |
1442 | name.internalSetIdentifier(identifiers[i]); |
1443 | result = newQualifiedName(result, name); |
1444 | } |
1445 | return result; |
1446 | } |
1447 | |
1448 | /** |
1449 | * Returns the modification count for this AST. The modification count |
1450 | * is a non-negative value that increases (by 1 or perhaps by more) as |
1451 | * this AST or its nodes are changed. The initial value is unspecified. |
1452 | * <p> |
1453 | * The following things count as modifying an AST: |
1454 | * <ul> |
1455 | * <li>creating a new node owned by this AST,</li> |
1456 | * <li>adding a child to a node owned by this AST,</li> |
1457 | * <li>removing a child from a node owned by this AST,</li> |
1458 | * <li>setting a non-node attribute of a node owned by this AST.</li> |
1459 | * </ul> |
1460 | * <p> |
1461 | * Operations which do not entail creating or modifying existing nodes |
1462 | * do not increase the modification count. |
1463 | * </p> |
1464 | * <p> |
1465 | * N.B. This method may be called several times in the course |
1466 | * of a single client operation. The only promise is that the modification |
1467 | * count increases monotonically as the AST or its nodes change; there is |
1468 | * no promise that a modifying operation increases the count by exactly 1. |
1469 | * </p> |
1470 | * |
1471 | * @return the current value (non-negative) of the modification counter of |
1472 | * this AST |
1473 | */ |
1474 | public long modificationCount() { |
1475 | return this.modificationCount; |
1476 | } |
1477 | |
1478 | /** |
1479 | * Indicates that this AST is about to be modified. |
1480 | * <p> |
1481 | * The following things count as modifying an AST: |
1482 | * <ul> |
1483 | * <li>creating a new node owned by this AST</li> |
1484 | * <li>adding a child to a node owned by this AST</li> |
1485 | * <li>removing a child from a node owned by this AST</li> |
1486 | * <li>setting a non-node attribute of a node owned by this AST</li>. |
1487 | * </ul> |
1488 | * <p> |
1489 | * N.B. This method may be called several times in the course |
1490 | * of a single client operation. |
1491 | * </p> |
1492 | */ |
1493 | void modifying() { |
1494 | // when this method is called during lazy init, events are disabled |
1495 | // and the modification count will not be increased |
1496 | if (this.disableEvents > 0) { |
1497 | return; |
1498 | } |
1499 | // increase the modification count |
1500 | this.modificationCount++; |
1501 | } |
1502 | |
1503 | /** |
1504 | * A local method to workaround calling deprecated method in array type. |
1505 | * |
1506 | * @deprecated |
1507 | */ |
1508 | private void setArrayComponentType(ArrayType arrayType, Type type) { |
1509 | arrayType.setComponentType(type); |
1510 | } |
1511 | |
1512 | /** |
1513 | * Creates and returns a new unparented annotation type declaration |
1514 | * node for an unspecified, but legal, name; no modifiers; no javadoc; |
1515 | * and an empty list of member declarations. |
1516 | * |
1517 | * @return a new unparented annotation type declaration node |
1518 | * @exception UnsupportedOperationException if this operation is used in |
1519 | * a JLS2 AST |
1520 | * @since 3.1 |
1521 | */ |
1522 | public AnnotationTypeDeclaration newAnnotationTypeDeclaration() { |
1523 | AnnotationTypeDeclaration result = new AnnotationTypeDeclaration(this); |
1524 | return result; |
1525 | } |
1526 | |
1527 | /** |
1528 | * Creates and returns a new unparented annotation type |
1529 | * member declaration node for an unspecified, but legal, |
1530 | * member name and type; no modifiers; no javadoc; |
1531 | * and no default value. |
1532 | * |
1533 | * @return a new unparented annotation type member declaration node |
1534 | * @exception UnsupportedOperationException if this operation is used in |
1535 | * a JLS2 AST |
1536 | * @since 3.1 |
1537 | */ |
1538 | public AnnotationTypeMemberDeclaration newAnnotationTypeMemberDeclaration() { |
1539 | AnnotationTypeMemberDeclaration result = new AnnotationTypeMemberDeclaration(this); |
1540 | return result; |
1541 | } |
1542 | |
1543 | /** |
1544 | * Creates and returns a new unparented anonymous class declaration |
1545 | * node owned by this AST. By default, the body declaration list is empty. |
1546 | * |
1547 | * @return a new unparented anonymous class declaration node |
1548 | */ |
1549 | public AnonymousClassDeclaration newAnonymousClassDeclaration() { |
1550 | AnonymousClassDeclaration result = new AnonymousClassDeclaration(this); |
1551 | return result; |
1552 | } |
1553 | |
1554 | /** |
1555 | * Creates and returns a new unparented array access expression node |
1556 | * owned by this AST. By default, the array and index expression are |
1557 | * both unspecified (but legal). |
1558 | * |
1559 | * @return a new unparented array access expression node |
1560 | */ |
1561 | public ArrayAccess newArrayAccess() { |
1562 | ArrayAccess result = new ArrayAccess(this); |
1563 | return result; |
1564 | } |
1565 | |
1566 | /** |
1567 | * Creates and returns a new unparented array creation expression node |
1568 | * owned by this AST. By default, the array type is an unspecified |
1569 | * 1-dimensional array, the list of dimensions is empty, and there is no |
1570 | * array initializer. |
1571 | * <p> |
1572 | * Examples: |
1573 | * |
1574 | * <pre> |
1575 | * <code> |
1576 | * // new String[len] |
1577 | * ArrayCreation ac1 = ast.newArrayCreation(); |
1578 | * ac1.setType( |
1579 | * ast.newArrayType( |
1580 | * ast.newSimpleType(ast.newSimpleName("String")))); |
1581 | * ac1.dimensions().add(ast.newSimpleName("len")); |
1582 | * |
1583 | * // new double[7][24][] |
1584 | * ArrayCreation ac2 = ast.newArrayCreation(); |
1585 | * ac2.setType( |
1586 | * ast.newArrayType( |
1587 | * ast.newPrimitiveType(PrimitiveType.DOUBLE), 3)); |
1588 | * ac2.dimensions().add(ast.newNumberLiteral("7")); |
1589 | * ac2.dimensions().add(ast.newNumberLiteral("24")); |
1590 | * |
1591 | * // new int[] {1, 2} |
1592 | * ArrayCreation ac3 = ast.newArrayCreation(); |
1593 | * ac3.setType( |
1594 | * ast.newArrayType( |
1595 | * ast.newPrimitiveType(PrimitiveType.INT))); |
1596 | * ArrayInitializer ai = ast.newArrayInitializer(); |
1597 | * ac3.setInitializer(ai); |
1598 | * ai.expressions().add(ast.newNumberLiteral("1")); |
1599 | * ai.expressions().add(ast.newNumberLiteral("2")); |
1600 | * </code> |
1601 | * </pre> |
1602 | * |
1603 | * @return a new unparented array creation expression node |
1604 | */ |
1605 | public ArrayCreation newArrayCreation() { |
1606 | ArrayCreation result = new ArrayCreation(this); |
1607 | return result; |
1608 | } |
1609 | |
1610 | /** |
1611 | * Creates and returns a new unparented array initializer node |
1612 | * owned by this AST. By default, the initializer has no expressions. |
1613 | * |
1614 | * @return a new unparented array initializer node |
1615 | */ |
1616 | public ArrayInitializer newArrayInitializer() { |
1617 | ArrayInitializer result = new ArrayInitializer(this); |
1618 | return result; |
1619 | } |
1620 | |
1621 | /** |
1622 | * Creates and returns a new unparented array type node with the given |
1623 | * element type, which cannot be an array type for API levels JLS8 and later. |
1624 | * By default, the array type has one non-annotated dimension. |
1625 | * <p> |
1626 | * For JLS4 and before, the given component type may be another array type. |
1627 | * </p> |
1628 | * |
1629 | * @param elementType element type for API level JLS8 and later, or the |
1630 | * component type (possibly another array type) for levels |
1631 | * less than JLS8 |
1632 | * @return a new unparented array type node |
1633 | * @exception IllegalArgumentException if: |
1634 | * <ul> |
1635 | * <li>the node belongs to a different |
1636 | * AST</li> |
1637 | * <li>the node already has a parent</li> |
1638 | * <li>API level is JLS8 or later and type |
1639 | * is an array type</li> |
1640 | * </ul> |
1641 | */ |
1642 | public ArrayType newArrayType(Type elementType) { |
1643 | ArrayType result; |
1644 | if (this.apiLevel < JLS8_INTERNAL) { |
1645 | result = new ArrayType(this); |
1646 | setArrayComponentType(result, elementType); |
1647 | return result; |
1648 | } |
1649 | if (elementType.isArrayType()) { |
1650 | throw new IllegalArgumentException(); |
1651 | } |
1652 | result = new ArrayType(this); |
1653 | result.setElementType(elementType); |
1654 | return result; |
1655 | } |
1656 | |
1657 | /** |
1658 | * Creates and returns a new unparented array type node with the given |
1659 | * element type and number of dimensions. |
1660 | * <p> |
1661 | * For JLS4 and before, the element type passed in can be an array type, but in |
1662 | * that case, the |
1663 | * element type of the result will not be the same as what was passed in. |
1664 | * For JLS4 and before, the dimensions cannot be 0. |
1665 | * </p> |
1666 | * |
1667 | * @param elementType the element type (cannot be an array type for JLS8 and |
1668 | * later) |
1669 | * @param dimensions the number of dimensions, a non-negative number |
1670 | * @return a new unparented array type node |
1671 | * @exception IllegalArgumentException if: |
1672 | * <ul> |
1673 | * <li>the node belongs to a different |
1674 | * AST</li> |
1675 | * <li>the node already has a parent</li> |
1676 | * <li>the element type is null</li> |
1677 | * <li>the number of dimensions is lower |
1678 | * than 0 (for JLS4 and before: lower than |
1679 | * 1)</li> |
1680 | * <li>the number of dimensions is greater |
1681 | * than 255</li> |
1682 | * <li>for levels from JLS8 and later, if |
1683 | * the element type is an array type</li> |
1684 | * </ul> |
1685 | */ |
1686 | public ArrayType newArrayType(Type elementType, int dimensions) { |
1687 | if (elementType == null) { |
1688 | throw new IllegalArgumentException(); |
1689 | } |
1690 | if (dimensions < 0 || dimensions > 255) { |
1691 | // max as per Java VM spec |
1692 | throw new IllegalArgumentException(); |
1693 | } |
1694 | ArrayType result; |
1695 | if (this.apiLevel < JLS8_INTERNAL) { |
1696 | if (dimensions < 1) { |
1697 | throw new IllegalArgumentException(); |
1698 | } |
1699 | result = new ArrayType(this); |
1700 | setArrayComponentType(result, elementType); |
1701 | for (int i = 2; i <= dimensions; i++) { |
1702 | result = newArrayType(result); |
1703 | } |
1704 | return result; |
1705 | } |
1706 | // level >= JLS8 |
1707 | if (elementType.isArrayType()) { |
1708 | throw new IllegalArgumentException(); |
1709 | } |
1710 | result = new ArrayType(this, 0); |
1711 | result.setElementType(elementType); |
1712 | for (int i = 0; i < dimensions; ++i) { |
1713 | result.dimensions().add(new Dimension(this)); |
1714 | } |
1715 | return result; |
1716 | |
1717 | } |
1718 | |
1719 | /** |
1720 | * Creates a new unparented assert statement node owned by this AST. |
1721 | * By default, the first expression is unspecified, but legal, and has no |
1722 | * message expression. |
1723 | * |
1724 | * @return a new unparented assert statement node |
1725 | */ |
1726 | public AssertStatement newAssertStatement() { |
1727 | return new AssertStatement(this); |
1728 | } |
1729 | |
1730 | /** |
1731 | * Creates and returns a new unparented assignment expression node |
1732 | * owned by this AST. By default, the assignment operator is "=" and |
1733 | * the left and right hand side expressions are unspecified, but |
1734 | * legal, names. |
1735 | * |
1736 | * @return a new unparented assignment expression node |
1737 | */ |
1738 | public Assignment newAssignment() { |
1739 | Assignment result = new Assignment(this); |
1740 | return result; |
1741 | } |
1742 | |
1743 | /** |
1744 | * Creates an unparented block node owned by this AST, for an empty list |
1745 | * of statements. |
1746 | * |
1747 | * @return a new unparented, empty block node |
1748 | */ |
1749 | public Block newBlock() { |
1750 | return new Block(this); |
1751 | } |
1752 | |
1753 | /** |
1754 | * Creates and returns a new block comment placeholder node. |
1755 | * <p> |
1756 | * Note that this node type is used to recording the source |
1757 | * range where a comment was found in the source string. |
1758 | * These comment nodes are normally found (only) in |
1759 | * {@linkplain CompilationUnit#getCommentList() |
1760 | * the comment table} for parsed compilation units. |
1761 | * </p> |
1762 | * |
1763 | * @return a new unparented block comment node |
1764 | * @since 3.0 |
1765 | */ |
1766 | public BlockComment newBlockComment() { |
1767 | BlockComment result = new BlockComment(this); |
1768 | return result; |
1769 | } |
1770 | |
1771 | /** |
1772 | * Creates and returns a new unparented boolean literal node. |
1773 | * <p> |
1774 | * For example, the assignment expression <code>foo = true</code> |
1775 | * is generated by the following snippet: |
1776 | * |
1777 | * <pre> |
1778 | * <code> |
1779 | * Assignment e= ast.newAssignment(); |
1780 | * e.setLeftHandSide(ast.newSimpleName("foo")); |
1781 | * e.setRightHandSide(ast.newBooleanLiteral(true)); |
1782 | * </code> |
1783 | * </pre> |
1784 | * |
1785 | * @param value the boolean value |
1786 | * @return a new unparented boolean literal node |
1787 | */ |
1788 | public BooleanLiteral newBooleanLiteral(boolean value) { |
1789 | BooleanLiteral result = new BooleanLiteral(this); |
1790 | result.setBooleanValue(value); |
1791 | return result; |
1792 | } |
1793 | |
1794 | /** |
1795 | * Creates an unparented break statement node owned by this AST. |
1796 | * The break statement has no label/identifier/expression and is not implicit. |
1797 | * |
1798 | * @return a new unparented break statement node |
1799 | */ |
1800 | public BreakStatement newBreakStatement() { |
1801 | return new BreakStatement(this); |
1802 | } |
1803 | |
1804 | /** |
1805 | * Creates and returns a new unparented default case expression node. |
1806 | * |
1807 | * @return a new unparented default case expression node |
1808 | * @since 3.28 |
1809 | */ |
1810 | public CaseDefaultExpression newCaseDefaultExpression() { |
1811 | CaseDefaultExpression result = new CaseDefaultExpression(this); |
1812 | return result; |
1813 | } |
1814 | |
1815 | /** |
1816 | * Creates and returns a new unparented cast expression node |
1817 | * owned by this AST. By default, the type and expression are unspecified |
1818 | * (but legal). |
1819 | * |
1820 | * @return a new unparented cast expression node |
1821 | */ |
1822 | public CastExpression newCastExpression() { |
1823 | CastExpression result = new CastExpression(this); |
1824 | return result; |
1825 | } |
1826 | |
1827 | /** |
1828 | * Creates a new unparented catch clause node owned by this AST. |
1829 | * By default, the catch clause declares an unspecified, but legal, |
1830 | * exception declaration and has an empty block. |
1831 | * |
1832 | * @return a new unparented catch clause node |
1833 | */ |
1834 | public CatchClause newCatchClause() { |
1835 | return new CatchClause(this); |
1836 | } |
1837 | |
1838 | /** |
1839 | * Creates and returns a new unparented character literal node. |
1840 | * Initially the node has an unspecified character literal. |
1841 | * |
1842 | * @return a new unparented character literal node |
1843 | */ |
1844 | public CharacterLiteral newCharacterLiteral() { |
1845 | return new CharacterLiteral(this); |
1846 | } |
1847 | |
1848 | /** |
1849 | * Creates and returns a new unparented class instance creation |
1850 | * ("new") expression node owned by this AST. By default, |
1851 | * there is no qualifying expression, no type parameters, |
1852 | * an unspecified (but legal) type name, an empty list of |
1853 | * arguments, and does not declare an anonymous class declaration. |
1854 | * |
1855 | * @return a new unparented class instance creation expression node |
1856 | */ |
1857 | public ClassInstanceCreation newClassInstanceCreation() { |
1858 | ClassInstanceCreation result = new ClassInstanceCreation(this); |
1859 | return result; |
1860 | } |
1861 | |
1862 | // =============================== DECLARATIONS =========================== |
1863 | /** |
1864 | * Creates an unparented compilation unit node owned by this AST. |
1865 | * The compilation unit initially has no package declaration, no |
1866 | * import declarations, and no type declarations. |
1867 | * |
1868 | * @return the new unparented compilation unit node |
1869 | */ |
1870 | public CompilationUnit newCompilationUnit() { |
1871 | return new CompilationUnit(this); |
1872 | } |
1873 | |
1874 | /** |
1875 | * Creates and returns a new unparented conditional expression node |
1876 | * owned by this AST. By default, the condition and both expressions |
1877 | * are unspecified (but legal). |
1878 | * |
1879 | * @return a new unparented array conditional expression node |
1880 | */ |
1881 | public ConditionalExpression newConditionalExpression() { |
1882 | ConditionalExpression result = new ConditionalExpression(this); |
1883 | return result; |
1884 | } |
1885 | |
1886 | /** |
1887 | * Creates an unparented alternate constructor ("this(...);") invocation |
1888 | * statement node owned by this AST. By default, the lists of arguments |
1889 | * and type arguments are both empty. |
1890 | * <p> |
1891 | * Note that this type of node is a Statement, whereas a regular |
1892 | * method invocation is an Expression. The only valid use of these |
1893 | * statements are as the first statement of a constructor body. |
1894 | * </p> |
1895 | * |
1896 | * @return a new unparented alternate constructor invocation statement node |
1897 | */ |
1898 | public ConstructorInvocation newConstructorInvocation() { |
1899 | ConstructorInvocation result = new ConstructorInvocation(this); |
1900 | return result; |
1901 | } |
1902 | |
1903 | /** |
1904 | * Creates an unparented continue statement node owned by this AST. |
1905 | * The continue statement has no label. |
1906 | * |
1907 | * @return a new unparented continue statement node |
1908 | */ |
1909 | public ContinueStatement newContinueStatement() { |
1910 | return new ContinueStatement(this); |
1911 | } |
1912 | |
1913 | /** |
1914 | * Creates an unparented creation reference node owned by this AST. |
1915 | * By default, the type is unspecified (but legal), and there are no type |
1916 | * arguments. |
1917 | * |
1918 | * @return a new unparented creation reference expression node |
1919 | * @exception UnsupportedOperationException if this operation is used in a JLS2, |
1920 | * JLS3 or JLS4 AST |
1921 | * @since 3.10 |
1922 | */ |
1923 | public CreationReference newCreationReference() { |
1924 | CreationReference result = new CreationReference(this); |
1925 | return result; |
1926 | } |
1927 | |
1928 | /** |
1929 | * Creates a new unparented do statement node owned by this AST. |
1930 | * By default, the expression is unspecified (but legal), and |
1931 | * the body statement is an empty block. |
1932 | * |
1933 | * @return a new unparented do statement node |
1934 | */ |
1935 | public DoStatement newDoStatement() { |
1936 | return new DoStatement(this); |
1937 | } |
1938 | |
1939 | /** |
1940 | * Creates a new unparented empty statement node owned by this AST. |
1941 | * |
1942 | * @return a new unparented empty statement node |
1943 | */ |
1944 | public EmptyStatement newEmptyStatement() { |
1945 | return new EmptyStatement(this); |
1946 | } |
1947 | |
1948 | /** |
1949 | * Creates a new unparented enhanced for statement node owned by this AST. |
1950 | * By default, the paramter and expression are unspecified |
1951 | * but legal subtrees, and the body is an empty block. |
1952 | * |
1953 | * @return a new unparented throw statement node |
1954 | * @exception UnsupportedOperationException if this operation is used in |
1955 | * a JLS2 AST |
1956 | * @since 3.1 |
1957 | */ |
1958 | public EnhancedForStatement newEnhancedForStatement() { |
1959 | return new EnhancedForStatement(this); |
1960 | } |
1961 | |
1962 | /** |
1963 | * Creates an unparented enum constant declaration node owned by this AST. |
1964 | * The name of the constant is an unspecified, but legal, name; |
1965 | * no doc comment; no modifiers or annotations; no arguments; |
1966 | * and does not declare an anonymous class. |
1967 | * |
1968 | * @return a new unparented enum constant declaration node |
1969 | * @exception UnsupportedOperationException if this operation is used in |
1970 | * a JLS2 AST |
1971 | * @since 3.1 |
1972 | */ |
1973 | public EnumConstantDeclaration newEnumConstantDeclaration() { |
1974 | EnumConstantDeclaration result = new EnumConstantDeclaration(this); |
1975 | return result; |
1976 | } |
1977 | |
1978 | /** |
1979 | * Creates an unparented enum declaration node owned by this AST. |
1980 | * The name of the enum is an unspecified, but legal, name; |
1981 | * no doc comment; no modifiers or annotations; |
1982 | * no superinterfaces; and empty lists of enum constants |
1983 | * and body declarations. |
1984 | * |
1985 | * @return a new unparented enum declaration node |
1986 | * @exception UnsupportedOperationException if this operation is used in |
1987 | * a JLS2 AST |
1988 | * @since 3.1 |
1989 | */ |
1990 | public EnumDeclaration newEnumDeclaration() { |
1991 | EnumDeclaration result = new EnumDeclaration(this); |
1992 | return result; |
1993 | } |
1994 | |
1995 | /** |
1996 | * Creates and returns a new unparented exports directive |
1997 | * node for an unspecified, but legal, name; no target modules |
1998 | * |
1999 | * @return a new unparented exports directive node |
2000 | * @exception UnsupportedOperationException if this operation is used in an AST |
2001 | * with level less than JLS9 |
2002 | * @since 3.14 |
2003 | */ |
2004 | public ExportsDirective newExportsStatement() { |
2005 | ExportsDirective result = new ExportsDirective(this); |
2006 | return result; |
2007 | } |
2008 | |
2009 | /** |
2010 | * Creates an unparented expression method reference node owned by this AST. |
2011 | * By default, the expression and method name are unspecified (but legal), |
2012 | * and there are no type arguments. |
2013 | * |
2014 | * @return a new unparented expression method reference expression node |
2015 | * @exception UnsupportedOperationException if this operation is used in a JLS2, |
2016 | * JLS3 or JLS4 AST |
2017 | * @since 3.10 |
2018 | */ |
2019 | public ExpressionMethodReference newExpressionMethodReference() { |
2020 | ExpressionMethodReference result = new ExpressionMethodReference(this); |
2021 | return result; |
2022 | } |
2023 | |
2024 | /** |
2025 | * Creates a new unparented expression statement node owned by this AST, |
2026 | * for the given expression. |
2027 | * <p> |
2028 | * This method can be used to convert an expression |
2029 | * (<code>Expression</code>) into a statement (<code>Type</code>) |
2030 | * by wrapping it. Note, however, that the result is only legal for |
2031 | * limited expression types, including method invocations, assignments, |
2032 | * and increment/decrement operations. |
2033 | * </p> |
2034 | * |
2035 | * @param expression the expression |
2036 | * @return a new unparented statement node |
2037 | * @exception IllegalArgumentException if: |
2038 | * <ul> |
2039 | * <li>the node belongs to a different |
2040 | * AST</li> |
2041 | * <li>the node already has a parent</li> |
2042 | * </ul> |
2043 | */ |
2044 | public ExpressionStatement newExpressionStatement(Expression expression) { |
2045 | ExpressionStatement result = new ExpressionStatement(this); |
2046 | result.setExpression(expression); |
2047 | return result; |
2048 | } |
2049 | |
2050 | /** |
2051 | * Creates and returns a new unparented annotatable dimension node |
2052 | * (Supported only in JLS8 level). |
2053 | * |
2054 | * @return a new unparented annotatable dimension node |
2055 | * @exception IllegalArgumentException if: |
2056 | * <ul> |
2057 | * <li>the node belongs to a different |
2058 | * AST</li> |
2059 | * <li>the node already has a |
2060 | * parent</li> |
2061 | * </ul> |
2062 | * @exception UnsupportedOperationException if this operation is used |
2063 | * in a JLS2, JLS3 or JLS4 AST |
2064 | * @since 3.10 |
2065 | */ |
2066 | public Dimension newDimension() { |
2067 | Dimension result = new Dimension(this); |
2068 | return result; |
2069 | } |
2070 | |
2071 | /** |
2072 | * Creates and returns a new unparented field access expression node |
2073 | * owned by this AST. By default, the expression and field are both |
2074 | * unspecified, but legal, names. |
2075 | * |
2076 | * @return a new unparented field access expression node |
2077 | */ |
2078 | public FieldAccess newFieldAccess() { |
2079 | FieldAccess result = new FieldAccess(this); |
2080 | return result; |
2081 | } |
2082 | |
2083 | /** |
2084 | * Creates a new unparented field declaration node owned by this AST, |
2085 | * for the given variable declaration fragment. By default, there are no |
2086 | * modifiers, no doc comment, and the base type is unspecified |
2087 | * (but legal). |
2088 | * <p> |
2089 | * This method can be used to wrap a variable declaration fragment |
2090 | * (<code>VariableDeclarationFragment</code>) into a field declaration |
2091 | * suitable for inclusion in the body of a type declaration |
2092 | * (<code>FieldDeclaration</code> implements <code>BodyDeclaration</code>). |
2093 | * Additional variable declaration fragments can be added afterwards. |
2094 | * </p> |
2095 | * |
2096 | * @param fragment the variable declaration fragment |
2097 | * @return a new unparented field declaration node |
2098 | * @exception IllegalArgumentException if: |
2099 | * <ul> |
2100 | * <li>the node belongs to a different |
2101 | * AST</li> |
2102 | * <li>the node already has a parent</li> |
2103 | * <li>the given fragment is null</li> |
2104 | * </ul> |
2105 | */ |
2106 | public FieldDeclaration newFieldDeclaration(VariableDeclarationFragment fragment) { |
2107 | if (fragment == null) { |
2108 | throw new IllegalArgumentException(); |
2109 | } |
2110 | FieldDeclaration result = new FieldDeclaration(this); |
2111 | result.fragments().add(fragment); |
2112 | return result; |
2113 | } |
2114 | |
2115 | /** |
2116 | * Creates a new unparented for statement node owned by this AST. |
2117 | * By default, there are no initializers, no condition expression, |
2118 | * no updaters, and the body is an empty block. |
2119 | * |
2120 | * @return a new unparented for statement node |
2121 | */ |
2122 | public ForStatement newForStatement() { |
2123 | return new ForStatement(this); |
2124 | } |
2125 | |
2126 | /** |
2127 | * Creates and returns a new unparented guarded pattern node with an |
2128 | * unspecified pattern variable name and a null expression. |
2129 | * |
2130 | * @return a new unparented guarded pattern node |
2131 | * @since 3.28 |
2132 | */ |
2133 | public GuardedPattern newGuardedPattern() { |
2134 | GuardedPattern result = new GuardedPattern(this); |
2135 | return result; |
2136 | } |
2137 | |
2138 | /** |
2139 | * Creates a new unparented if statement node owned by this AST. |
2140 | * By default, the expression is unspecified (but legal), |
2141 | * the then statement is an empty block, and there is no else statement. |
2142 | * |
2143 | * @return a new unparented if statement node |
2144 | */ |
2145 | public IfStatement newIfStatement() { |
2146 | return new IfStatement(this); |
2147 | } |
2148 | |
2149 | /** |
2150 | * Creates an unparented import declaration node owned by this AST. |
2151 | * The import declaration initially contains a single-type import |
2152 | * of a type with an unspecified name. |
2153 | * |
2154 | * @return the new unparented import declaration node |
2155 | */ |
2156 | public ImportDeclaration newImportDeclaration() { |
2157 | ImportDeclaration result = new ImportDeclaration(this); |
2158 | return result; |
2159 | } |
2160 | |
2161 | /** |
2162 | * Creates and returns a new unparented infix expression node |
2163 | * owned by this AST. By default, the operator and left and right |
2164 | * operand are unspecified (but legal), and there are no extended |
2165 | * operands. |
2166 | * |
2167 | * @return a new unparented infix expression node |
2168 | */ |
2169 | public InfixExpression newInfixExpression() { |
2170 | InfixExpression result = new InfixExpression(this); |
2171 | return result; |
2172 | } |
2173 | |
2174 | /** |
2175 | * Creates an unparented initializer node owned by this AST, with an |
2176 | * empty block. By default, the initializer has no modifiers and |
2177 | * an empty block. |
2178 | * |
2179 | * @return a new unparented initializer node |
2180 | */ |
2181 | public Initializer newInitializer() { |
2182 | Initializer result = new Initializer(this); |
2183 | return result; |
2184 | } |
2185 | |
2186 | /** |
2187 | * Creates and returns a new unparented instanceof expression node |
2188 | * owned by this AST. By default, the operator and left and right |
2189 | * operand are unspecified (but legal). |
2190 | * |
2191 | * @return a new unparented instanceof expression node |
2192 | */ |
2193 | public InstanceofExpression newInstanceofExpression() { |
2194 | InstanceofExpression result = new InstanceofExpression(this); |
2195 | return result; |
2196 | } |
2197 | |
2198 | /** |
2199 | * Creates and returns a new doc comment node. |
2200 | * Initially the new node has an empty list of tag elements |
2201 | * (and, for backwards compatability, an unspecified, but legal, |
2202 | * doc comment string) |
2203 | * |
2204 | * @return a new unparented doc comment node |
2205 | */ |
2206 | public Javadoc newJavadoc() { |
2207 | Javadoc result = new Javadoc(this); |
2208 | return result; |
2209 | } |
2210 | |
2211 | /** |
2212 | * Creates and returns a new doc comment region node. |
2213 | * Initially the new node has an empty list of tag elements |
2214 | * (and, for backwards compatability, an unspecified, but legal, |
2215 | * doc comment string) |
2216 | * |
2217 | * @return a new unparented doc comment node |
2218 | * @since 3.30 |
2219 | */ |
2220 | public JavaDocRegion newJavaDocRegion() { |
2221 | JavaDocRegion result = new JavaDocRegion(this); |
2222 | return result; |
2223 | } |
2224 | |
2225 | /** |
2226 | * Creates a new unparented labeled statement node owned by this AST. |
2227 | * By default, the label and statement are both unspecified, but legal. |
2228 | * |
2229 | * @return a new unparented labeled statement node |
2230 | */ |
2231 | public LabeledStatement newLabeledStatement() { |
2232 | return new LabeledStatement(this); |
2233 | } |
2234 | |
2235 | /** |
2236 | * Creates an unparented lambda expression node owned by this AST. |
2237 | * By default, the new lambda expression has parentheses enabled, contains an |
2238 | * empty argument |
2239 | * list, and the body is an empty block. |
2240 | * |
2241 | * @return a new unparented lambda expression node |
2242 | * @exception UnsupportedOperationException if this operation is used in a JLS2, |
2243 | * JLS3 or JLS4 AST |
2244 | * @since 3.10 |
2245 | */ |
2246 | public LambdaExpression newLambdaExpression() { |
2247 | LambdaExpression result = new LambdaExpression(this); |
2248 | return result; |
2249 | } |
2250 | |
2251 | /** |
2252 | * Creates and returns a new line comment placeholder node. |
2253 | * <p> |
2254 | * Note that this node type is used to recording the source |
2255 | * range where a comment was found in the source string. |
2256 | * These comment nodes are normally found (only) in |
2257 | * {@linkplain CompilationUnit#getCommentList() |
2258 | * the comment table} for parsed compilation units. |
2259 | * </p> |
2260 | * |
2261 | * @return a new unparented line comment node |
2262 | * @since 3.0 |
2263 | */ |
2264 | public LineComment newLineComment() { |
2265 | LineComment result = new LineComment(this); |
2266 | return result; |
2267 | } |
2268 | |
2269 | /** |
2270 | * Creates and returns a new unparented marker annotation node with |
2271 | * an unspecified type name. |
2272 | * |
2273 | * @return a new unparented marker annotation node |
2274 | * @exception UnsupportedOperationException if this operation is used in |
2275 | * a JLS2 AST |
2276 | * @since 3.1 |
2277 | */ |
2278 | public MarkerAnnotation newMarkerAnnotation() { |
2279 | MarkerAnnotation result = new MarkerAnnotation(this); |
2280 | return result; |
2281 | } |
2282 | |
2283 | /** |
2284 | * Creates and returns a new member reference node. |
2285 | * Initially the new node has no qualifier name and |
2286 | * an unspecified, but legal, member name. |
2287 | * <p> |
2288 | * Note that this node type is used only inside doc comments |
2289 | * ({@link Javadoc}). |
2290 | * </p> |
2291 | * |
2292 | * @return a new unparented member reference node |
2293 | * @since 3.0 |
2294 | */ |
2295 | public MemberRef newMemberRef() { |
2296 | MemberRef result = new MemberRef(this); |
2297 | return result; |
2298 | } |
2299 | |
2300 | // =============================== COMMENTS =========================== |
2301 | |
2302 | /** |
2303 | * Creates and returns a new unparented member value pair node with |
2304 | * an unspecified member name and value. |
2305 | * |
2306 | * @return a new unparented member value pair node |
2307 | * @exception UnsupportedOperationException if this operation is used in |
2308 | * a JLS2 AST |
2309 | * @since 3.1 |
2310 | */ |
2311 | public MemberValuePair newMemberValuePair() { |
2312 | MemberValuePair result = new MemberValuePair(this); |
2313 | return result; |
2314 | } |
2315 | |
2316 | /** |
2317 | * Creates an unparented method declaration node owned by this AST. |
2318 | * By default, the declaration is for a method of an unspecified, but |
2319 | * legal, name; no modifiers; no doc comment; no parameters; return |
2320 | * type void; no extra array dimensions; no thrown exceptions; and no |
2321 | * body (as opposed to an empty body). |
2322 | * <p> |
2323 | * To create a constructor, use this method and then call |
2324 | * <code>MethodDeclaration.setConstructor(true)</code> and |
2325 | * <code>MethodDeclaration.setName(className)</code>. |
2326 | * </p> |
2327 | * |
2328 | * @return a new unparented method declaration node |
2329 | */ |
2330 | public MethodDeclaration newMethodDeclaration() { |
2331 | MethodDeclaration result = new MethodDeclaration(this); |
2332 | result.setConstructor(false); |
2333 | return result; |
2334 | } |
2335 | |
2336 | /** |
2337 | * Creates an unparented method invocation expression node owned by this |
2338 | * AST. By default, the name of the method is unspecified (but legal) |
2339 | * there is no receiver expression, no type arguments, and the list of |
2340 | * arguments is empty. |
2341 | * |
2342 | * @return a new unparented method invocation expression node |
2343 | */ |
2344 | public MethodInvocation newMethodInvocation() { |
2345 | MethodInvocation result = new MethodInvocation(this); |
2346 | return result; |
2347 | } |
2348 | |
2349 | /** |
2350 | * Creates and returns a new method reference node. |
2351 | * Initially the new node has no qualifier name, |
2352 | * an unspecified, but legal, method name, and an |
2353 | * empty parameter list. |
2354 | * <p> |
2355 | * Note that this node type is used only inside doc comments |
2356 | * ({@link Javadoc Javadoc}). |
2357 | * </p> |
2358 | * |
2359 | * @return a new unparented method reference node |
2360 | * @since 3.0 |
2361 | */ |
2362 | public MethodRef newMethodRef() { |
2363 | MethodRef result = new MethodRef(this); |
2364 | return result; |
2365 | } |
2366 | |
2367 | /** |
2368 | * Creates and returns a new method reference node. |
2369 | * Initially the new node has an unspecified, but legal, |
2370 | * type, not variable arity, and no parameter name. |
2371 | * <p> |
2372 | * Note that this node type is used only inside doc comments |
2373 | * ({@link Javadoc}). |
2374 | * </p> |
2375 | * |
2376 | * @return a new unparented method reference parameter node |
2377 | * @since 3.0 |
2378 | */ |
2379 | public MethodRefParameter newMethodRefParameter() { |
2380 | MethodRefParameter result = new MethodRefParameter(this); |
2381 | return result; |
2382 | } |
2383 | |
2384 | /** |
2385 | * Creates and returns a new unparented modifier node for the given |
2386 | * modifier. |
2387 | * |
2388 | * @param keyword one of the modifier keyword constants |
2389 | * @return a new unparented modifier node |
2390 | * @exception IllegalArgumentException if the primitive type code is |
2391 | * invalid |
2392 | * @exception UnsupportedOperationException if this operation is used in |
2393 | * a JLS2 AST |
2394 | * @since 3.1 |
2395 | */ |
2396 | public Modifier newModifier(Modifier.ModifierKeyword keyword) { |
2397 | Modifier result = new Modifier(this); |
2398 | result.setKeyword(keyword); |
2399 | return result; |
2400 | } |
2401 | |
2402 | /** |
2403 | * Creates and returns a new unparented module modifier node for the given |
2404 | * module modifier. |
2405 | * |
2406 | * @param keyword one of the module modifier keyword constants |
2407 | * @return a new unparented module modifier node |
2408 | * @exception IllegalArgumentException if the primitive type code is |
2409 | * invalid |
2410 | * @exception UnsupportedOperationException if this operation is used in |
2411 | * an AST with level less than JLS9 |
2412 | * @since 3.14 |
2413 | */ |
2414 | public ModuleModifier newModuleModifier(ModuleModifier.ModuleModifierKeyword keyword) { |
2415 | ModuleModifier result = new ModuleModifier(this); |
2416 | result.setKeyword(keyword); |
2417 | return result; |
2418 | } |
2419 | |
2420 | /** |
2421 | * Creates and returns a list of new unparented modifier nodes |
2422 | * for the given modifier flags. When multiple modifiers are |
2423 | * requested, the modifier nodes will appear in the following order: |
2424 | * |
2425 | * <pre> |
2426 | * public protected private |
2427 | * abstract default static final synchronized native strictfp transient volatile |
2428 | * </pre> |
2429 | * <p> |
2430 | * This order is consistent with the recommendations in JLS8 ("*Modifier:" rules |
2431 | * in chapters 8 and 9). |
2432 | * </p> |
2433 | * |
2434 | * @param flags bitwise or of modifier flags declared on {@link Modifier} |
2435 | * @return a possibly empty list of new unparented modifier nodes |
2436 | * (element type <code>Modifier</code>) |
2437 | * @exception UnsupportedOperationException if this operation is used in |
2438 | * a JLS2 AST |
2439 | * @since 3.1 |
2440 | */ |
2441 | public List newModifiers(int flags) { |
2442 | if (this.apiLevel == AST.JLS2) { |
2443 | unsupportedIn2(); |
2444 | } |
2445 | List result = new ArrayList(3); // 3 modifiers is more than average |
2446 | if (Modifier.isPublic(flags)) { |
2447 | result.add(newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); |
2448 | } |
2449 | if (Modifier.isProtected(flags)) { |
2450 | result.add(newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD)); |
2451 | } |
2452 | if (Modifier.isPrivate(flags)) { |
2453 | result.add(newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); |
2454 | } |
2455 | if (Modifier.isAbstract(flags)) { |
2456 | result.add(newModifier(Modifier.ModifierKeyword.ABSTRACT_KEYWORD)); |
2457 | } |
2458 | if (Modifier.isDefault(flags)) { |
2459 | result.add(newModifier(Modifier.ModifierKeyword.DEFAULT_KEYWORD)); |
2460 | } |
2461 | if (Modifier.isStatic(flags)) { |
2462 | result.add(newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); |
2463 | } |
2464 | if (Modifier.isFinal(flags)) { |
2465 | result.add(newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); |
2466 | } |
2467 | if (Modifier.isSynchronized(flags)) { |
2468 | result.add(newModifier(Modifier.ModifierKeyword.SYNCHRONIZED_KEYWORD)); |
2469 | } |
2470 | if (Modifier.isNative(flags)) { |
2471 | result.add(newModifier(Modifier.ModifierKeyword.NATIVE_KEYWORD)); |
2472 | } |
2473 | if (Modifier.isStrictfp(flags)) { |
2474 | result.add(newModifier(Modifier.ModifierKeyword.STRICTFP_KEYWORD)); |
2475 | } |
2476 | if (Modifier.isTransient(flags)) { |
2477 | result.add(newModifier(Modifier.ModifierKeyword.TRANSIENT_KEYWORD)); |
2478 | } |
2479 | if (Modifier.isVolatile(flags)) { |
2480 | result.add(newModifier(Modifier.ModifierKeyword.VOLATILE_KEYWORD)); |
2481 | } |
2482 | if (Modifier.isSealed(flags)) { |
2483 | result.add(newModifier(Modifier.ModifierKeyword.SEALED_KEYWORD)); |
2484 | } |
2485 | if (Modifier.isNonSealed(flags)) { |
2486 | result.add(newModifier(Modifier.ModifierKeyword.NON_SEALED_KEYWORD)); |
2487 | } |
2488 | return result; |
2489 | } |
2490 | |
2491 | /** |
2492 | * Creates and returns a new unparented module declaration |
2493 | * node for an unspecified, but legal, name; no modifiers; no javadoc; |
2494 | * and an empty list of statements. |
2495 | * |
2496 | * @return a new unparented module declaration node |
2497 | * @exception UnsupportedOperationException if this operation is used in an AST |
2498 | * with level less than JLS9 |
2499 | * @since 3.14 |
2500 | */ |
2501 | public ModuleDeclaration newModuleDeclaration() { |
2502 | ModuleDeclaration result = new ModuleDeclaration(this); |
2503 | return result; |
2504 | } |
2505 | |
2506 | /** |
2507 | * Creates and returns a new unparented name node for the given name. |
2508 | * The name string must consist of 1 or more name segments separated |
2509 | * by single dots '.'. Returns a {@link QualifiedName} if the name has |
2510 | * dots, and a {@link SimpleName} otherwise. Each of the name |
2511 | * segments should be legal Java identifiers (this constraint may or may |
2512 | * not be enforced), and there must be at least one name segment. |
2513 | * The string must not contains white space, '<', '>', |
2514 | * '[', ']', or other any other characters that are not |
2515 | * part of the Java identifiers or separating '.'s. |
2516 | * |
2517 | * @param qualifiedName string consisting of 1 or more name segments, |
2518 | * each of which is a legal Java identifier, separated by |
2519 | * single dots '.' |
2520 | * @return a new unparented name node |
2521 | * @exception IllegalArgumentException if: |
2522 | * <ul> |
2523 | * <li>the string is empty</li> |
2524 | * <li>the string begins or ends in a |
2525 | * '.'</li> |
2526 | * <li>the string has adjacent '.'s</li> |
2527 | * <li>the segments between the '.'s are not |
2528 | * valid Java identifiers</li> |
2529 | * </ul> |
2530 | * @since 3.1 |
2531 | */ |
2532 | public Name newName(String qualifiedName) { |
2533 | StringTokenizer t = new StringTokenizer(qualifiedName, ".", true); //$NON-NLS-1$ |
2534 | Name result = null; |
2535 | // balance is # of name tokens - # of period tokens seen so far |
2536 | // initially 0; finally 1; should never drop < 0 or > 1 |
2537 | int balance = 0; |
2538 | while (t.hasMoreTokens()) { |
2539 | String s = t.nextToken(); |
2540 | if (s.indexOf('.') >= 0) { |
2541 | // this is a delimiter |
2542 | if (s.length() > 1) { |
2543 | // too many dots in a row |
2544 | throw new IllegalArgumentException(); |
2545 | } |
2546 | balance--; |
2547 | if (balance < 0) { |
2548 | throw new IllegalArgumentException(); |
2549 | } |
2550 | } else { |
2551 | // this is an identifier segment |
2552 | balance++; |
2553 | SimpleName name = newSimpleName(s); |
2554 | if (result == null) { |
2555 | result = name; |
2556 | } else { |
2557 | result = newQualifiedName(result, name); |
2558 | } |
2559 | } |
2560 | } |
2561 | if (balance != 1) { |
2562 | throw new IllegalArgumentException(); |
2563 | } |
2564 | return result; |
2565 | } |
2566 | |
2567 | /** |
2568 | * Creates and returns a new unparented name node for the given name |
2569 | * segments. Returns a simple name if there is only one name segment, and |
2570 | * a qualified name if there are multiple name segments. Each of the name |
2571 | * segments should be legal Java identifiers (this constraint may or may |
2572 | * not be enforced), and there must be at least one name segment. |
2573 | * |
2574 | * @param identifiers a list of 1 or more name segments, each of which |
2575 | * is a legal Java identifier |
2576 | * @return a new unparented name node |
2577 | * @exception IllegalArgumentException if: |
2578 | * <ul> |
2579 | * <li>the identifier is invalid</li> |
2580 | * <li>the list of identifiers is empty</li> |
2581 | * </ul> |
2582 | */ |
2583 | public Name newName(String[] identifiers) { |
2584 | // update internalSetName(String[] if changed |
2585 | int count = identifiers.length; |
2586 | if (count == 0) { |
2587 | throw new IllegalArgumentException(); |
2588 | } |
2589 | Name result = newSimpleName(identifiers[0]); |
2590 | for (int i = 1; i < count; i++) { |
2591 | SimpleName name = newSimpleName(identifiers[i]); |
2592 | result = newQualifiedName(result, name); |
2593 | } |
2594 | return result; |
2595 | } |
2596 | |
2597 | /** |
2598 | * Creates and returns a new unparented name qualified type node with |
2599 | * the given qualifier and name. |
2600 | * |
2601 | * @param qualifier the name qualifier name node |
2602 | * @param name the simple name being qualified |
2603 | * @return a new unparented qualified type node |
2604 | * @exception IllegalArgumentException if: |
2605 | * <ul> |
2606 | * <li>the node belongs to a different |
2607 | * AST</li> |
2608 | * <li>the node already has a |
2609 | * parent</li> |
2610 | * </ul> |
2611 | * @exception UnsupportedOperationException if this operation is used in |
2612 | * a JLS2, JLS3 and JLS4 AST |
2613 | * @since 3.10 |
2614 | */ |
2615 | public NameQualifiedType newNameQualifiedType(Name qualifier, SimpleName name) { |
2616 | NameQualifiedType result = new NameQualifiedType(this); |
2617 | result.setQualifier(qualifier); |
2618 | result.setName(name); |
2619 | return result; |
2620 | } |
2621 | |
2622 | /** |
2623 | * Creates and returns a new unparented normal annotation node with |
2624 | * an unspecified type name and an empty list of member value |
2625 | * pairs. |
2626 | * |
2627 | * @return a new unparented normal annotation node |
2628 | * @exception UnsupportedOperationException if this operation is used in |
2629 | * a JLS2 AST |
2630 | * @since 3.1 |
2631 | */ |
2632 | public NormalAnnotation newNormalAnnotation() { |
2633 | NormalAnnotation result = new NormalAnnotation(this); |
2634 | return result; |
2635 | } |
2636 | |
2637 | /** |
2638 | * Creates and returns a new unparented null literal node. |
2639 | * |
2640 | * @return a new unparented null literal node |
2641 | */ |
2642 | public NullLiteral newNullLiteral() { |
2643 | return new NullLiteral(this); |
2644 | } |
2645 | |
2646 | /** |
2647 | * Creates and returns a new unparented null pattern node . |
2648 | * |
2649 | * @return a new unparented null pattern node |
2650 | * @since 3.28 |
2651 | */ |
2652 | public NullPattern newNullPattern() { |
2653 | NullPattern result = new NullPattern(this); |
2654 | return result; |
2655 | } |
2656 | |
2657 | /** |
2658 | * Creates and returns a new unparented number literal node. |
2659 | * Initially the number literal token is <code>"0"</code>. |
2660 | * |
2661 | * @return a new unparented number literal node |
2662 | */ |
2663 | public NumberLiteral newNumberLiteral() { |
2664 | NumberLiteral result = new NumberLiteral(this); |
2665 | return result; |
2666 | } |
2667 | |
2668 | /** |
2669 | * Creates and returns a new unparented number literal node. |
2670 | * |
2671 | * @param literal the token for the numeric literal as it would |
2672 | * appear in Java source code |
2673 | * @return a new unparented number literal node |
2674 | * @exception IllegalArgumentException if the literal is null |
2675 | */ |
2676 | public NumberLiteral newNumberLiteral(String literal) { |
2677 | if (literal == null) { |
2678 | throw new IllegalArgumentException(); |
2679 | } |
2680 | NumberLiteral result = new NumberLiteral(this); |
2681 | result.setToken(literal); |
2682 | return result; |
2683 | } |
2684 | |
2685 | /** |
2686 | * Creates and returns a new unparented opens directive |
2687 | * node for an unspecified, but legal, name; no target modules |
2688 | * |
2689 | * @return a new unparented opens directive node |
2690 | * @exception UnsupportedOperationException if this operation is used in an AST |
2691 | * with level less than JLS9 |
2692 | * @since 3.14 |
2693 | */ |
2694 | public OpensDirective newOpensDirective() { |
2695 | OpensDirective result = new OpensDirective(this); |
2696 | return result; |
2697 | } |
2698 | |
2699 | /** |
2700 | * Creates an unparented package declaration node owned by this AST. |
2701 | * The package declaration initially declares a package with an |
2702 | * unspecified name. |
2703 | * |
2704 | * @return the new unparented package declaration node |
2705 | */ |
2706 | public PackageDeclaration newPackageDeclaration() { |
2707 | PackageDeclaration result = new PackageDeclaration(this); |
2708 | return result; |
2709 | } |
2710 | |
2711 | /** |
2712 | * Creates and returns a new unparented parameterized type node with the |
2713 | * given type and an empty list of type arguments. |
2714 | * |
2715 | * @param type the type that is parameterized |
2716 | * @return a new unparented parameterized type node |
2717 | * @exception IllegalArgumentException if: |
2718 | * <ul> |
2719 | * <li>the node belongs to a different |
2720 | * AST</li> |
2721 | * <li>the node already has a |
2722 | * parent</li> |
2723 | * </ul> |
2724 | * @exception UnsupportedOperationException if this operation is used in |
2725 | * a JLS2 AST |
2726 | * @since 3.1 |
2727 | */ |
2728 | public ParameterizedType newParameterizedType(Type type) { |
2729 | ParameterizedType result = new ParameterizedType(this); |
2730 | result.setType(type); |
2731 | return result; |
2732 | } |
2733 | |
2734 | /** |
2735 | * Creates and returns a new unparented parenthesized expression node |
2736 | * owned by this AST. By default, the expression is unspecified (but legal). |
2737 | * |
2738 | * @return a new unparented parenthesized expression node |
2739 | */ |
2740 | public ParenthesizedExpression newParenthesizedExpression() { |
2741 | ParenthesizedExpression result = new ParenthesizedExpression(this); |
2742 | return result; |
2743 | } |
2744 | |
2745 | /** |
2746 | * Creates and returns a new unparented instanceof expression node |
2747 | * owned by this AST. By default, the operator and left and right |
2748 | * operand are unspecified (but legal). |
2749 | * |
2750 | * @return a new unparented instanceof expression node |
2751 | * @since 3.26 |
2752 | */ |
2753 | public PatternInstanceofExpression newPatternInstanceofExpression() { |
2754 | PatternInstanceofExpression result = new PatternInstanceofExpression(this); |
2755 | return result; |
2756 | } |
2757 | |
2758 | /** |
2759 | * Creates and returns a new unparented postfix expression node |
2760 | * owned by this AST. By default, the operator and operand are |
2761 | * unspecified (but legal). |
2762 | * |
2763 | * @return a new unparented postfix expression node |
2764 | */ |
2765 | public PostfixExpression newPostfixExpression() { |
2766 | PostfixExpression result = new PostfixExpression(this); |
2767 | return result; |
2768 | } |
2769 | |
2770 | /** |
2771 | * Creates and returns a new unparented prefix expression node |
2772 | * owned by this AST. By default, the operator and operand are |
2773 | * unspecified (but legal). |
2774 | * |
2775 | * @return a new unparented prefix expression node |
2776 | */ |
2777 | public PrefixExpression newPrefixExpression() { |
2778 | PrefixExpression result = new PrefixExpression(this); |
2779 | return result; |
2780 | } |
2781 | |
2782 | /** |
2783 | * Creates and returns a new unparented primitive type node with the given |
2784 | * type code. |
2785 | * |
2786 | * @param typeCode one of the primitive type code constants declared in |
2787 | * <code>PrimitiveType</code> |
2788 | * @return a new unparented primitive type node |
2789 | * @exception IllegalArgumentException if the primitive type code is invalid |
2790 | */ |
2791 | public PrimitiveType newPrimitiveType(PrimitiveType.Code typeCode) { |
2792 | PrimitiveType result = new PrimitiveType(this); |
2793 | result.setPrimitiveTypeCode(typeCode); |
2794 | return result; |
2795 | } |
2796 | |
2797 | /** |
2798 | * Creates and returns a new unparented provides directive |
2799 | * node for an unspecified, but legal, type; no target types |
2800 | * |
2801 | * @return a new unparented provides directive node |
2802 | * @exception UnsupportedOperationException if this operation is used in an AST |
2803 | * with level less than JLS9 |
2804 | * @since 3.14 |
2805 | */ |
2806 | public ProvidesDirective newProvidesDirective() { |
2807 | ProvidesDirective result = new ProvidesDirective(this); |
2808 | return result; |
2809 | } |
2810 | |
2811 | /** |
2812 | * Creates and returns a new unparented qualified name node for the given |
2813 | * qualifier and simple name child node. |
2814 | * |
2815 | * @param qualifier the qualifier name node |
2816 | * @param name the simple name being qualified |
2817 | * @return a new unparented qualified name node |
2818 | * @exception IllegalArgumentException if: |
2819 | * <ul> |
2820 | * <li>the node belongs to a different |
2821 | * AST</li> |
2822 | * <li>the node already has a parent</li> |
2823 | * </ul> |
2824 | */ |
2825 | public QualifiedName newQualifiedName( |
2826 | Name qualifier, |
2827 | SimpleName name) { |
2828 | QualifiedName result = new QualifiedName(this); |
2829 | result.setQualifier(qualifier); |
2830 | result.setName(name); |
2831 | return result; |
2832 | |
2833 | } |
2834 | |
2835 | /** |
2836 | * Creates and returns a new unparented qualified type node with |
2837 | * the given qualifier type and name. |
2838 | * |
2839 | * @param qualifier the qualifier type node |
2840 | * @param name the simple name being qualified |
2841 | * @return a new unparented qualified type node |
2842 | * @exception IllegalArgumentException if: |
2843 | * <ul> |
2844 | * <li>the node belongs to a different |
2845 | * AST</li> |
2846 | * <li>the node already has a |
2847 | * parent</li> |
2848 | * </ul> |
2849 | * @exception UnsupportedOperationException if this operation is used in |
2850 | * a JLS2 AST |
2851 | * @since 3.1 |
2852 | */ |
2853 | public QualifiedType newQualifiedType(Type qualifier, SimpleName name) { |
2854 | QualifiedType result = new QualifiedType(this); |
2855 | result.setQualifier(qualifier); |
2856 | result.setName(name); |
2857 | return result; |
2858 | } |
2859 | |
2860 | /** |
2861 | * Creates an unparented record declaration node owned by this AST. |
2862 | * The name of the class is an unspecified, but legal, name; |
2863 | * no modifiers; no doc comment; no superclass or superinterfaces; |
2864 | * and an empty record body. |
2865 | * |
2866 | * @return a new unparented type declaration node |
2867 | * @exception UnsupportedOperationException if this operation is used in an AST |
2868 | * with level less than JLS16 |
2869 | * @since 3.23 |
2870 | */ |
2871 | public RecordDeclaration newRecordDeclaration() { |
2872 | RecordDeclaration result = new RecordDeclaration(this); |
2873 | return result; |
2874 | } |
2875 | |
2876 | /** |
2877 | * Creates and returns a new unparented requires directive |
2878 | * node for an unspecified, but legal, name; |
2879 | * |
2880 | * @return a new unparented requires directive node |
2881 | * @exception UnsupportedOperationException if this operation is used in an AST |
2882 | * with level less than JLS9 |
2883 | * @since 3.14 |
2884 | */ |
2885 | public RequiresDirective newRequiresDirective() { |
2886 | RequiresDirective result = new RequiresDirective(this); |
2887 | return result; |
2888 | } |
2889 | |
2890 | /** |
2891 | * Creates a new unparented return statement node owned by this AST. |
2892 | * By default, the return statement has no expression. |
2893 | * |
2894 | * @return a new unparented return statement node |
2895 | */ |
2896 | public ReturnStatement newReturnStatement() { |
2897 | return new ReturnStatement(this); |
2898 | } |
2899 | |
2900 | // =============================== NAMES =========================== |
2901 | /** |
2902 | * Creates and returns a new unparented simple name node for the given |
2903 | * identifier. The identifier should be a legal Java identifier, but not |
2904 | * a keyword, boolean literal ("true", "false") or null literal ("null"). |
2905 | * |
2906 | * @param identifier the identifier |
2907 | * @return a new unparented simple name node |
2908 | * @exception IllegalArgumentException if the identifier is invalid |
2909 | */ |
2910 | public SimpleName newSimpleName(String identifier) { |
2911 | if (identifier == null) { |
2912 | throw new IllegalArgumentException(); |
2913 | } |
2914 | SimpleName result = new SimpleName(this); |
2915 | result.setIdentifier(identifier); |
2916 | return result; |
2917 | } |
2918 | |
2919 | // =============================== TYPES =========================== |
2920 | /** |
2921 | * Creates and returns a new unparented simple type node with the given |
2922 | * type name. |
2923 | * <p> |
2924 | * This method can be used to convert a name (<code>Name</code>) into a |
2925 | * type (<code>Type</code>) by wrapping it. |
2926 | * </p> |
2927 | * |
2928 | * @param typeName the name of the class or interface |
2929 | * @return a new unparented simple type node |
2930 | * @exception IllegalArgumentException if: |
2931 | * <ul> |
2932 | * <li>the node belongs to a different |
2933 | * AST</li> |
2934 | * <li>the node already has a parent</li> |
2935 | * </ul> |
2936 | */ |
2937 | public SimpleType newSimpleType(Name typeName) { |
2938 | SimpleType result = new SimpleType(this); |
2939 | result.setName(typeName); |
2940 | return result; |
2941 | } |
2942 | |
2943 | /** |
2944 | * Creates and returns a new unparented single member annotation node with |
2945 | * an unspecified type name and value. |
2946 | * |
2947 | * @return a new unparented single member annotation node |
2948 | * @exception UnsupportedOperationException if this operation is used in |
2949 | * a JLS2 AST |
2950 | * @since 3.1 |
2951 | */ |
2952 | public SingleMemberAnnotation newSingleMemberAnnotation() { |
2953 | SingleMemberAnnotation result = new SingleMemberAnnotation(this); |
2954 | return result; |
2955 | } |
2956 | |
2957 | /** |
2958 | * Creates an unparented single variable declaration node owned by this AST. |
2959 | * By default, the declaration is for a variable with an unspecified, but |
2960 | * legal, name and type; no modifiers; no array dimensions after the |
2961 | * variable; no initializer; not variable arity. |
2962 | * |
2963 | * @return a new unparented single variable declaration node |
2964 | */ |
2965 | public SingleVariableDeclaration newSingleVariableDeclaration() { |
2966 | SingleVariableDeclaration result = new SingleVariableDeclaration(this); |
2967 | return result; |
2968 | } |
2969 | |
2970 | // =============================== EXPRESSIONS =========================== |
2971 | /** |
2972 | * Creates and returns a new unparented string literal node for |
2973 | * the empty string literal. |
2974 | * |
2975 | * @return a new unparented string literal node |
2976 | */ |
2977 | public StringLiteral newStringLiteral() { |
2978 | return new StringLiteral(this); |
2979 | } |
2980 | |
2981 | /** |
2982 | * Creates an unparented alternate super constructor ("super(...);") |
2983 | * invocation statement node owned by this AST. By default, there is no |
2984 | * qualifier, no type arguments, and the list of arguments is empty. |
2985 | * <p> |
2986 | * Note that this type of node is a Statement, whereas a regular |
2987 | * super method invocation is an Expression. The only valid use of these |
2988 | * statements are as the first statement of a constructor body. |
2989 | * </p> |
2990 | * |
2991 | * @return a new unparented super constructor invocation statement node |
2992 | */ |
2993 | public SuperConstructorInvocation newSuperConstructorInvocation() { |
2994 | SuperConstructorInvocation result = new SuperConstructorInvocation(this); |
2995 | return result; |
2996 | } |
2997 | |
2998 | /** |
2999 | * Creates and returns a new unparented super field access expression node |
3000 | * owned by this AST. By default, the expression and field are both |
3001 | * unspecified, but legal, names. |
3002 | * |
3003 | * @return a new unparented super field access expression node |
3004 | */ |
3005 | public SuperFieldAccess newSuperFieldAccess() { |
3006 | SuperFieldAccess result = new SuperFieldAccess(this); |
3007 | return result; |
3008 | } |
3009 | |
3010 | /** |
3011 | * Creates an unparented "super" method invocation expression node owned by |
3012 | * this AST. By default, the name of the method is unspecified (but legal), |
3013 | * there is no qualifier, no type arguments, and the list of arguments is empty. |
3014 | * |
3015 | * @return a new unparented "super" method invocation |
3016 | * expression node |
3017 | */ |
3018 | public SuperMethodInvocation newSuperMethodInvocation() { |
3019 | SuperMethodInvocation result = new SuperMethodInvocation(this); |
3020 | return result; |
3021 | } |
3022 | |
3023 | /** |
3024 | * Creates and returns a new unparented super method reference node owned by |
3025 | * this AST. By default, the name of the method is unspecified (but legal), |
3026 | * and there is no qualifier and no type arguments. |
3027 | * |
3028 | * @return a new unparented super method reference node |
3029 | * @since 3.10 |
3030 | */ |
3031 | public SuperMethodReference newSuperMethodReference() { |
3032 | SuperMethodReference result = new SuperMethodReference(this); |
3033 | return result; |
3034 | } |
3035 | |
3036 | /** |
3037 | * Creates and returns a new unparented switch expression node |
3038 | * owned by this AST. By default, the expression is unspecified, but legal, |
3039 | * and there are no statements or switch cases. |
3040 | * |
3041 | * @return a new unparented labeled switch expression node |
3042 | * @since 3.18 |
3043 | */ |
3044 | public SwitchExpression newSwitchExpression() { |
3045 | SwitchExpression result = new SwitchExpression(this); |
3046 | return result; |
3047 | } |
3048 | |
3049 | /** |
3050 | * Creates a new unparented switch case statement node owned by |
3051 | * this AST. By default, the node has no expression, but legal, and |
3052 | * switchLabeledRule is false which indicates ":". |
3053 | * |
3054 | * @return a new unparented switch case node |
3055 | */ |
3056 | public SwitchCase newSwitchCase() { |
3057 | return new SwitchCase(this); |
3058 | } |
3059 | |
3060 | /** |
3061 | * Creates a new unparented switch statement node owned by this AST. |
3062 | * By default, the expression is unspecified, but legal, and there are |
3063 | * no statements or switch cases. |
3064 | * |
3065 | * @return a new unparented labeled statement node |
3066 | */ |
3067 | public SwitchStatement newSwitchStatement() { |
3068 | return new SwitchStatement(this); |
3069 | } |
3070 | |
3071 | /** |
3072 | * Creates a new unparented synchronized statement node owned by this AST. |
3073 | * By default, the expression is unspecified, but legal, and the body is |
3074 | * an empty block. |
3075 | * |
3076 | * @return a new unparented synchronized statement node |
3077 | */ |
3078 | public SynchronizedStatement newSynchronizedStatement() { |
3079 | return new SynchronizedStatement(this); |
3080 | } |
3081 | |
3082 | /** |
3083 | * Creates and returns a new tag element node. |
3084 | * Initially the new node has no tag name and an empty list of fragments and |
3085 | * properties. |
3086 | * <p> |
3087 | * Note that this node type is used only inside doc comments |
3088 | * ({@link Javadoc}). |
3089 | * </p> |
3090 | * |
3091 | * @return a new unparented tag element node |
3092 | * @since 3.0 |
3093 | */ |
3094 | public TagElement newTagElement() { |
3095 | TagElement result = new TagElement(this); |
3096 | return result; |
3097 | } |
3098 | |
3099 | /** |
3100 | * Creates and returns a new tag property node. |
3101 | * Initially the new node has no property name and value. |
3102 | * <p> |
3103 | * Note that this node type is used only inside doc comments |
3104 | * ({@link Javadoc}). |
3105 | * </p> |
3106 | * |
3107 | * @return a new unparented tag element node |
3108 | * @since 3.30 |
3109 | */ |
3110 | public TagProperty newTagProperty() { |
3111 | TagProperty result = new TagProperty(this); |
3112 | return result; |
3113 | } |
3114 | |
3115 | /** |
3116 | * Creates an unparented yield statement node owned by this AST. The yield |
3117 | * statement has no |
3118 | * label/identifier/expression and is not implicit. |
3119 | * |
3120 | * @return a new unparented yield statement node |
3121 | * @since 3.24 |
3122 | */ |
3123 | public TextBlock newTextBlock() { |
3124 | return new TextBlock(this); |
3125 | } |
3126 | |
3127 | /** |
3128 | * Creates and returns a new text element node. |
3129 | * Initially the new node has an empty text string. |
3130 | * <p> |
3131 | * Note that this node type is used only inside doc comments |
3132 | * ({@link Javadoc Javadoc}). |
3133 | * </p> |
3134 | * |
3135 | * @return a new unparented text element node |
3136 | * @since 3.0 |
3137 | */ |
3138 | public TextElement newTextElement() { |
3139 | TextElement result = new TextElement(this); |
3140 | return result; |
3141 | } |
3142 | |
3143 | /** |
3144 | * Creates and returns a new unparented "this" expression node |
3145 | * owned by this AST. By default, there is no qualifier. |
3146 | * |
3147 | * @return a new unparented "this" expression node |
3148 | */ |
3149 | public ThisExpression newThisExpression() { |
3150 | ThisExpression result = new ThisExpression(this); |
3151 | return result; |
3152 | } |
3153 | |
3154 | /** |
3155 | * Creates a new unparented throw statement node owned by this AST. |
3156 | * By default, the expression is unspecified, but legal. |
3157 | * |
3158 | * @return a new unparented throw statement node |
3159 | */ |
3160 | public ThrowStatement newThrowStatement() { |
3161 | return new ThrowStatement(this); |
3162 | } |
3163 | |
3164 | /** |
3165 | * Creates a new unparented try statement node owned by this AST. |
3166 | * By default, the try statement has no resources, an empty block, no catch |
3167 | * clauses, and no finally block. |
3168 | * |
3169 | * @return a new unparented try statement node |
3170 | */ |
3171 | public TryStatement newTryStatement() { |
3172 | return new TryStatement(this); |
3173 | } |
3174 | |
3175 | /** |
3176 | * Creates an unparented class declaration node owned by this AST. |
3177 | * The name of the class is an unspecified, but legal, name; |
3178 | * no modifiers; no doc comment; no superclass or superinterfaces; |
3179 | * and an empty class body. |
3180 | * <p> |
3181 | * To create an interface, use this method and then call |
3182 | * <code>TypeDeclaration.setInterface(true)</code>. |
3183 | * </p> |
3184 | * |
3185 | * @return a new unparented type declaration node |
3186 | */ |
3187 | public TypeDeclaration newTypeDeclaration() { |
3188 | TypeDeclaration result = new TypeDeclaration(this); |
3189 | result.setInterface(false); |
3190 | return result; |
3191 | } |
3192 | |
3193 | /** |
3194 | * Creates a new unparented local type declaration statement node |
3195 | * owned by this AST, for the given type declaration. |
3196 | * <p> |
3197 | * This method can be used to convert any kind of type declaration |
3198 | * (<code>AbstractTypeDeclaration</code>) into a statement |
3199 | * (<code>Statement</code>) by wrapping it. |
3200 | * </p> |
3201 | * |
3202 | * @param decl the type declaration |
3203 | * @return a new unparented local type declaration statement node |
3204 | * @exception IllegalArgumentException if: |
3205 | * <ul> |
3206 | * <li>the node belongs to a different |
3207 | * AST</li> |
3208 | * <li>the node already has a parent</li> |
3209 | * </ul> |
3210 | * @since 3.0 |
3211 | */ |
3212 | public TypeDeclarationStatement newTypeDeclarationStatement(AbstractTypeDeclaration decl) { |
3213 | TypeDeclarationStatement result = new TypeDeclarationStatement(this); |
3214 | if (this.apiLevel == AST.JLS2) { |
3215 | result.internalSetTypeDeclaration((TypeDeclaration) decl); |
3216 | } |
3217 | if (this.apiLevel >= AST.JLS3) { |
3218 | result.setDeclaration(decl); |
3219 | } |
3220 | return result; |
3221 | } |
3222 | |
3223 | /** |
3224 | * Creates a new unparented local type declaration statement node |
3225 | * owned by this AST, for the given type declaration. |
3226 | * <p> |
3227 | * This method can be used to convert a type declaration |
3228 | * (<code>TypeDeclaration</code>) into a statement |
3229 | * (<code>Statement</code>) by wrapping it. |
3230 | * </p> |
3231 | * |
3232 | * @param decl the type declaration |
3233 | * @return a new unparented local type declaration statement node |
3234 | * @exception IllegalArgumentException if: |
3235 | * <ul> |
3236 | * <li>the node belongs to a different |
3237 | * AST</li> |
3238 | * <li>the node already has a parent</li> |
3239 | * </ul> |
3240 | */ |
3241 | public TypeDeclarationStatement newTypeDeclarationStatement(TypeDeclaration decl) { |
3242 | TypeDeclarationStatement result = new TypeDeclarationStatement(this); |
3243 | result.setDeclaration(decl); |
3244 | return result; |
3245 | } |
3246 | |
3247 | /** |
3248 | * Creates and returns a new unparented type literal expression node |
3249 | * owned by this AST. By default, the type is unspecified (but legal). |
3250 | * |
3251 | * @return a new unparented type literal node |
3252 | */ |
3253 | public TypeLiteral newTypeLiteral() { |
3254 | TypeLiteral result = new TypeLiteral(this); |
3255 | return result; |
3256 | } |
3257 | |
3258 | /** |
3259 | * Creates an unparented type method reference node owned by this AST. |
3260 | * By default, the type and method name are unspecified (but legal), |
3261 | * and there are no type arguments. |
3262 | * |
3263 | * @return a new unparented type method reference node |
3264 | * @exception UnsupportedOperationException if this operation is used in a JLS2, |
3265 | * JLS3 or JLS4 AST |
3266 | * @since 3.10 |
3267 | */ |
3268 | public TypeMethodReference newTypeMethodReference() { |
3269 | TypeMethodReference result = new TypeMethodReference(this); |
3270 | return result; |
3271 | } |
3272 | |
3273 | /** |
3274 | * Creates and returns a new unparented type parameter type node with an |
3275 | * unspecified type variable name and an empty list of type bounds. |
3276 | * |
3277 | * @return a new unparented type parameter node |
3278 | * @exception UnsupportedOperationException if this operation is used in |
3279 | * a JLS2 AST |
3280 | * @since 3.1 |
3281 | */ |
3282 | public TypeParameter newTypeParameter() { |
3283 | TypeParameter result = new TypeParameter(this); |
3284 | return result; |
3285 | } |
3286 | |
3287 | /** |
3288 | * Creates and returns a new unparented type pattern node with an |
3289 | * unspecified pattern variable. |
3290 | * |
3291 | * @return a new unparented type pattern node |
3292 | * @since 3.28 |
3293 | */ |
3294 | public TypePattern newTypePattern() { |
3295 | TypePattern result = new TypePattern(this); |
3296 | return result; |
3297 | } |
3298 | |
3299 | /** |
3300 | * Creates a new unparented union type node owned by this AST. |
3301 | * By default, the union type has no types. |
3302 | * |
3303 | * @return a new unparented UnionType node |
3304 | * @exception UnsupportedOperationException if this operation is used in |
3305 | * a JLS2 or JLS3 AST |
3306 | * @since 3.7.1 |
3307 | */ |
3308 | public UnionType newUnionType() { |
3309 | return new UnionType(this); |
3310 | } |
3311 | |
3312 | /** |
3313 | * Creates and returns a new unparented uses directive |
3314 | * node for an unspecified, but legal, name; |
3315 | * |
3316 | * @return a new unparented uses directive node |
3317 | * @exception UnsupportedOperationException if this operation is used in level |
3318 | * less than JLS9 |
3319 | * @since 3.14 |
3320 | */ |
3321 | public UsesDirective newUsesDirective() { |
3322 | UsesDirective result = new UsesDirective(this); |
3323 | return result; |
3324 | } |
3325 | |
3326 | /** |
3327 | * Creates a new unparented intersection type node owned by this AST. |
3328 | * By default, the intersection type has no types. |
3329 | * |
3330 | * @return a new unparented IntersectionType node |
3331 | * @exception UnsupportedOperationException if this operation is used in |
3332 | * a JLS2, JLS3 or JLS4 AST |
3333 | * @since 3.10 |
3334 | */ |
3335 | public IntersectionType newIntersectionType() { |
3336 | return new IntersectionType(this); |
3337 | } |
3338 | |
3339 | /** |
3340 | * Creates a new unparented local variable declaration expression node |
3341 | * owned by this AST, for the given variable declaration fragment. By |
3342 | * default, there are no modifiers and the base type is unspecified |
3343 | * (but legal). |
3344 | * <p> |
3345 | * This method can be used to convert a variable declaration fragment |
3346 | * (<code>VariableDeclarationFragment</code>) into an expression |
3347 | * (<code>Expression</code>) by wrapping it. Additional variable |
3348 | * declaration fragments can be added afterwards. |
3349 | * </p> |
3350 | * |
3351 | * @param fragment the first variable declaration fragment |
3352 | * @return a new unparented variable declaration expression node |
3353 | * @exception IllegalArgumentException if: |
3354 | * <ul> |
3355 | * <li>the node belongs to a different |
3356 | * AST</li> |
3357 | * <li>the node already has a parent</li> |
3358 | * <li>the given fragment is null</li> |
3359 | * </ul> |
3360 | */ |
3361 | public VariableDeclarationExpression newVariableDeclarationExpression(VariableDeclarationFragment fragment) { |
3362 | if (fragment == null) { |
3363 | throw new IllegalArgumentException(); |
3364 | } |
3365 | VariableDeclarationExpression result = new VariableDeclarationExpression(this); |
3366 | result.fragments().add(fragment); |
3367 | return result; |
3368 | } |
3369 | |
3370 | /** |
3371 | * Creates an unparented variable declaration fragment node owned by this |
3372 | * AST. By default, the fragment is for a variable with an unspecified, but |
3373 | * legal, name; no extra array dimensions; and no initializer. |
3374 | * |
3375 | * @return a new unparented variable declaration fragment node |
3376 | */ |
3377 | public VariableDeclarationFragment newVariableDeclarationFragment() { |
3378 | VariableDeclarationFragment result = new VariableDeclarationFragment(this); |
3379 | return result; |
3380 | } |
3381 | |
3382 | // =============================== STATEMENTS =========================== |
3383 | /** |
3384 | * Creates a new unparented local variable declaration statement node |
3385 | * owned by this AST, for the given variable declaration fragment. |
3386 | * By default, there are no modifiers and the base type is unspecified |
3387 | * (but legal). |
3388 | * <p> |
3389 | * This method can be used to convert a variable declaration fragment |
3390 | * (<code>VariableDeclarationFragment</code>) into a statement |
3391 | * (<code>Statement</code>) by wrapping it. Additional variable |
3392 | * declaration fragments can be added afterwards. |
3393 | * </p> |
3394 | * |
3395 | * @param fragment the variable declaration fragment |
3396 | * @return a new unparented variable declaration statement node |
3397 | * @exception IllegalArgumentException if: |
3398 | * <ul> |
3399 | * <li>the node belongs to a different |
3400 | * AST</li> |
3401 | * <li>the node already has a parent</li> |
3402 | * <li>the variable declaration fragment is |
3403 | * null</li> |
3404 | * </ul> |
3405 | */ |
3406 | public VariableDeclarationStatement newVariableDeclarationStatement(VariableDeclarationFragment fragment) { |
3407 | if (fragment == null) { |
3408 | throw new IllegalArgumentException(); |
3409 | } |
3410 | VariableDeclarationStatement result = new VariableDeclarationStatement(this); |
3411 | result.fragments().add(fragment); |
3412 | return result; |
3413 | } |
3414 | |
3415 | /** |
3416 | * Creates a new unparented while statement node owned by this AST. |
3417 | * By default, the expression is unspecified (but legal), and |
3418 | * the body statement is an empty block. |
3419 | * |
3420 | * @return a new unparented while statement node |
3421 | */ |
3422 | public WhileStatement newWhileStatement() { |
3423 | return new WhileStatement(this); |
3424 | } |
3425 | |
3426 | /** |
3427 | * Creates and returns a new unparented wildcard type node with no |
3428 | * type bound. |
3429 | * |
3430 | * @return a new unparented wildcard type node |
3431 | * @exception UnsupportedOperationException if this operation is used in |
3432 | * a JLS2 AST |
3433 | * @since 3.1 |
3434 | */ |
3435 | public WildcardType newWildcardType() { |
3436 | WildcardType result = new WildcardType(this); |
3437 | return result; |
3438 | } |
3439 | |
3440 | /** |
3441 | * Creates an unparented yield statement node owned by this AST. The yield |
3442 | * statement has no |
3443 | * label/identifier/expression and is not implicit. |
3444 | * |
3445 | * @return a new unparented yield statement node |
3446 | * @since 3.24 |
3447 | */ |
3448 | public YieldStatement newYieldStatement() { |
3449 | return new YieldStatement(this); |
3450 | } |
3451 | |
3452 | /** |
3453 | * Reports that the given node has just gained a child. |
3454 | * |
3455 | * @param node the node that was modified |
3456 | * @param child the node that was added as a child |
3457 | * @param property the child or child list property descriptor |
3458 | * @since 3.0 |
3459 | */ |
3460 | void postAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { |
3461 | // IMPORTANT: this method is called by readers during lazy init |
3462 | synchronized (this.internalASTLock) { |
3463 | // guard against concurrent access by a reader doing lazy init |
3464 | if (this.disableEvents > 0) { |
3465 | // doing lazy init OR already processing an event |
3466 | // System.out.println("[BOUNCE ADD]"); |
3467 | return; |
3468 | } else { |
3469 | disableEvents(); |
3470 | } |
3471 | } |
3472 | try { |
3473 | this.eventHandler.postAddChildEvent(node, child, property); |
3474 | // N.B. even if event handler blows up, the AST is not |
3475 | // corrupted since node has already been changed |
3476 | } finally { |
3477 | reenableEvents(); |
3478 | } |
3479 | } |
3480 | |
3481 | /** |
3482 | * Reports that the given node has just been cloned. |
3483 | * |
3484 | * @param node the node that was cloned |
3485 | * @param clone the clone of <code>node</code> |
3486 | * @since 3.0 |
3487 | */ |
3488 | void postCloneNodeEvent(ASTNode node, ASTNode clone) { |
3489 | synchronized (this.internalASTLock) { |
3490 | // guard against concurrent access by a reader doing lazy init |
3491 | if (this.disableEvents > 0) { |
3492 | // doing lazy init OR already processing an event |
3493 | // System.out.println("[BOUNCE CLONE]"); |
3494 | return; |
3495 | } else { |
3496 | disableEvents(); |
3497 | } |
3498 | } |
3499 | try { |
3500 | this.eventHandler.postCloneNodeEvent(node, clone); |
3501 | // N.B. even if event handler blows up, the AST is not |
3502 | // corrupted since node has already been changed |
3503 | } finally { |
3504 | reenableEvents(); |
3505 | } |
3506 | } |
3507 | |
3508 | /** |
3509 | * Reports that the given node jsut lost a child. |
3510 | * |
3511 | * @param node the node that was modified |
3512 | * @param child the child node that was removed |
3513 | * @param property the child or child list property descriptor |
3514 | * @since 3.0 |
3515 | */ |
3516 | void postRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { |
3517 | // IMPORTANT: this method is called by readers during lazy init |
3518 | synchronized (this.internalASTLock) { |
3519 | // guard against concurrent access by a reader doing lazy init |
3520 | if (this.disableEvents > 0) { |
3521 | // doing lazy init OR already processing an event |
3522 | // System.out.println("[BOUNCE DEL]"); |
3523 | return; |
3524 | } else { |
3525 | disableEvents(); |
3526 | } |
3527 | } |
3528 | try { |
3529 | this.eventHandler.postRemoveChildEvent(node, child, property); |
3530 | // N.B. even if event handler blows up, the AST is not |
3531 | // corrupted since node has not been changed yet |
3532 | } finally { |
3533 | reenableEvents(); |
3534 | } |
3535 | } |
3536 | |
3537 | /** |
3538 | * Reports that the given node has just had a child replaced. |
3539 | * |
3540 | * @param node the node modified |
3541 | * @param child the child removed |
3542 | * @param newChild the replacement child |
3543 | * @param property the child or child list property descriptor |
3544 | * @since 3.0 |
3545 | */ |
3546 | void postReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) { |
3547 | // IMPORTANT: this method is called by readers during lazy init |
3548 | synchronized (this.internalASTLock) { |
3549 | // guard against concurrent access by a reader doing lazy init |
3550 | if (this.disableEvents > 0) { |
3551 | // doing lazy init OR already processing an event |
3552 | // System.out.println("[BOUNCE REP]"); |
3553 | return; |
3554 | } else { |
3555 | disableEvents(); |
3556 | } |
3557 | } |
3558 | try { |
3559 | this.eventHandler.postReplaceChildEvent(node, child, newChild, property); |
3560 | // N.B. even if event handler blows up, the AST is not |
3561 | // corrupted since node has not been changed yet |
3562 | } finally { |
3563 | reenableEvents(); |
3564 | } |
3565 | } |
3566 | |
3567 | /** |
3568 | * Reports that the given node has just changed the value of a |
3569 | * non-child property. |
3570 | * |
3571 | * @param node the node that was modified |
3572 | * @param property the property descriptor |
3573 | * @since 3.0 |
3574 | */ |
3575 | void postValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { |
3576 | // IMPORTANT: this method is called by readers during lazy init |
3577 | synchronized (this.internalASTLock) { |
3578 | // guard against concurrent access by a reader doing lazy init |
3579 | if (this.disableEvents > 0) { |
3580 | // doing lazy init OR already processing an event |
3581 | // System.out.println("[BOUNCE CHANGE]"); |
3582 | return; |
3583 | } else { |
3584 | disableEvents(); |
3585 | } |
3586 | } |
3587 | try { |
3588 | this.eventHandler.postValueChangeEvent(node, property); |
3589 | // N.B. even if event handler blows up, the AST is not |
3590 | // corrupted since node has already been changed |
3591 | } finally { |
3592 | reenableEvents(); |
3593 | } |
3594 | } |
3595 | |
3596 | /** |
3597 | * Reports that the given node is about to gain a child. |
3598 | * |
3599 | * @param node the node that to be modified |
3600 | * @param child the node that to be added as a child |
3601 | * @param property the child or child list property descriptor |
3602 | * @since 3.0 |
3603 | */ |
3604 | void preAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { |
3605 | // IMPORTANT: this method is called by readers during lazy init |
3606 | synchronized (this.internalASTLock) { |
3607 | // guard against concurrent access by a reader doing lazy init |
3608 | if (this.disableEvents > 0) { |
3609 | // doing lazy init OR already processing an event |
3610 | // System.out.println("[BOUNCE ADD]"); |
3611 | return; |
3612 | } else { |
3613 | disableEvents(); |
3614 | } |
3615 | } |
3616 | try { |
3617 | this.eventHandler.preAddChildEvent(node, child, property); |
3618 | // N.B. even if event handler blows up, the AST is not |
3619 | // corrupted since node has already been changed |
3620 | } finally { |
3621 | reenableEvents(); |
3622 | } |
3623 | } |
3624 | |
3625 | /** |
3626 | * Reports that the given node is about to be cloned. |
3627 | * |
3628 | * @param node the node to be cloned |
3629 | * @since 3.0 |
3630 | */ |
3631 | void preCloneNodeEvent(ASTNode node) { |
3632 | synchronized (this.internalASTLock) { |
3633 | // guard against concurrent access by a reader doing lazy init |
3634 | if (this.disableEvents > 0) { |
3635 | // doing lazy init OR already processing an event |
3636 | // System.out.println("[BOUNCE CLONE]"); |
3637 | return; |
3638 | } else { |
3639 | disableEvents(); |
3640 | } |
3641 | } |
3642 | try { |
3643 | this.eventHandler.preCloneNodeEvent(node); |
3644 | // N.B. even if event handler blows up, the AST is not |
3645 | // corrupted since node has already been changed |
3646 | } finally { |
3647 | reenableEvents(); |
3648 | } |
3649 | } |
3650 | |
3651 | /** |
3652 | * Reports that the given node is about to lose a child. |
3653 | * |
3654 | * @param node the node about to be modified |
3655 | * @param child the node about to be removed |
3656 | * @param property the child or child list property descriptor |
3657 | * @since 3.0 |
3658 | */ |
3659 | void preRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { |
3660 | // IMPORTANT: this method is called by readers during lazy init |
3661 | synchronized (this.internalASTLock) { |
3662 | // guard against concurrent access by a reader doing lazy init |
3663 | if (this.disableEvents > 0) { |
3664 | // doing lazy init OR already processing an event |
3665 | // System.out.println("[BOUNCE DEL]"); |
3666 | return; |
3667 | } else { |
3668 | disableEvents(); |
3669 | } |
3670 | } |
3671 | try { |
3672 | this.eventHandler.preRemoveChildEvent(node, child, property); |
3673 | // N.B. even if event handler blows up, the AST is not |
3674 | // corrupted since node has not been changed yet |
3675 | } finally { |
3676 | reenableEvents(); |
3677 | } |
3678 | } |
3679 | |
3680 | /** |
3681 | * Reports that the given node is about have a child replaced. |
3682 | * |
3683 | * @param node the node about to be modified |
3684 | * @param child the child node about to be removed |
3685 | * @param newChild the replacement child |
3686 | * @param property the child or child list property descriptor |
3687 | * @since 3.0 |
3688 | */ |
3689 | void preReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) { |
3690 | // IMPORTANT: this method is called by readers during lazy init |
3691 | synchronized (this.internalASTLock) { |
3692 | // guard against concurrent access by a reader doing lazy init |
3693 | if (this.disableEvents > 0) { |
3694 | // doing lazy init OR already processing an event |
3695 | // System.out.println("[BOUNCE REP]"); |
3696 | return; |
3697 | } else { |
3698 | disableEvents(); |
3699 | } |
3700 | } |
3701 | try { |
3702 | this.eventHandler.preReplaceChildEvent(node, child, newChild, property); |
3703 | // N.B. even if event handler blows up, the AST is not |
3704 | // corrupted since node has not been changed yet |
3705 | } finally { |
3706 | reenableEvents(); |
3707 | } |
3708 | } |
3709 | |
3710 | /** |
3711 | * Reports that the given node is about to change the value of a |
3712 | * non-child property. |
3713 | * |
3714 | * @param node the node to be modified |
3715 | * @param property the property descriptor |
3716 | * @since 3.0 |
3717 | */ |
3718 | void preValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { |
3719 | // IMPORTANT: this method is called by readers during lazy init |
3720 | synchronized (this.internalASTLock) { |
3721 | // guard against concurrent access by a reader doing lazy init |
3722 | if (this.disableEvents > 0) { |
3723 | // doing lazy init OR already processing an event |
3724 | // System.out.println("[BOUNCE CHANGE]"); |
3725 | return; |
3726 | } else { |
3727 | disableEvents(); |
3728 | } |
3729 | } |
3730 | try { |
3731 | this.eventHandler.preValueChangeEvent(node, property); |
3732 | // N.B. even if event handler blows up, the AST is not |
3733 | // corrupted since node has already been changed |
3734 | } finally { |
3735 | reenableEvents(); |
3736 | } |
3737 | } |
3738 | |
3739 | /** |
3740 | * Enables the recording of changes to the given compilation |
3741 | * unit and its descendents. The compilation unit must have |
3742 | * been created by <code>ASTParser</code> and still be in |
3743 | * its original state. Once recording is on, |
3744 | * arbitrary changes to the subtree rooted at the compilation |
3745 | * unit are recorded internally. Once the modification has |
3746 | * been completed, call <code>rewrite</code> to get an object |
3747 | * representing the corresponding edits to the original |
3748 | * source code string. |
3749 | * |
3750 | * @exception IllegalArgumentException if this compilation unit is |
3751 | * marked as unmodifiable, or if this |
3752 | * compilation unit has already |
3753 | * been tampered with, or if recording has |
3754 | * already been enabled, |
3755 | * or if <code>root</code> is not owned by |
3756 | * this AST |
3757 | * @see CompilationUnit#recordModifications() |
3758 | * @since 3.0 |
3759 | */ |
3760 | void recordModifications(CompilationUnit root) { |
3761 | if (this.modificationCount != this.originalModificationCount) { |
3762 | throw new IllegalArgumentException("AST is already modified"); //$NON-NLS-1$ |
3763 | } else if (this.rewriter != null) { |
3764 | throw new IllegalArgumentException("AST modifications are already recorded"); //$NON-NLS-1$ |
3765 | } else if ((root.getFlags() & ASTNode.PROTECT) != 0) { |
3766 | throw new IllegalArgumentException("Root node is unmodifiable"); //$NON-NLS-1$ |
3767 | } else if (root.getAST() != this) { |
3768 | throw new IllegalArgumentException("Root node is not owned by this ast"); //$NON-NLS-1$ |
3769 | } |
3770 | |
3771 | this.rewriter = new InternalASTRewrite(root); |
3772 | setEventHandler(this.rewriter); |
3773 | } |
3774 | |
3775 | // =============================== ANNOTATIONS ==================== |
3776 | |
3777 | /** |
3778 | * Reenable events. |
3779 | * This method is thread-safe for AST readers. |
3780 | * |
3781 | * @see #disableEvents() |
3782 | * @since 3.0 |
3783 | */ |
3784 | final void reenableEvents() { |
3785 | synchronized (this.internalASTLock) { |
3786 | // guard against concurrent access by another reader |
3787 | this.disableEvents--; |
3788 | } |
3789 | } |
3790 | |
3791 | /** |
3792 | * Returns the type binding for a "well known" type. |
3793 | * <p> |
3794 | * Note that bindings are generally unavailable unless requested when the |
3795 | * AST is being built. |
3796 | * </p> |
3797 | * <p> |
3798 | * The following type names are supported: |
3799 | * <ul> |
3800 | * <li><code>"boolean"</code></li> |
3801 | * <li><code>"byte"</code></li> |
3802 | * <li><code>"char"</code></li> |
3803 | * <li><code>"double"</code></li> |
3804 | * <li><code>"float"</code></li> |
3805 | * <li><code>"int"</code></li> |
3806 | * <li><code>"long"</code></li> |
3807 | * <li><code>"short"</code></li> |
3808 | * <li><code>"void"</code></li> |
3809 | * <li><code>"java.lang.AssertionError"</code> (since 3.7)</li> |
3810 | * <li><code>"java.lang.Boolean"</code> (since 3.1)</li> |
3811 | * <li><code>"java.lang.Byte"</code> (since 3.1)</li> |
3812 | * <li><code>"java.lang.Character"</code> (since 3.1)</li> |
3813 | * <li><code>"java.lang.Class"</code></li> |
3814 | * <li><code>"java.lang.Cloneable"</code></li> |
3815 | * <li><code>"java.lang.Double"</code> (since 3.1)</li> |
3816 | * <li><code>"java.lang.Error"</code></li> |
3817 | * <li><code>"java.lang.Exception"</code></li> |
3818 | * <li><code>"java.lang.Float"</code> (since 3.1)</li> |
3819 | * <li><code>"java.lang.Integer"</code> (since 3.1)</li> |
3820 | * <li><code>"java.lang.Long"</code> (since 3.1)</li> |
3821 | * <li><code>"java.lang.Object"</code></li> |
3822 | * <li><code>"java.lang.RuntimeException"</code></li> |
3823 | * <li><code>"java.lang.Short"</code> (since 3.1)</li> |
3824 | * <li><code>"java.lang.String"</code></li> |
3825 | * <li><code>"java.lang.StringBuffer"</code></li> |
3826 | * <li><code>"java.lang.Throwable"</code></li> |
3827 | * <li><code>"java.lang.Void"</code> (since 3.1)</li> |
3828 | * <li><code>"java.io.Serializable"</code></li> |
3829 | * </ul> |
3830 | * |
3831 | * @param name the name of a well known type |
3832 | * @return the corresponding type binding, or <code>null</code> if the |
3833 | * named type is not considered well known or if no binding can be found |
3834 | * for it |
3835 | */ |
3836 | public ITypeBinding resolveWellKnownType(String name) { |
3837 | if (name == null) { |
3838 | return null; |
3839 | } |
3840 | return getBindingResolver().resolveWellKnownType(name); |
3841 | } |
3842 | |
3843 | /** |
3844 | * Sets the binding resolver for this AST. |
3845 | * |
3846 | * @param resolver the new binding resolver for this AST |
3847 | */ |
3848 | void setBindingResolver(BindingResolver resolver) { |
3849 | if (resolver == null) { |
3850 | throw new IllegalArgumentException(); |
3851 | } |
3852 | this.resolver = resolver; |
3853 | } |
3854 | |
3855 | /** |
3856 | * Sets default node flags of new nodes of this AST. |
3857 | * |
3858 | * @param flag node flags of new nodes of this AST |
3859 | * @since 3.0 |
3860 | */ |
3861 | void setDefaultNodeFlag(int flag) { |
3862 | this.defaultNodeFlag = flag; |
3863 | } |
3864 | |
3865 | /** |
3866 | * Sets the event handler for this AST. |
3867 | * |
3868 | * @param eventHandler the event handler for this AST |
3869 | * @since 3.0 |
3870 | */ |
3871 | void setEventHandler(NodeEventHandler eventHandler) { |
3872 | if (this.eventHandler == null) { |
3873 | throw new IllegalArgumentException(); |
3874 | } |
3875 | this.eventHandler = eventHandler; |
3876 | } |
3877 | |
3878 | void setFlag(int newValue) { |
3879 | this.bits |= newValue; |
3880 | } |
3881 | |
3882 | /** |
3883 | * Set <code>originalModificationCount</code> to the current modification count |
3884 | * |
3885 | * @since 3.0 |
3886 | */ |
3887 | void setOriginalModificationCount(long count) { |
3888 | this.originalModificationCount = count; |
3889 | } |
3890 | |
3891 | /** |
3892 | * Checks that this AST operation is only used when |
3893 | * building level JLS2 ASTs. |
3894 | * |
3895 | * @exception UnsupportedOperationException |
3896 | * @since 3.0 |
3897 | */ |
3898 | void supportedOnlyIn2() { |
3899 | if (this.apiLevel != AST.JLS2) { |
3900 | throw new UnsupportedOperationException("Operation not supported in JLS2 AST"); //$NON-NLS-1$ |
3901 | } |
3902 | } |
3903 | |
3904 | /** |
3905 | * Checks that this AST operation is not used when |
3906 | * building level JLS2 ASTs. |
3907 | * |
3908 | * @exception UnsupportedOperationException |
3909 | * @since 3.0 |
3910 | */ |
3911 | void unsupportedIn2() { |
3912 | if (this.apiLevel == AST.JLS2) { |
3913 | throw new UnsupportedOperationException("Operation not supported in JLS2 AST"); //$NON-NLS-1$ |
3914 | } |
3915 | } |
3916 | |
3917 | /** |
3918 | * |
3919 | * @return If previewEnabled flag is set to <code>true</code>, return |
3920 | * <code>true</code> else <code>false</code> |
3921 | * @since 3.21 |
3922 | * @noreference This method is not intended to be referenced by clients. |
3923 | */ |
3924 | public boolean isPreviewEnabledSet() { |
3925 | return this.previewEnabled; |
3926 | } |
3927 | |
3928 | /** |
3929 | * |
3930 | * @return If preview is enabled and apiLevel is latest, return |
3931 | * <code>true</code> else <code>false</code> |
3932 | * @since 3.19 |
3933 | */ |
3934 | public boolean isPreviewEnabled() { |
3935 | if (this.apiLevel == AST.JLS_INTERNAL_Latest && this.previewEnabled) { |
3936 | return true; |
3937 | } |
3938 | return false; |
3939 | } |
3940 | |
3941 | /** |
3942 | * Returns latest supported JLS level |
3943 | * |
3944 | * @return the latest supported JLS level |
3945 | * @since 3.27 |
3946 | */ |
3947 | public static int getJLSLatest() { |
3948 | return JLS_INTERNAL_Latest; |
3949 | } |
3950 | } |
3951 |
Members