Generate static information about Java Beans
It is an annotation processor for generating code containing static information about the fields of any Java class.
Create typesafe code to be constructed in a strongly-typed manner when you need to reference fields by literal value.
In Maven pom.xml
<dependency>
<groupId>io.github.mhagnumdw</groupId>
<artifactId>bean-info-generator</artifactId>
<version>0.0.1</version>
<scope>compile</scope>
</dependency>
Annotate a class with @GenerateBeanMetaInfo
package test;
import io.github.mhagnumdw.beaninfogenerator.GenerateBeanMetaInfo;
@GenerateBeanMetaInfo
public class People {
private int age;
private Date birthday;
private String name;
}
When the build run, People_INFO.java
and People_INFO.class
are generated:
package test;
import io.github.mhagnumdw.beaninfogenerator.BeanMetaInfo;
import javax.annotation.Generated;
@Generated(
value = "io.github.mhagnumdw.beaninfogenerator.BeanMetaInfoProcessor",
comments = "Only Name: false, Class metadata information of: test.People"
)
public abstract class People_INFO {
public static final BeanMetaInfo age = new BeanMetaInfo("age");
public static final BeanMetaInfo birthday = new BeanMetaInfo("birthday");
public static final BeanMetaInfo name = new BeanMetaInfo("name");
}
So, a code that is traditionally written like this:
Field field = People.class.getDeclaredField("age");
Now it can be written like this:
Field field = People.class.getDeclaredField(People_INFO.age.getName());
Someday, when the attribute age
is renamed it will break in the compilation or your IDE will warn of the problem, something that would not happen before.
- debug (default:
false
): iftrue
more information about annotation processing is written in the log; - suffix (default:
_INFO
): sets the suffix to name the generated code; - addGenerationDate (default:
false
): iftrue
is added to the source its generation date; - onlyName (default:
false
): iftrue
a simple String is used to represent a field.
The options can be seen in @SupportedOptions
here.
When onlyName=true
People_INFO
is generated as following:
package test;
import java.lang.String;
import javax.annotation.Generated;
@Generated(
value = "io.github.mhagnumdw.beaninfogenerator.BeanMetaInfoProcessor",
comments = "Only Name: true, Class metadata information of: test.People"
)
public abstract class People_INFO {
public static final String age = "age";
public static final String birthday = "birthday";
public static final String name = "name";
}
In Maven pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
<configuration>
<compilerArgs>
<compilerArg>-AaddGenerationDate=true</compilerArg>
<compilerArg>-Asuffix=_GENERATED</compilerArg>
</compilerArgs>
</configuration>
</plugin>
- Right click on the project > Properties
- Java Compiler > Annotation Processing > Factory path
- Add two jars:
- bean-info-generator-X.Y.Z.jar
- javapoet-X.Y.Z.jar
- It currently generates information only for fields;
- Internal classes are not created within the generated class.