generator-jhipster icon indicating copy to clipboard operation
generator-jhipster copied to clipboard

Use APT to generate code on compilation

Open gzsombor opened this issue 3 years ago • 9 comments

Overview of the feature request

As JHipster generates lots of boilerplate source code, which could be intimidating or annoying - especially for entities with long list of fields (for DTOs, Criteria objects, etc...), and with MapStruct or the Hibernate Static Metamodel generator it is a proven path to reduce the repetitiveness, I'm suggesting to use APT to generate part of our code on compilation instead of project setup/re-generation.

Motivation for or Use Case

DTOs and Criteria objects are really long and boring code, and most of the cases it's just waste - for example adding 5 fields to an entity, currently result in a lot of changes in the generated code, and reviewing these changes are annoying - easy to miss the important changes. The other problem which I regularly face, is that after a generated project matures a bit, using the current JHipster generators to add new fields, or regenerate the project is much bigger task that used to be. (Because the customization is everywhere, for example having a @Version field, which needs special handling, or custom field types...) My idea is to develop annotations and annotation processing tools to improve the situation. Imagine, for a starter, instead of generating all the DTOs and criteria objects, on your entity you could write:

@Entity
@GenerateBean(package="com.mypackage.dto", suffix="DetailedDTO", 
   excludeFields = [ "version", "otherEntities" ], 
   addNewField = [ @ExtraField("String", "someValue") ], 
   constructorStyle = NO_ARG_AND_FULL, 
   typeConversion = [ @TypeConversion("UUID", "String") ])
class MyEntity {

And you would get a com.mypackage.dto.MyEntityDetailedDTO with all the fields from MyEntity, minus the version and 'otherEntities', but a String someValue is added, and all the UUID fields are mapped to String.

If someone needs to customize the generated code, they could easily do it, with copying the generated source to in their source folder, and remove the annotation. Or if it's enough, they can just extend the generated java class in their project. With this annotation based approach we wouldn't need to maintain the separate UserDTO templates anymore, I believe.

Related issues or PR

I'm sure, there were tickets about the huge amounts of code generated ...

  • [X] Checking this box is mandatory (this is just to show you read everything)

gzsombor avatar Apr 28 '21 12:04 gzsombor

@gzsombor : it's an important subject, so you should post a mail to the mailing list and mention this ticket. It needs to be discussed before doing something

pascalgrimaud avatar Apr 29 '21 06:04 pascalgrimaud

Do I hear xdoclet revival? We may still have templates for it somewhere. Actually not having those code under version control is a great idea

ko5tik avatar May 07 '21 08:05 ko5tik

This issue is stale because it has been open 30 days with no activity. Our core developers tend to be more verbose on denying. If there is no negative comment, possibly this feature will be accepted. We are accepting PRs :smiley:. Comment or this will be closed in 7 days

github-actions[bot] avatar Jun 07 '21 00:06 github-actions[bot]

Lets reopen, I've started working on this ...

gzsombor avatar Jun 15 '21 07:06 gzsombor

Have we considered use of Java record ?

vishal423 avatar Jun 15 '21 07:06 vishal423

Yes, definitely. I'm not sure what kind of customization could be feasible, I would really like to have the possibility to even extend the logic by anyone. But I'm a bit concerned the classloading issues on source generation. But as JHipster currently doesn't target Java 16, we can't generate Java records by default. And as I saw, JavaPoet does not support Java record generation - even though a pull request is out there to add: https://github.com/square/javapoet/issues/829 - and there were no recent activity on that project, I fear, we have to wait a little bit.

gzsombor avatar Jun 16 '21 07:06 gzsombor

This issue is stale because it has been open 30 days with no activity. Our core developers tend to be more verbose on denying. If there is no negative comment, possibly this feature will be accepted. We are accepting PRs :smiley:. Comment or this will be closed in 7 days

github-actions[bot] avatar Jul 17 '21 00:07 github-actions[bot]

@gzsombor, what is the current status? Are you still available to create a PR?

DanielFran avatar Oct 31 '23 13:10 DanielFran

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days

github-actions[bot] avatar Apr 29 '24 00:04 github-actions[bot]