Thursday 22 July 2021

Javapoet: CodeBlock: Positional Arguments

Place an integer index (1-based) before the placeholder in the format string to specify which argument to use.

 

Example

Builder codeBlock1 = CodeBlock.builder().add("Hi, my name is '$2L' and I am '$1L' years old", 32, "Krishna");

Above snippet generates below string.

"Hi, my name is 'Krishna' and I am '32' years old"

 

Find the below working application.

 

CodeBlockPositionalArgumentsDemo.java

package com.sample.app;

import java.io.IOException;

import javax.lang.model.element.Modifier;

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

public class CodeBlockPositionalArgumentsDemo {

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

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

        Builder codeBlock1 = CodeBlock.builder().add("Hi, my name is '$2L' and I am '$1L' years old", 32, "Krishna");

        methodBuilder.addStatement("$T.out.println($S)", System.class, codeBlock1.build().toString());
        MethodSpec mainMethod = methodBuilder.build();

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

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

        javaFile.writeTo(System.out);
    }

}


Generated code

package com.sample.app;

import java.lang.String;
import java.lang.System;

public final class Test {
  public static void main(String[] args) {
    System.out.println("Hi, my name is 'Krishna' and I am '32' years old");
  }
}


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment