/javapoet-util

Utility for working with javapoet library

Primary LanguageJavaMIT LicenseMIT

Utility for my personal style when using JavaPoet

checkstyle

using checkstyle settings from javapoet library, but ignored some rules

  • MethodName (uppercase method names for modifier builder)

Example code-producing changes with this utility and abse for requesting changes in original javapoet

Add field

intended declaration:

private String type;

normal JavaPoet

builder.addField(FieldSpec.builder( String.class, "type", Modifier.PRIVATE).build());

with this utility

addField(builder, PRIVATE(), String.class, "type");

Add field with initializer

intended declaration:

public static final String PRIMARY = null;

normal JavaPoet

builder.addField(
		FieldSpec.builder(String.class, "PRIMARY", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
		.initializer("return $L", varName).build() );

with this utility

addField(builder, PUBLIC().STATIC().FINAL(), String.class, "PRIMARY", "return $L", varName);

with this utility (different line break)
addField(builder, PUBLIC().STATIC().FINAL(), 
	String.class, "PRIMARY", "return $L", varName);

Add parameter to a method

intended declaration for one of the parameters:

...(..., boolean primitive)...

normal JavaPoet

method.addParameter(ParameterSpec.builder(boolean.class, "primitive").build());

with this utility

addParameter(method, boolean.class, "primitive");

Add field with getter and setter (bean style)

intended code

private boolean primitive;
public booelan isPrimitive(){ 
	return primitive; 
}
public booelan setPrimitive(boolean primitive){ 
	this.primitive = primitive; 
}

normal JavaPoet

builder.addField(FieldSpec.builder( boolean.class, "primitive", Modifier.PRIVATE).build());

builder.addMethod( MethodSpec.methodBuilder("isPrimitive")
	.addModifiers(Modifier.PUBLIC)
	.addCode("return primitive;").build());

builder.addMethod( MethodSpec.methodBuilder("setPrimitive")
		.addModifiers(Modifier.PUBLIC)
		.addParameter(boolean.class,"primitive")
		.addCode("this.primitive = primitive;\n").build());

with this utility

addBeanField(builder, boolean.class, "primitive");