Getting Started

Fluentast is a fluent API to create abstract syntax trees for Java. Getting started is simple.

Install Fluentast using either Maven or Gradle.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
testCompile group: 'junit', name: 'junit', version: '4.12'

You may then use Fluentast to create a syntax tree.

Examples

The example below demonstrates how to build a simple code snippet can be built using JDT Core or Fluentast.

int i=1;
while (true) {
  System.out.println(i);
  if (i > 99) break;
  i++;
}
AST ast = AST.newAST(AST.JLS8);
Block block = ast.newBlock();
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName("i"));
fragment.setInitializer(ast.newNumberLiteral("1"));
VariableDeclarationStatement statement = ast.newVariableDeclarationStatement(fragment);
statement.setType(ast.newPrimitiveType(PrimitiveType.INT));
block.statements().add(statement);
WhileStatement whileStatement = ast.newWhileStatement();
whileStatement.setExpression(ast.newBooleanLiteral(true));
Block whileBody = ast.newBlock();
MethodInvocation methodInvocation = ast.newMethodInvocation();
methodInvocation.setName(ast.newSimpleName("println"));
methodInvocation.setExpression(ast.newName("System.out"));
methodInvocation
    .arguments()
    .add(ast.newSimpleName("i"));
whileBody
    .statements()
    .add(ast.newExpressionStatement(methodInvocation));
IfStatement ifStatement = ast.newIfStatement();
InfixExpression infixExpression = ast.newInfixExpression();
infixExpression.setLeftOperand(ast.newSimpleName("i"));
infixExpression.setOperator(Operator.GREATER);
infixExpression.setRightOperand(ast.newNumberLiteral("99"));
ifStatement.setExpression(infixExpression);
ifStatement.setThenStatement(ast.newBreakStatement());
whileBody
    .statements()
    .add(ifStatement);
PostfixExpression postfixExpression = ast.newPostfixExpression();
postfixExpression.setOperator(PostfixExpression.Operator.INCREMENT);
postfixExpression.setOperand(ast.newSimpleName("i"));
whileBody
    .statements()
    .add(ast.newExpressionStatement(postfixExpression));
whileStatement.setBody(whileBody);
block.statements().add(whileStatement);
FluentStatement variableDeclaration = stmnt(decl("int",1));
FluentStatement methodInvocation = stmnt(invocation(name("System.out"),
                                                    new ArrayList<FluentType>(),
                                                    "println",
                                                    n("i")));
FluentBlock whileBody = block(methodInvocation,
                              if_(infix(">")
                                      .left(n("i"))
                                      .right(i(99))).then(break_()),
                              stmnt(postfix("++").operand(n("i"))));
block(variableDeclaration,
      while_(b(true))
          .do_(whileBody))
    .build();

You can find additional examples in the in the demo project on Github.

For more information regarding the Fluentast syntax have a look at the syntax page or at the API


Improve this page