oak-java 0.0.11

High-performance incremental Java language parser for the oak ecosystem with flexible configuration, supporting modern Java features and object-oriented programming.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#![doc = include_str!("readme.md")]

use core::range::Range;

/// Root node of a Java program
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JavaRoot {
    /// Items in the compilation unit
    pub items: Vec<Item>,
}

/// Annotation/Attribute
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Annotation {
    /// Annotation name
    pub name: String,
    /// Arguments (if any)
    pub arguments: Vec<Expression>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Top-level items in a Java program
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Item {
    /// Class declaration
    Class(ClassDeclaration),
    /// Interface declaration
    Interface(InterfaceDeclaration),
    /// Struct declaration
    Struct(StructDeclaration),
    /// Enum declaration
    Enum(EnumDeclaration),
    /// Record declaration
    Record(RecordDeclaration),
    /// Annotation type declaration
    AnnotationType(AnnotationTypeDeclaration),
    /// Package declaration
    Package(PackageDeclaration),
    /// Import declaration
    Import(ImportDeclaration),
}

/// Class declaration
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ClassDeclaration {
    /// Class name
    pub name: String,
    /// Type parameters
    pub type_parameters: Vec<TypeParameter>,
    /// Modifiers
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Superclass
    pub extends: Option<String>,
    /// Implemented interfaces
    pub implements: Vec<String>,
    /// Members
    pub members: Vec<Member>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Class members
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Member {
    /// Method declaration
    Method(MethodDeclaration),
    /// Field declaration
    Field(FieldDeclaration),
    /// Constructor declaration
    Constructor(ConstructorDeclaration),
    /// Inner class declaration
    InnerClass(ClassDeclaration),
}

/// Constructor declaration
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ConstructorDeclaration {
    /// Modifiers
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Name (should match class name)
    pub name: String,
    /// Parameter list
    pub parameters: Vec<Parameter>,
    /// Method body
    pub body: Vec<Statement>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Method declaration
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MethodDeclaration {
    /// Method name
    pub name: String,
    /// Type parameters
    pub type_parameters: Vec<TypeParameter>,
    /// Modifiers
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Return type
    pub return_type: String,
    /// Parameter list
    pub parameters: Vec<Parameter>,
    /// Method body
    pub body: Vec<Statement>,
    /// Thrown exceptions
    pub throws: Vec<String>,
    /// Whether it is static
    pub is_static: bool,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Parameter
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Parameter {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub r#type: String,
}

/// Type parameter for generics
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeParameter {
    /// Type parameter name
    pub name: String,
    /// Type parameter constraints
    pub constraints: Vec<TypeParameterConstraint>,
}

/// Type parameter constraint
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeParameterConstraint {
    /// Constraint type (class or interface)
    pub constraint_type: String,
}

/// Generic type reference
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GenericType {
    /// Base type name
    pub base_type: String,
    /// Type arguments
    pub type_arguments: Vec<String>,
}

/// Field declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FieldDeclaration {
    /// Field name.
    pub name: String,
    /// Field type.
    pub r#type: String,
    /// Field modifiers (e.g., public, static).
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// The span of the field declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Statement.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Statement {
    /// Expression statement.
    Expression(Expression),
    /// Return statement.
    Return(Option<Expression>),
    /// Block statement.
    Block(Vec<Statement>),
    /// Try statement.
    Try(TryStatement),
    /// Throw statement.
    Throw(Expression),
    /// If statement.
    If {
        /// Condition expression.
        condition: Expression,
        /// Then branch statement.
        then_branch: Box<Statement>,
        /// Optional else branch statement.
        else_branch: Option<Box<Statement>>,
    },
    /// While loop.
    While {
        /// Condition expression.
        condition: Expression,
        /// Loop body.
        body: Box<Statement>,
    },
    /// Do-while loop.
    DoWhile {
        /// Condition expression.
        condition: Expression,
        /// Loop body.
        body: Box<Statement>,
    },
    /// For loop.
    For {
        /// Initializer statement.
        init: Option<Box<Statement>>,
        /// Condition expression.
        condition: Option<Expression>,
        /// Update expression.
        update: Option<Expression>,
        /// Loop body.
        body: Box<Statement>,
    },
    /// For-each loop.
    ForEach {
        /// Item type.
        item_type: String,
        /// Item name.
        item_name: String,
        /// Iterable expression.
        iterable: Expression,
        /// Loop body.
        body: Box<Statement>,
    },
    /// Switch statement.
    Switch {
        /// Selector expression.
        selector: Expression,
        /// Switch cases.
        cases: Vec<SwitchCase>,
        /// Optional default case statements.
        default: Option<Vec<Statement>>,
    },
    /// Break statement.
    Break,
    /// Continue statement.
    Continue,
    /// Local variable declaration statement.
    LocalVariable {
        /// Variable type.
        r#type: String,
        /// Variable name.
        name: String,
        /// Optional initializer expression.
        initializer: Option<Expression>,
    },
}

/// Switch case.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwitchCase {
    /// Case label expression.
    pub label: Expression,
    /// Case body statements.
    pub body: Vec<Statement>,
}

/// Try statement.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TryStatement {
    /// Try block.
    pub block: Vec<Statement>,
    /// Catch clauses.
    pub catches: Vec<CatchClause>,
    /// Optional finally block.
    pub finally: Option<Vec<Statement>>,
}

/// Catch clause.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CatchClause {
    /// Catch parameter.
    pub parameter: Parameter,
    /// Catch block.
    pub block: Vec<Statement>,
}

