0
# AST Parsing and Navigation
1
2
Complete Abstract Syntax Tree representation for Apex code with 95+ node types covering all language constructs from type declarations to expressions and statements. Provides parsing capabilities and visitor pattern for tree traversal and analysis.
3
4
## Capabilities
5
6
### Parser
7
8
Creates AST from Apex source code.
9
10
```java { .api }
11
/**
12
* Parses Apex source code into an Abstract Syntax Tree
13
* @param task - Parser task containing source code and configuration
14
* @returns Root ASTApexFile node representing the parsed file
15
*/
16
public class ApexParser {
17
public ASTApexFile parse(ParserTask task);
18
}
19
```
20
21
### Root Node
22
23
Represents the root of an Apex file AST.
24
25
```java { .api }
26
/**
27
* Root node representing an entire Apex file
28
*/
29
public class ASTApexFile implements ApexNode<ASTApexFile> {
30
/** Get the main class, interface, enum, or trigger in this file */
31
public ASTUserClassOrInterface<?> getMainNode();
32
/** Get multifile analysis issues for this file */
33
public List<Issue> getGlobalIssues();
34
/** Get AST metadata information */
35
public AstInfo getAstInfo();
36
}
37
```
38
39
### Core Interfaces
40
41
Base interfaces for all AST nodes and visitor pattern.
42
43
```java { .api }
44
/**
45
* Root interface for all Apex AST nodes
46
* @param <T> The concrete node type
47
*/
48
public interface ApexNode<T> {
49
/** Check if node has real source location */
50
boolean hasRealLoc();
51
/** Get the defining type for this node */
52
String getDefiningType();
53
/** Get the root ASTApexFile node */
54
ASTApexFile getRoot();
55
}
56
57
/**
58
* Visitor pattern interface for traversing Apex AST
59
* @param <P> Parameter type passed to visit methods
60
* @param <R> Return type from visit methods
61
*/
62
public interface ApexVisitor<P, R> {
63
// Root node
64
R visit(ASTApexFile node, P data);
65
66
// Type declarations
67
R visit(ASTUserClass node, P data);
68
R visit(ASTUserInterface node, P data);
69
R visit(ASTUserEnum node, P data);
70
R visit(ASTUserTrigger node, P data);
71
R visit(ASTAnonymousClass node, P data);
72
73
// Members
74
R visit(ASTMethod node, P data);
75
R visit(ASTField node, P data);
76
R visit(ASTProperty node, P data);
77
R visit(ASTParameter node, P data);
78
R visit(ASTUserClassMethods node, P data);
79
R visit(ASTUserExceptionMethods node, P data);
80
81
// Statements
82
R visit(ASTBlockStatement node, P data);
83
R visit(ASTIfBlockStatement node, P data);
84
R visit(ASTIfElseBlockStatement node, P data);
85
R visit(ASTWhileLoopStatement node, P data);
86
R visit(ASTDoLoopStatement node, P data);
87
R visit(ASTForLoopStatement node, P data);
88
R visit(ASTForEachStatement node, P data);
89
R visit(ASTBreakStatement node, P data);
90
R visit(ASTContinueStatement node, P data);
91
R visit(ASTReturnStatement node, P data);
92
R visit(ASTThrowStatement node, P data);
93
R visit(ASTTryCatchFinallyBlockStatement node, P data);
94
R visit(ASTCatchBlockStatement node, P data);
95
R visit(ASTRunAsBlockStatement node, P data);
96
R visit(ASTExpressionStatement node, P data);
97
R visit(ASTStatementExecuted node, P data);
98
R visit(ASTMultiStatement node, P data);
99
100
// Switch statements
101
R visit(ASTSwitchStatement node, P data);
102
R visit(ASTElseWhenBlock node, P data);
103
R visit(ASTTypeWhenBlock node, P data);
104
R visit(ASTValueWhenBlock node, P data);
105
R visit(ASTLiteralCase node, P data);
106
R visit(ASTIdentifierCase node, P data);
107
108
// DML statements
109
R visit(ASTDmlInsertStatement node, P data);
110
R visit(ASTDmlUpdateStatement node, P data);
111
R visit(ASTDmlDeleteStatement node, P data);
112
R visit(ASTDmlUpsertStatement node, P data);
113
R visit(ASTDmlMergeStatement node, P data);
114
R visit(ASTDmlUndeleteStatement node, P data);
115
116
// Expressions
117
R visit(ASTExpression node, P data);
118
R visit(ASTBinaryExpression node, P data);
119
R visit(ASTBooleanExpression node, P data);
120
R visit(ASTAssignmentExpression node, P data);
121
R visit(ASTPrefixExpression node, P data);
122
R visit(ASTPostfixExpression node, P data);
123
R visit(ASTTernaryExpression node, P data);
124
R visit(ASTCastExpression node, P data);
125
R visit(ASTInstanceOfExpression node, P data);
126
R visit(ASTNestedExpression node, P data);
127
128
// Variable and reference expressions
129
R visit(ASTVariableExpression node, P data);
130
R visit(ASTThisVariableExpression node, P data);
131
R visit(ASTSuperVariableExpression node, P data);
132
R visit(ASTTriggerVariableExpression node, P data);
133
R visit(ASTJavaVariableExpression node, P data);
134
R visit(ASTReferenceExpression node, P data);
135
R visit(ASTEmptyReferenceExpression node, P data);
136
R visit(ASTArrayLoadExpression node, P data);
137
R visit(ASTArrayStoreExpression node, P data);
138
R visit(ASTNestedStoreExpression node, P data);
139
R visit(ASTIllegalStoreExpression node, P data);
140
141
// Method call expressions
142
R visit(ASTMethodCallExpression node, P data);
143
R visit(ASTThisMethodCallExpression node, P data);
144
R visit(ASTSuperMethodCallExpression node, P data);
145
R visit(ASTJavaMethodCallExpression node, P data);
146
147
// Object creation
148
R visit(ASTNewObjectExpression node, P data);
149
R visit(ASTNewListInitExpression node, P data);
150
R visit(ASTNewListLiteralExpression node, P data);
151
R visit(ASTNewMapInitExpression node, P data);
152
R visit(ASTNewMapLiteralExpression node, P data);
153
R visit(ASTNewSetInitExpression node, P data);
154
R visit(ASTNewSetLiteralExpression node, P data);
155
R visit(ASTNewKeyValueObjectExpression node, P data);
156
157
// Literals and constants
158
R visit(ASTLiteralExpression node, P data);
159
R visit(ASTClassRefExpression node, P data);
160
R visit(ASTPackageVersionExpression node, P data);
161
162
// SOQL/SOSL
163
R visit(ASTSoqlExpression node, P data);
164
R visit(ASTSoslExpression node, P data);
165
R visit(ASTBindExpressions node, P data);
166
167
// Variable declarations
168
R visit(ASTVariableDeclaration node, P data);
169
R visit(ASTVariableDeclarationStatements node, P data);
170
R visit(ASTFieldDeclaration node, P data);
171
R visit(ASTFieldDeclarationStatements node, P data);
172
173
// Annotations and modifiers
174
R visit(ASTAnnotation node, P data);
175
R visit(ASTAnnotationParameter node, P data);
176
R visit(ASTModifier node, P data);
177
R visit(ASTModifierNode node, P data);
178
R visit(ASTModifierOrAnnotation node, P data);
179
180
// Method/constructor components
181
R visit(ASTMethodBlockStatement node, P data);
182
R visit(ASTConstructorPreamble node, P data);
183
R visit(ASTConstructorPreambleStatement node, P data);
184
185
// Comments
186
R visit(ASTFormalComment node, P data);
187
188
// Utility nodes
189
R visit(ASTMapEntryNode node, P data);
190
R visit(ASTStandardCondition node, P data);
191
R visit(ASTInvalidDependentCompilation node, P data);
192
}
193
194
/**
195
* Base implementation of ApexVisitor with default traversal behavior
196
*/
197
public abstract class ApexVisitorBase<P, R> implements ApexVisitor<P, R> {
198
// Default implementations that traverse child nodes
199
}
200
```
201
202
### Type Declaration Nodes
203
204
AST nodes representing class, interface, enum, and trigger declarations.
205
206
```java { .api }
207
/**
208
* Apex class declaration
209
*/
210
public class ASTUserClass extends BaseApexClass<ASTUserClass>
211
implements ApexQualifiableNode, AccessNode {
212
// Class-specific methods
213
}
214
215
/**
216
* Interface declaration
217
*/
218
public class ASTUserInterface extends BaseApexClass<ASTUserInterface>
219
implements ApexQualifiableNode, AccessNode {
220
// Interface-specific methods
221
}
222
223
/**
224
* Enum declaration
225
*/
226
public class ASTUserEnum extends BaseApexClass<ASTUserEnum>
227
implements ApexQualifiableNode, AccessNode {
228
// Enum-specific methods
229
}
230
231
/**
232
* Trigger declaration
233
*/
234
public class ASTUserTrigger extends BaseApexClass<ASTUserTrigger>
235
implements ApexQualifiableNode {
236
/** Get trigger usage patterns (before/after insert/update/delete/undelete) */
237
public Set<TriggerUsage> getUsages();
238
}
239
240
/**
241
* Anonymous class declaration
242
*/
243
public class ASTAnonymousClass extends BaseApexClass<ASTAnonymousClass> {
244
// Anonymous class-specific methods
245
}
246
```
247
248
### Member Declaration Nodes
249
250
AST nodes for class members like methods, fields, and properties.
251
252
```java { .api }
253
/**
254
* Method declaration
255
*/
256
public class ASTMethod extends AbstractApexNode<ASTMethod>
257
implements ApexQualifiableNode, AccessNode {
258
/** Get method name */
259
public String getName();
260
/** Get method parameters */
261
public List<ASTParameter> getParameters();
262
/** Check if method is constructor */
263
public boolean isConstructor();
264
}
265
266
/**
267
* Field declaration
268
*/
269
public class ASTField extends AbstractApexNode<ASTField>
270
implements ApexQualifiableNode, AccessNode {
271
/** Get field name */
272
public String getName();
273
/** Get field type */
274
public String getType();
275
}
276
277
/**
278
* Property declaration
279
*/
280
public class ASTProperty extends AbstractApexNode<ASTProperty>
281
implements ApexQualifiableNode, AccessNode {
282
/** Get property name */
283
public String getName();
284
/** Check if property has getter */
285
public boolean hasGetter();
286
/** Check if property has setter */
287
public boolean hasSetter();
288
}
289
290
/**
291
* Method parameter
292
*/
293
public class ASTParameter extends AbstractApexNode<ASTParameter> {
294
/** Get parameter name */
295
public String getName();
296
/** Get parameter type */
297
public String getType();
298
}
299
```
300
301
### Expression Nodes
302
303
AST nodes for various types of expressions.
304
305
```java { .api }
306
/**
307
* Binary operation expression (arithmetic, comparison, logical)
308
*/
309
public class ASTBinaryExpression extends AbstractApexNode<ASTBinaryExpression> {
310
/** Get the binary operator */
311
public BinaryOperator getOperator();
312
/** Get left operand */
313
public ASTExpression getLeftOperand();
314
/** Get right operand */
315
public ASTExpression getRightOperand();
316
}
317
318
/**
319
* Boolean operation expression (comparison, logical)
320
*/
321
public class ASTBooleanExpression extends AbstractApexNode<ASTBooleanExpression> {
322
/** Get the boolean operator */
323
public BooleanOperator getOperator();
324
/** Get left operand */
325
public ASTExpression getLeftOperand();
326
/** Get right operand */
327
public ASTExpression getRightOperand();
328
}
329
330
/**
331
* Assignment expression
332
*/
333
public class ASTAssignmentExpression extends AbstractApexNode<ASTAssignmentExpression> {
334
/** Get the assignment operator */
335
public AssignmentOperator getOperator();
336
/** Get left side (target) of assignment */
337
public ASTExpression getLeftOperand();
338
/** Get right side (value) of assignment */
339
public ASTExpression getRightOperand();
340
}
341
342
/**
343
* Method call expression
344
*/
345
public class ASTMethodCallExpression extends AbstractApexNode<ASTMethodCallExpression> {
346
/** Get method name */
347
public String getMethodName();
348
/** Get method arguments */
349
public List<ASTExpression> getArguments();
350
/** Get target object (null for static calls) */
351
public ASTExpression getTarget();
352
}
353
354
/**
355
* Variable reference expression
356
*/
357
public class ASTVariableExpression extends AbstractApexNode<ASTVariableExpression> {
358
/** Get variable name */
359
public String getVariableName();
360
}
361
362
/**
363
* SOQL query expression
364
*/
365
public class ASTSoqlExpression extends AbstractApexNode<ASTSoqlExpression> {
366
/** Get SOQL query string */
367
public String getQuery();
368
/** Get bind expressions in the query */
369
public List<ASTBindExpressions> getBindExpressions();
370
}
371
372
/**
373
* SOSL search expression
374
*/
375
public class ASTSoslExpression extends AbstractApexNode<ASTSoslExpression> {
376
/** Get SOSL search string */
377
public String getSearch();
378
/** Get bind expressions in the search */
379
public List<ASTBindExpressions> getBindExpressions();
380
}
381
```
382
383
### Utility Interfaces and Classes
384
385
```java { .api }
386
/**
387
* Interface for nodes that can have qualified names
388
*/
389
public interface ApexQualifiableNode {
390
/** Get qualified name for this node */
391
ApexQualifiedName getQualifiedName();
392
}
393
394
/**
395
* Interface for nodes with access modifiers
396
*/
397
public interface AccessNode {
398
// Access modifier related methods
399
}
400
401
/**
402
* Represents qualified names for classes and methods
403
*/
404
public class ApexQualifiedName {
405
/** Check if this represents a class */
406
public boolean isClass();
407
/** Check if this represents an operation/method */
408
public boolean isOperation();
409
/** Get the class portion of the qualified name */
410
public String getClassName();
411
/** Create qualified name from string representation */
412
public static ApexQualifiedName ofString(String qualifiedName);
413
}
414
415
/**
416
* Comment container interface
417
*/
418
public interface ASTCommentContainer {
419
// Comment-related methods
420
}
421
422
/**
423
* Utility for building comments
424
*/
425
public class ApexCommentBuilder {
426
// Comment building utilities
427
}
428
```
429
430
**Usage Examples:**
431
432
```java
433
// Parse Apex code
434
ApexParser parser = new ApexParser();
435
ASTApexFile apexFile = parser.parse(parserTask);
436
437
// Get main type declaration
438
ApexNode<?> mainNode = apexFile.getMainNode();
439
if (mainNode instanceof ASTUserClass) {
440
ASTUserClass classNode = (ASTUserClass) mainNode;
441
ApexQualifiedName qname = classNode.getQualifiedName();
442
System.out.println("Class: " + qname.getClassName());
443
}
444
445
// Custom visitor for analysis
446
public class ComplexityVisitor extends ApexVisitorBase<Object, Object> {
447
private int methodCount = 0;
448
449
@Override
450
public Object visit(ASTMethod node, Object data) {
451
methodCount++;
452
String methodName = node.getName();
453
System.out.println("Found method: " + methodName);
454
return super.visit(node, data);
455
}
456
457
@Override
458
public Object visit(ASTBinaryExpression node, Object data) {
459
BinaryOperator op = node.getOperator();
460
System.out.println("Binary operation: " + op);
461
return super.visit(node, data);
462
}
463
}
464
465
// Use visitor
466
ComplexityVisitor visitor = new ComplexityVisitor();
467
apexFile.jjtAccept(visitor, null);
468
469
// Navigate AST manually
470
if (apexFile.getMainNode() instanceof ASTUserClass) {
471
ASTUserClass classNode = (ASTUserClass) apexFile.getMainNode();
472
// Find all methods in the class
473
List<ASTMethod> methods = classNode.findDescendantsOfType(ASTMethod.class);
474
for (ASTMethod method : methods) {
475
System.out.println("Method: " + method.getName());
476
// Analyze method parameters
477
for (ASTParameter param : method.getParameters()) {
478
System.out.println(" Parameter: " + param.getName() + " : " + param.getType());
479
}
480
}
481
}
482
```