I am creating springboot applications, and most of the time I find myself writing boilerplate code for my models - repositories, services, controllers, builders... I do not want to do that.
Based on my my experiences, previous works and researches I have a concept kind of developed in my mind. Basically the following:
- I create an annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface CodeGenSubject {
}
- I create a processor
public class MyProcessor extends AbstractProcessor {
    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return Collections.singleton(CodeGenSubject.class.getCanonicalName());
    }
    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        for(Element e: roundEnvironment.getElementsAnnotatedWith(CodeGenSubject.class)){
            // Observe fields and methods with reflection API
            // "Write" some code with JavaPoet
            // Place the generated code to the src/java folder
            // (with javax.annotation.processing.Filer)
        }
    }
}
- I write my domain specific class
@CodeGenSubject
@Entity
public class MyDomainSpecificEntity {
    @Id
    private Long id;
    private String stuff;
    // getters and setters
}
- And lasty, I create a gradle task (?)
task myCodeGeneratorTask(type: ???, group: "", desription: "") {
    // With this I am stuck
}
Ideally this template generator would be a separate module.
I have seen some example projects (mostly for android), and then I found the most promising:
https://www.baeldung.com/java-annotation-processing-builder
Would be perfect, but... it uses maven, and the code is placed in a totally impenetrable repository with a pom.xml file in the root project with a few thousand lines. Thanks : D
Right now I am working on an example multi-module gradle project with a springboot application. I have one entity (MyDomainSpecificEntity) and I am trying to make gradle generate some source code for me based on my annotation and processor.
Firstly, the biggest help would be some advice if I am conceptually wrong.
Secondly, if I am not, I would appreciate some help with that gradle script.
Lastly... the best would be a cleansed example project, If anyone ever played with this subject, and have some sort of public repo, that would be the most welcome.
Thanks.
 
Aucun commentaire:
Enregistrer un commentaire