Saturday, 24 July 2021

Javapoet: Define nested class

Step 1: define a class specification.

TypeSpec nestedClass = TypeSpec.classBuilder("NestedClass").addModifiers(Modifier.STATIC).build();

 

Step 2: Add this class specification to parent class.

TypeSpec parentClass = TypeSpec.classBuilder("Test").addType(nestedClass)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL).build();

Find the below working application.

 

NestedClassDemo.java

package com.sample.app;

import java.io.IOException;

import javax.lang.model.element.Modifier;

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

public class NestedClassDemo {

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

		TypeSpec nestedClass = TypeSpec.classBuilder("NestedClass").addModifiers(Modifier.STATIC).build();
		TypeSpec parentClass = TypeSpec.classBuilder("Test").addType(nestedClass)
				.addModifiers(Modifier.PUBLIC, Modifier.FINAL).build();

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

		javaFile.writeTo(System.out);
	}

}


Generated code

package com.sample.app;

public final class Test {
  static class NestedClass {
  }
}



 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment