Thursday 22 July 2021

Javapoet: Define parameters using ParameterSpec

'ParameterSpec.builder()' is used to define parameter specifications.

 

Example

ParameterSpec empId = ParameterSpec.builder(int.class, "id")
			    .addModifiers(Modifier.FINAL)
			    .build();

 

Once you define the parameter specification, you can add this parameter to a method specification.

 

Find the below working application.

 

AddParametersDemo.java

package com.sample.app;

import java.io.IOException;

import javax.lang.model.element.Modifier;

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

public class AddParametersDemo {
	public static void main(String args[]) throws IOException {
		FieldSpec nameField = FieldSpec.builder(String.class, "name")
			    .addModifiers(Modifier.PRIVATE)
			    .build();
		
		FieldSpec idField = FieldSpec.builder(int.class, "id")
			    .addModifiers(Modifier.PRIVATE)
			    .build();
		
		ParameterSpec empId = ParameterSpec.builder(int.class, "id")
			    .addModifiers(Modifier.FINAL)
			    .build();
		
		ParameterSpec empName = ParameterSpec.builder(String.class, "name")
			    .addModifiers(Modifier.FINAL)
			    .build();

		MethodSpec constructor = MethodSpec.constructorBuilder()
					.addModifiers(Modifier.PUBLIC)
					.addParameter(empId)
					.addParameter(empName)
					.addStatement("this.$N = $N", "id", "id")
					.addStatement("this.$N = $N", "name", "name").build();

		String className = "Employee";
		TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
		classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
		classBuilder.addMethod(constructor);
		classBuilder.addField(nameField).addField(idField);
		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;

public final class Employee {
  private String name;

  private int id;

  public Employee(final int id, final String name, String name) {
    this.id = id;
    this.name = name;
  }
}

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment