Wednesday, 21 July 2021

Javapoet: Add control flow statements

'beginControlFlow' and 'endControlFlow' methods are used to generate control flow snippets.

 

Example

methodSpecBuilder.beginControlFlow("while(i < 10)");
methodSpecBuilder.beginControlFlow("while(j < i+1)");
methodSpecBuilder.addStatement("System.out.print(\"*\")");
methodSpecBuilder.addStatement("j++");
methodSpecBuilder.endControlFlow();
methodSpecBuilder.addStatement("System.out.println()");
methodSpecBuilder.addStatement("i++");
methodSpecBuilder.addStatement("j = 0");
methodSpecBuilder.addCode("\n");
methodSpecBuilder.endControlFlow();

 

Above statements generate below snippet.

while(i < 10) {
    while(j < i+1) {
      System.out.print("*");
      j++;
    }
    System.out.println();
    i++;
    j = 0;

  }

Find the below working application.

 

ControlFlowStatementsDemo.java

package com.sample.app;

import java.io.IOException;

import javax.lang.model.element.Modifier;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

public class ControlFlowStatementsDemo {

	public static void main(String args[]) throws IOException {

		MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder("main");
		methodSpecBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
		methodSpecBuilder.returns(void.class);
		methodSpecBuilder.addParameter(String[].class, "args");

		methodSpecBuilder.addCode("\n");
		methodSpecBuilder.addComment("Print stars triangle");
		methodSpecBuilder.addStatement("int i = 0, j = 0");
		methodSpecBuilder.beginControlFlow("while(i < 10)");
		methodSpecBuilder.beginControlFlow("while(j < i+1)");
		methodSpecBuilder.addStatement("System.out.print(\"*\")");
		methodSpecBuilder.addStatement("j++");
		methodSpecBuilder.endControlFlow();
		methodSpecBuilder.addStatement("System.out.println()");
		methodSpecBuilder.addStatement("i++");
		methodSpecBuilder.addStatement("j = 0");
		methodSpecBuilder.addCode("\n");
		methodSpecBuilder.endControlFlow();

		MethodSpec mainMethodSpec = methodSpecBuilder.build();

		String className = "Test";
		TypeSpec.Builder classSpecBuilder = TypeSpec.classBuilder(className);
		classSpecBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
		classSpecBuilder.addMethod(mainMethodSpec);
		TypeSpec helloWorldTypeSpec = classSpecBuilder.build();

		String packageName = "com.sample.app";
		JavaFile javaFile = JavaFile.builder(packageName, helloWorldTypeSpec).build();

		javaFile.writeTo(System.out);
	}

}


Generated output

package com.sample.app;

import java.lang.String;

public final class Test {
  public static void main(String[] args) {

    // Print stars triangle
    int i = 0, j = 0;
    while(i < 10) {
      while(j < i+1) {
        System.out.print("*");
        j++;
      }
      System.out.println();
      i++;
      j = 0;

    }
  }
}





 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment