7

The Java project I am working on currently has a complicated folder structure, and to add a new functionality, one needs to add many .java files in different places in order to let it work in our project structure. For example, when I want to add a new object User, I need to add Entity, JsonModel, Controller, Listener, Publisher.., etc. It'd be easily missed if handled manually, so I'm looking for some way to automatically generate .java files dedicated to one same object in different paths on demand.

For example, the programmer set the tool to generate .java files about User and it generates UserEntity, UserInputModel... into fit places, and then the programmer go write the actual code for those .java files.

Is there a common way to handle this problem in Java? Maybe a famous tool, or some framework? I also ain't quite sure what's the keyword I should look for on this question. Code Generation doesn't seem to mean the same, while Generate Classes leads to the similar discussions. They're both more about automatically coding or transforming natural languages into actual codes. But here I just want to automate the .java file creations, not the code in them.

I am not native English speaker, so I hope if you can, please also point me to the right word on this topic.

Bergi
  • 996
  • 7
  • 15
  • 3
    When you really need to add 6 different classes to introduce one new kind of business object, then your project might be suffering from software overengineering. – Philipp Jul 22 '22 at 13:03
  • 1
    @Philipp maybe - or maybe the separation into 6 classes is useful because over time some or all those six classes will likely grow to to an extent where it's useful to have them separated. – bdsl Jul 22 '22 at 14:58
  • I don't think 6 new classes is bad at all. In my current project, a new business object includes the object, its entity id, mediator pattern classes, repository and test stubs for all generated classes. This brings consistency and prevents 'forgetful' developers from creating tests. – Neil Jul 22 '22 at 15:26
  • Many projects use a _build tool_ or _build system_ that not only handles compilation and generation of build artifacts (executables, JARs,...), but also pre-processing tasks like code generation. For Java I think Maven or Gradle are the most common ones, but many others exist: Visual Studio uses MSBuild, many C++ projects use CMake, etc... Many of these can support multiple languages, but a specific tool is generally better for a core set of languages that it most often used and developed with; – F.X. Jul 22 '22 at 17:04

2 Answers2

10

I think the word you're looking for is a template. Most IDEs support some kind of template mechanism; specifically in the Java world, if you happen to use IntelliJ, it supports multi-file templates that solve your exact problem. You can create a template that includes boilerplate code for all the classes you want to generate, using a variable for the object name. When you instantiate the template, it will create a bunch of files and replace the variable with the name you specify.

If you don't use IntelliJ or need to do something fancier, you can also build a custom solution based on one of the common template languages out there (IntelliJ itself uses Velocity, but there are many other options out there).

casablanca
  • 4,934
  • 16
  • 24
  • 1
    This is exactly the solution I am currently look for. Thank you very much! Not sure why someone downvoted this though, I am curious for reason. – Xiang Wei Huang Jul 22 '22 at 06:45
5

To automatically make .class files you compile .java files. To automatically make .java files you do something called code generation. If you do this the .java files will not be "source code" files. They will be "intermediate code" files. You should document them as that to keep people from fiddling with them.

Code generation is an old idea. Used to be Python generated C code (as it's intermediate code) that was compiled to machine code. The trick is making sure people don't edit the intermediate code thinking it's source. Once they do that things get very confusing.

If you really do have to always add 6+ classes together when making a change then code generation might be helpful. It also might be worth considering redesigning the system so you don't have to add classes that way.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • Oops, I am really sorry, I've confused `.java` and `.class`. I meant generating `.java` files, for coding only. I'd be very happy to redesign the system too, but well... the design is by my bosses and I can't interfere about that too much. – Xiang Wei Huang Jul 22 '22 at 05:48
  • I have fixed the wording around `.java files` in the OP. Really sorry for the confusion! – Xiang Wei Huang Jul 22 '22 at 05:49
  • As for your answer, I think it's then different from what I want because the `.java` files generated are meant to be changed by the programmers, as they have only the minimum codes (boilerplates?) needed after generated. – Xiang Wei Huang Jul 22 '22 at 05:53
  • @XiangWeiHuang This is starting to sound more like a use for static metaprogramming - https://stackoverflow.com/a/57638025/1301901 – Ben Cottrell Jul 22 '22 at 06:35
  • 1
    @BenCottrell Thanks for the suggestion, first time hearing about that interesting idea. The `JavaPoet` approach seems similar to what I was looking for. – Xiang Wei Huang Jul 22 '22 at 07:08
  • 1
    @XiangWeiHuang if that doesn't fit, then consider adding a concrete example to the question. Annotations are often used to generate code in that way (e.g. methods inside classes which already contains other developer-written code); indeed that code can call/use the auto-generated code despite it not being visible to developers in their IDE. (For example, Lombok generates methods called `getXyz()` and `setXyz()` -- even though developers can't actually *see* those methods on-screen with their own eyes, they exist and are usable just like any other method (IDEs/tools can usually see them). – Ben Cottrell Jul 22 '22 at 07:20