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:

  1. I create an annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface CodeGenSubject {
}
  1. 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)
        }
    }
}
  1. I write my domain specific class
@CodeGenSubject
@Entity
public class MyDomainSpecificEntity {
    @Id
    private Long id;
    private String stuff;

    // getters and setters
}
  1. 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.

1 Answers

0
Community On

I actually have played with the concept and have an open source gradle plugin on github (gradle-boilerplate-generator-plugin) that generates boilerplates.

It is a generic plugin to generate any type of boilerplates (the user can define their own using a simple YAML descripto, FreeMarker based templates and gradle closure configurations rather than java classes with annotations that you prefer). This plugin comes by default with a few demo boilerplates.

And, actually, one of these demo boilerplates includes (out-of-the-box) a task named boilerplateWebServiceResource to generate the typical Entity, DTO, Repo, Controller, Resource, etc. classes required for a typical web service resource.

Its documentation is very detailed on how to use it, configure it and extend it (create custom boilerplates). It may not be exactly what you are looking for; but you can send me suggestions, questions, fork it and modify it, or just maybe get ideas from it. Hope this helps.