/// Expression.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Expression {
    /// Literal expression.
    Literal(Literal),
    /// Identifier expression (e.g., variable reference).
    Identifier(String),
    /// Method call expression.
    MethodCall(MethodCall),
    /// Field access expression.
    FieldAccess(FieldAccess),
    /// Array access expression.
    ArrayAccess(ArrayAccess),
    /// Array creation expression.
    ArrayCreation(ArrayCreation),
    /// New expression (object creation).
    New(NewExpression),
    /// Anonymous class creation expression.
    AnonymousClass(AnonymousClassExpression),
    /// This expression.
    This,
    /// Super expression.
    Super,
    /// Binary operation.
    Binary {
        /// Left operand.
        left: Box<Expression>,
        /// Operator.
        op: String,
        /// Right operand.
        right: Box<Expression>,
    },
    /// Unary operation.
    Unary {
        /// Operator.
        op: String,
        /// Operand expression.
        expression: Box<Expression>,
    },
    /// Assignment operation.
    Assignment {
        /// Left operand.
        left: Box<Expression>,
        /// Operator.
        op: String,
        /// Right operand.
        right: Box<Expression>,
    },
    /// Update operation (increment/decrement).
    Update {
        /// Operand expression.
        expression: Box<Expression>,
        /// Operator.
        op: String,
        /// Whether the operator is a prefix.
        is_prefix: bool,
    },
    /// Ternary operation.
    Ternary {
        /// Condition expression.
        condition: Box<Expression>,
        /// Then branch expression.
        then_branch: Box<Expression>,
        /// Else branch expression.
        else_branch: Box<Expression>,
    },
    /// Cast operation.
    Cast {
        /// Target type.
        target_type: String,
        /// Operand expression.
        expression: Box<Expression>,
    },
    /// Lambda expression
    Lambda(LambdaExpression),
}

/// Lambda body (either an expression or a block)
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LambdaBody {
    /// Expression body
    Expression(Box<Expression>),
    /// Block body
    Block(Vec<Statement>),
}

/// Lambda expression
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LambdaExpression {
    /// Parameters
    pub parameters: Vec<Parameter>,
    /// Body (either an expression or a block)
    pub body: LambdaBody,
}

/// New expression.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NewExpression {
    /// Object type.
    pub r#type: String,
    /// Constructor arguments.
    pub arguments: Vec<Expression>,
}

/// Anonymous class creation expression.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnonymousClassExpression {
    /// Supertype (class or interface).
    pub super_type: String,
    /// Constructor arguments.
    pub arguments: Vec<Expression>,
    /// Class body members.
    pub members: Vec<Member>,
}

/// Literal.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Literal {
    /// Integer literal.
    Integer(i64),
    /// Float literal.
    Float(f64),
    /// String literal.
    String(String),
    /// Boolean literal.
    Boolean(bool),
}

/// Field access.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FieldAccess {
    /// Target expression.
    pub target: Box<Expression>,
    /// Field name.
    pub name: String,
}

/// Method call.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MethodCall {
    /// Call target (optional, e.g., System.out).
    pub target: Option<Box<Expression>>,
    /// Method name.
    pub name: String,
    /// Arguments.
    pub arguments: Vec<Expression>,
}

/// Array access.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ArrayAccess {
    /// Target array expression.
    pub target: Box<Expression>,
    /// Index expression.
    pub index: Box<Expression>,
}

/// Array creation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ArrayCreation {
    /// Element type.
    pub element_type: String,
    /// Array dimensions.
    pub dimensions: Vec<Expression>,
}

/// Interface declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InterfaceDeclaration {
    /// Interface name.
    pub name: String,
    /// Type parameters
    pub type_parameters: Vec<TypeParameter>,
    /// Modifiers.
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Extended interfaces.
    pub extends: Vec<String>,
    /// Interface members.
    pub members: Vec<Member>,
    /// The span of the interface declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Struct declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StructDeclaration {
    /// Struct name.
    pub name: String,
    /// Modifiers.
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Implemented interfaces.
    pub implements: Vec<String>,
    /// Struct members.
    pub members: Vec<Member>,
    /// The span of the struct declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Enum declaration
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnumDeclaration {
    /// Enum name
    pub name: String,
    /// Modifiers
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Implemented interfaces
    pub implements: Vec<String>,
    /// Enum constants
    pub constants: Vec<EnumConstant>,
    /// Members
    pub members: Vec<Member>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Enum constant
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnumConstant {
    /// Constant name
    pub name: String,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Constructor arguments (optional)
    pub arguments: Vec<Expression>,
    /// Class body (optional)
    pub body: Option<Vec<Member>>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Record declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RecordDeclaration {
    /// Record name.
    pub name: String,
    /// Modifiers.
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Record parameters (e.g., primary constructor parameters).
    pub parameters: Vec<Parameter>,
    /// Implemented interfaces.
    pub implements: Vec<String>,
    /// Record members.
    pub members: Vec<Member>,
    /// The span of the record declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Annotation type declaration
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnnotationTypeDeclaration {
    /// Annotation name
    pub name: String,
    /// Modifiers
    pub modifiers: Vec<String>,
    /// Annotations
    pub annotations: Vec<Annotation>,
    /// Annotation elements
    pub elements: Vec<AnnotationElement>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Annotation element
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnnotationElement {
    /// Element name
    pub name: String,
    /// Element type
    pub r#type: String,
    /// Default value (optional)
    pub default_value: Option<Expression>,
    /// Source span
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Package declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PackageDeclaration {
    /// Package name.
    pub name: String,
    /// The span of the package declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}

/// Import declaration.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ImportDeclaration {
    /// Import path.
    pub path: String,
    /// Whether it's a static import.
    pub is_static: bool,
    /// The span of the import declaration in the source file.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: Range<usize>,
}