0
# AST Node Hierarchy
1
2
Complete set of wrapper classes representing all Scala AST node types, providing PMD-compatible interfaces for Scala language constructs. These nodes wrap Scalameta AST nodes and integrate with PMD's visitor pattern for analysis and rule development.
3
4
## Capabilities
5
6
### Base Node Interface
7
8
Core interface implemented by all Scala AST nodes, providing visitor pattern support and tree navigation.
9
10
```java { .api }
11
/**
12
* A Base interface of a Scala Node. Defines several required methods of all nodes.
13
* @param <T> The Scala node type that extends Scala's Tree trait
14
*/
15
public interface ScalaNode<T extends Tree> extends Node {
16
/**
17
* Accept a visitor and traverse this node
18
* @param <D> The type of the data input
19
* @param <R> The type of the returned data
20
* @param visitor the visitor to visit this node with
21
* @param data context-specific data to pass along
22
* @return context-specific data for this Visitor pattern
23
*/
24
<D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
25
26
/**
27
* Get the underlying Scala Node
28
* @return the Scala Node for this node
29
* @deprecated The underlying scala node should not be used directly
30
*/
31
@Deprecated
32
T getNode();
33
34
/**
35
* Returns true if the node is implicit. If this node has no non-implicit
36
* descendant, then its text bounds identify an empty region of the source
37
* document.
38
*/
39
boolean isImplicit();
40
41
@Override
42
ScalaNode<?> getChild(int idx);
43
44
@Override
45
ScalaNode<?> getParent();
46
47
@Override
48
Iterable<? extends ScalaNode<?>> children();
49
}
50
```
51
52
### Root AST Node
53
54
The root node of every parsed Scala source file, implementing PMD's RootNode interface.
55
56
```java { .api }
57
/**
58
* The ASTSource node implementation - root of all Scala ASTs
59
*/
60
public class ASTSource extends AbstractScalaNode<Source> implements RootNode {
61
/**
62
* Create ASTSource from Scalameta Source node
63
* @param scalaNode the underlying Scalameta Source
64
* @deprecated Internal API
65
*/
66
@Deprecated
67
@InternalApi
68
public ASTSource(Source scalaNode);
69
70
/**
71
* Accept visitor for AST traversal
72
* @param <D> visitor data type
73
* @param <R> visitor return type
74
* @param visitor the visitor instance
75
* @param data context data
76
* @return visitor result
77
*/
78
@Override
79
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
80
}
81
```
82
83
### Declaration Nodes
84
85
AST nodes representing Scala declarations (abstract definitions without implementations).
86
87
```java { .api }
88
/**
89
* Declaration nodes for abstract definitions
90
*/
91
92
// Method declarations
93
public class ASTDeclDef extends AbstractScalaNode<Decl.Def> {
94
public ASTDeclDef(Decl.Def scalaNode);
95
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
96
}
97
98
// Type declarations
99
public class ASTDeclType extends AbstractScalaNode<Decl.Type> {
100
public ASTDeclType(Decl.Type scalaNode);
101
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
102
}
103
104
// Value declarations
105
public class ASTDeclVal extends AbstractScalaNode<Decl.Val> {
106
public ASTDeclVal(Decl.Val scalaNode);
107
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
108
}
109
110
// Variable declarations
111
public class ASTDeclVar extends AbstractScalaNode<Decl.Var> {
112
public ASTDeclVar(Decl.Var scalaNode);
113
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
114
}
115
```
116
117
### Definition Nodes
118
119
AST nodes representing Scala definitions (concrete implementations).
120
121
```java { .api }
122
/**
123
* Definition nodes for concrete implementations
124
*/
125
126
// Class definitions
127
public class ASTDefnClass extends AbstractScalaNode<Defn.Class> {
128
public ASTDefnClass(Defn.Class scalaNode);
129
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
130
}
131
132
// Method definitions
133
public class ASTDefnDef extends AbstractScalaNode<Defn.Def> {
134
public ASTDefnDef(Defn.Def scalaNode);
135
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
136
}
137
138
// Macro definitions
139
public class ASTDefnMacro extends AbstractScalaNode<Defn.Macro> {
140
public ASTDefnMacro(Defn.Macro scalaNode);
141
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
142
}
143
144
// Object definitions
145
public class ASTDefnObject extends AbstractScalaNode<Defn.Object> {
146
public ASTDefnObject(Defn.Object scalaNode);
147
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
148
}
149
150
// Trait definitions
151
public class ASTDefnTrait extends AbstractScalaNode<Defn.Trait> {
152
public ASTDefnTrait(Defn.Trait scalaNode);
153
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
154
}
155
156
// Type definitions
157
public class ASTDefnType extends AbstractScalaNode<Defn.Type> {
158
public ASTDefnType(Defn.Type scalaNode);
159
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
160
}
161
162
// Value definitions
163
public class ASTDefnVal extends AbstractScalaNode<Defn.Val> {
164
public ASTDefnVal(Defn.Val scalaNode);
165
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
166
}
167
168
// Variable definitions
169
public class ASTDefnVar extends AbstractScalaNode<Defn.Var> {
170
public ASTDefnVar(Defn.Var scalaNode);
171
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
172
}
173
```
174
175
### Term Expression Nodes
176
177
AST nodes representing Scala expressions and terms.
178
179
```java { .api }
180
/**
181
* Key term expression nodes
182
*/
183
184
// Function application: f(x, y)
185
public class ASTTermApply extends AbstractScalaNode<Term.Apply> {
186
public ASTTermApply(Term.Apply scalaNode);
187
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
188
}
189
190
// Infix application: x + y
191
public class ASTTermApplyInfix extends AbstractScalaNode<Term.ApplyInfix> {
192
public ASTTermApplyInfix(Term.ApplyInfix scalaNode);
193
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
194
}
195
196
// Type application: List[Int]
197
public class ASTTermApplyType extends AbstractScalaNode<Term.ApplyType> {
198
public ASTTermApplyType(Term.ApplyType scalaNode);
199
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
200
}
201
202
// Assignment: x = value
203
public class ASTTermAssign extends AbstractScalaNode<Term.Assign> {
204
public ASTTermAssign(Term.Assign scalaNode);
205
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
206
}
207
208
// Code blocks: { statements }
209
public class ASTTermBlock extends AbstractScalaNode<Term.Block> {
210
public ASTTermBlock(Term.Block scalaNode);
211
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
212
}
213
214
// For comprehensions: for (x <- list) yield x * 2
215
public class ASTTermFor extends AbstractScalaNode<Term.For> {
216
public ASTTermFor(Term.For scalaNode);
217
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
218
}
219
220
// For-yield expressions
221
public class ASTTermForYield extends AbstractScalaNode<Term.ForYield> {
222
public ASTTermForYield(Term.ForYield scalaNode);
223
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
224
}
225
226
// Function literals: (x: Int) => x * 2
227
public class ASTTermFunction extends AbstractScalaNode<Term.Function> {
228
public ASTTermFunction(Term.Function scalaNode);
229
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
230
}
231
232
// If expressions: if (cond) then else
233
public class ASTTermIf extends AbstractScalaNode<Term.If> {
234
public ASTTermIf(Term.If scalaNode);
235
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
236
}
237
238
// Pattern matching: expr match { case ... }
239
public class ASTTermMatch extends AbstractScalaNode<Term.Match> {
240
public ASTTermMatch(Term.Match scalaNode);
241
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
242
}
243
244
// Term names/identifiers
245
public class ASTTermName extends AbstractScalaNode<Term.Name> {
246
public ASTTermName(Term.Name scalaNode);
247
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
248
}
249
250
// Object instantiation: new Class()
251
public class ASTTermNew extends AbstractScalaNode<Term.New> {
252
public ASTTermNew(Term.New scalaNode);
253
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
254
}
255
256
// Member selection: obj.member
257
public class ASTTermSelect extends AbstractScalaNode<Term.Select> {
258
public ASTTermSelect(Term.Select scalaNode);
259
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
260
}
261
262
// This references
263
public class ASTTermThis extends AbstractScalaNode<Term.This> {
264
public ASTTermThis(Term.This scalaNode);
265
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
266
}
267
268
// Try-catch expressions
269
public class ASTTermTry extends AbstractScalaNode<Term.Try> {
270
public ASTTermTry(Term.Try scalaNode);
271
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
272
}
273
274
// While loops
275
public class ASTTermWhile extends AbstractScalaNode<Term.While> {
276
public ASTTermWhile(Term.While scalaNode);
277
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
278
}
279
```
280
281
### Type System Nodes
282
283
AST nodes representing Scala type expressions and type system constructs.
284
285
```java { .api }
286
/**
287
* Type system nodes
288
*/
289
290
// Type application: List[String]
291
public class ASTTypeApply extends AbstractScalaNode<Type.Apply> {
292
public ASTTypeApply(Type.Apply scalaNode);
293
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
294
}
295
296
// Function types: (Int, String) => Boolean
297
public class ASTTypeFunction extends AbstractScalaNode<Type.Function> {
298
public ASTTypeFunction(Type.Function scalaNode);
299
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
300
}
301
302
// Type names
303
public class ASTTypeName extends AbstractScalaNode<Type.Name> {
304
public ASTTypeName(Type.Name scalaNode);
305
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
306
}
307
308
// Type parameters: [T, U]
309
public class ASTTypeParam extends AbstractScalaNode<Type.Param> {
310
public ASTTypeParam(Type.Param scalaNode);
311
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
312
}
313
314
// Tuple types: (Int, String, Boolean)
315
public class ASTTypeTuple extends AbstractScalaNode<Type.Tuple> {
316
public ASTTypeTuple(Type.Tuple scalaNode);
317
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
318
}
319
320
// Compound types with "with": TraitA with TraitB
321
public class ASTTypeWith extends AbstractScalaNode<Type.With> {
322
public ASTTypeWith(Type.With scalaNode);
323
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
324
}
325
```
326
327
### Pattern Matching Nodes
328
329
AST nodes representing Scala pattern matching constructs.
330
331
```java { .api }
332
/**
333
* Pattern matching nodes
334
*/
335
336
// Alternative patterns: pattern1 | pattern2
337
public class ASTPatAlternative extends AbstractScalaNode<Pat.Alternative> {
338
public ASTPatAlternative(Pat.Alternative scalaNode);
339
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
340
}
341
342
// Pattern binding: name @ pattern
343
public class ASTPatBind extends AbstractScalaNode<Pat.Bind> {
344
public ASTPatBind(Pat.Bind scalaNode);
345
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
346
}
347
348
// Extractor patterns: Extractor(args)
349
public class ASTPatExtract extends AbstractScalaNode<Pat.Extract> {
350
public ASTPatExtract(Pat.Extract scalaNode);
351
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
352
}
353
354
// Tuple patterns: (a, b, c)
355
public class ASTPatTuple extends AbstractScalaNode<Pat.Tuple> {
356
public ASTPatTuple(Pat.Tuple scalaNode);
357
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
358
}
359
360
// Variable patterns: variable names in patterns
361
public class ASTPatVar extends AbstractScalaNode<Pat.Var> {
362
public ASTPatVar(Pat.Var scalaNode);
363
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
364
}
365
366
// Wildcard patterns: _
367
public class ASTPatWildcard extends AbstractScalaNode<Pat.Wildcard> {
368
public ASTPatWildcard(Pat.Wildcard scalaNode);
369
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
370
}
371
```
372
373
### Literal Nodes
374
375
AST nodes representing Scala literal values.
376
377
```java { .api }
378
/**
379
* Literal value nodes
380
*/
381
382
public class ASTLitBoolean extends AbstractScalaNode<Lit.Boolean> {
383
public ASTLitBoolean(Lit.Boolean scalaNode);
384
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
385
}
386
387
public class ASTLitInt extends AbstractScalaNode<Lit.Int> {
388
public ASTLitInt(Lit.Int scalaNode);
389
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
390
}
391
392
public class ASTLitLong extends AbstractScalaNode<Lit.Long> {
393
public ASTLitLong(Lit.Long scalaNode);
394
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
395
}
396
397
public class ASTLitDouble extends AbstractScalaNode<Lit.Double> {
398
public ASTLitDouble(Lit.Double scalaNode);
399
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
400
}
401
402
public class ASTLitFloat extends AbstractScalaNode<Lit.Float> {
403
public ASTLitFloat(Lit.Float scalaNode);
404
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
405
}
406
407
public class ASTLitString extends AbstractScalaNode<Lit.String> {
408
public ASTLitString(Lit.String scalaNode);
409
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
410
}
411
412
public class ASTLitChar extends AbstractScalaNode<Lit.Char> {
413
public ASTLitChar(Lit.Char scalaNode);
414
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
415
}
416
417
public class ASTLitNull extends AbstractScalaNode<Lit.Null> {
418
public ASTLitNull(Lit.Null scalaNode);
419
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
420
}
421
422
public class ASTLitUnit extends AbstractScalaNode<Lit.Unit> {
423
public ASTLitUnit(Lit.Unit scalaNode);
424
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
425
}
426
```
427
428
### Modifier Nodes
429
430
AST nodes representing Scala modifiers and annotations.
431
432
```java { .api }
433
/**
434
* Modifier nodes
435
*/
436
437
public class ASTModAbstract extends AbstractScalaNode<Mod.Abstract> {
438
public ASTModAbstract(Mod.Abstract scalaNode);
439
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
440
}
441
442
public class ASTModCase extends AbstractScalaNode<Mod.Case> {
443
public ASTModCase(Mod.Case scalaNode);
444
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
445
}
446
447
public class ASTModFinal extends AbstractScalaNode<Mod.Final> {
448
public ASTModFinal(Mod.Final scalaNode);
449
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
450
}
451
452
public class ASTModImplicit extends AbstractScalaNode<Mod.Implicit> {
453
public ASTModImplicit(Mod.Implicit scalaNode);
454
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
455
}
456
457
public class ASTModLazy extends AbstractScalaNode<Mod.Lazy> {
458
public ASTModLazy(Mod.Lazy scalaNode);
459
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
460
}
461
462
public class ASTModOverride extends AbstractScalaNode<Mod.Override> {
463
public ASTModOverride(Mod.Override scalaNode);
464
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
465
}
466
467
public class ASTModPrivate extends AbstractScalaNode<Mod.Private> {
468
public ASTModPrivate(Mod.Private scalaNode);
469
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
470
}
471
472
public class ASTModProtected extends AbstractScalaNode<Mod.Protected> {
473
public ASTModProtected(Mod.Protected scalaNode);
474
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
475
}
476
477
public class ASTModSealed extends AbstractScalaNode<Mod.Sealed> {
478
public ASTModSealed(Mod.Sealed scalaNode);
479
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
480
}
481
```
482
483
### Import System Nodes
484
485
AST nodes representing Scala import statements and clauses.
486
487
```java { .api }
488
/**
489
* Import system nodes
490
*/
491
492
// Import statements: import scala.util.Random
493
public class ASTImport extends AbstractScalaNode<Import> {
494
public ASTImport(Import scalaNode);
495
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
496
}
497
498
// Import clauses
499
public class ASTImporter extends AbstractScalaNode<Importer> {
500
public ASTImporter(Importer scalaNode);
501
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
502
}
503
504
// Named imports: import pkg.{SpecificClass}
505
public class ASTImporteeName extends AbstractScalaNode<Importee.Name> {
506
public ASTImporteeName(Importee.Name scalaNode);
507
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
508
}
509
510
// Renamed imports: import pkg.{Original => Renamed}
511
public class ASTImporteeRename extends AbstractScalaNode<Importee.Rename> {
512
public ASTImporteeRename(Importee.Rename scalaNode);
513
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
514
}
515
516
// Wildcard imports: import pkg._
517
public class ASTImporteeWildcard extends AbstractScalaNode<Importee.Wildcard> {
518
public ASTImporteeWildcard(Importee.Wildcard scalaNode);
519
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
520
}
521
522
// Excluded imports: import pkg.{Excluded => _}
523
public class ASTImporteeUnimport extends AbstractScalaNode<Importee.Unimport> {
524
public ASTImporteeUnimport(Importee.Unimport scalaNode);
525
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
526
}
527
```
528
529
**Usage Examples:**
530
531
```java
532
// Navigate AST structure
533
ASTSource root = parser.parse("Example.scala", sourceReader);
534
535
// Check node types
536
if (root.getChild(0) instanceof ASTDefnObject) {
537
ASTDefnObject objectDef = (ASTDefnObject) root.getChild(0);
538
System.out.println("Found object definition");
539
540
// Access child nodes
541
for (ScalaNode<?> child : objectDef.children()) {
542
if (child instanceof ASTDefnDef) {
543
System.out.println("Found method definition");
544
}
545
}
546
}
547
548
// Check for implicit nodes (empty source regions)
549
if (node.isImplicit()) {
550
// This node represents generated or implicit code
551
System.out.println("Implicit node: " + node.getClass().getSimpleName());
552
}
553
```