Generate custom java code like lombok at compile time

88 Views Asked by At

I want create an annotation to append a new variable to annotated class at compile time like the below, it accepts variable name and variable type

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface AddNewVariable {
    String fieldName();
    Class<?> fieldType();
}

I tried to use AbstractProcessor to generate the code but I found that the limitation of the annotation processing API — it can only be used to generate new files, not to change existing ones.

I want change in the exist class like Lombok at compile time

** I develop Spring Application and I use hibernate ORM, I have a generic columns like (is_Active, is_deleted, created_by, create_on, etc.) but I don't want all entities inherit them from mapped super class, because in some cases, One class want only is_active and is_deleted or any combination of them. So I suggest to make it by annotation, if class want generic columns, it should add annotation with the variables name and types

**

Example

I want write the below

@AddNewVariable("isActive", Boolean.class)
Class User {
int id;
String fullName;
int age;
}

My expectation is the below int the target folder

Class User {
int id;
String fullName;
int age;
boolean isActive;
}
0

There are 0 best solutions below