The AST tree is designed to reflect the syntax rules of the programming language.
Syntactically, an Initializer consists of a block with an optional static
modifier before it. So it’s the most natural implementation having an Initializer
class consisting of a Block
and modifiers (inherited from BodyDeclaration
). So I don’t understand why you question it.
Example of initializers:
class Foo {
static {
System.out.println("static initializer");
System.out.println("class Foo now initialized");
}
{
System.out.println("instance initializer");
System.out.println("an instance of Foo has been created");
}
}
Expressions and statements are two different syntactic constructs. There are places where only either an expression or a statement is allowed. But there are constructs like method invocations which are Expression Statements which means the can fulfill both roles, Expression and Statement. They can be invoked stand-alone for their side-effects but also at places where a value is required.
But since Java does not allow multiple inheritance you cannot create an AST class ExpressionStatement
inheriting from both Expression
and Statement
. So you need a solution like in the Eclipse AST where ExpressionStatement
inherits from one and wraps the other. The decision which one to inherit and which one to wrap is easy: you can create a Statement
implementation (subclass) which drops the result of the Expression
it has wrapped but you cannot create an Expression
implementation which provides a result for a Statement
that provides no result through its interface. An alternative to such an implementation would be the use of interfaces.