Refactor the Java code generator to allow “template variants”#3124
Merged
Conversation
This PR refactors the Java code generator to make it possible to have an
arbitrary number of "template variants", where a variant can be used to
represent a particular "style" of Java code.
This replaces the previous code that was used to implement the
`--generate-records` option by a more generic mechanism.
There is no longer a default template that is embedded directly into the
code of the Java generator module. Instead, all templates, for all
variants, are expected to be located in the `javagen` directory, and
named according to the following convention:
NAME[-VARIANT].jinja2
where NAME is the name of the class or enum to be generated (can be
`class` for a generic template to use for all classes, or `enum` for a
generic template to use for all enums) and VARIANT is an optional
variant name.
When the generator has to generate the code file for a class named
`Foo` using the variant `bar`, it will successively search for the
following templates, using the first one that is found:
1. Foo-bar.jinja2 (template specific for the Foo class, bar variant);
2. class-bar.jinja2 (generic class template, bar variant);
3. Foo.jinja2 (template specific for the Foo class, default variant);
4. class.jinja2 (generic class template, default variant).
Steps 1 and 2 are skipped if no explicit variant is requested.
The same logic applies when generating the code for an enum (assuming
the `--true-enums` option is used), with the fallback templates in step
2 and 4 being named `enum-bar.jinja2` and `enum.jinja2` in that case.
On the command line, two new options are added:
* `--template-dir` to specify an alternative directory for the
templates;
* `--template-variant` to specify the variant to use.
The existing option `--generate-records` is kept for backwards
compatibility, and is now equivalent to `--template-variant=records`.
The `--template-file` option can still be used to completely bypass the
template selection mechanism and use a single template for all the
objects to generate.
Add a test to check that we can still use the `--template-file` option of the Java code generator and get the expected results, bypassing the generator's own logic to pick up the template.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3124 +/- ##
==========================================
+ Coverage 79.86% 83.62% +3.75%
==========================================
Files 149 149
Lines 16888 16924 +36
Branches 3465 3476 +11
==========================================
+ Hits 13488 14152 +664
+ Misses 2659 1981 -678
- Partials 741 791 +50
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
1. Damn "modern" editors. 2. Why isn't that check part of `uv run tox -e lint`?
kevinschaper
approved these changes
Feb 5, 2026
kevinschaper
left a comment
Contributor
There was a problem hiding this comment.
very nice! thank you!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR refactors the Java code generator to make it possible to have an arbitrary number of "template variants", where a variant can be used to represent a particular "style" of Java code. This implements the idea proposed in #3073.
This replaces the previous code that was used to implement the
--generate-recordsoption by a more generic mechanism.There is no longer a default template that is embedded directly into the code of the Java generator module. Instead, all templates, for all variants, are expected to be located in the
javagendirectory, and named according to the following convention:where NAME is the name of the class or enum to be generated (can be
classfor a generic template to use for all classes, orenumfor a generic template to use for all enums) and VARIANT is an optional variant name.When the generator has to generate the code file for a class named Foo using the variant
bar, it will successively search for the following templates, using the first one that is found:Foo-bar.jinja2(template specific for the Foo class,barvariant);class-bar.jinja2(generic class template,barvariant);Foo.jinja2(template specific for the Foo class, default variant);class.jinja2(generic class template, default variant).Steps 1 and 2 are skipped if no explicit variant is requested.
The same logic applies when generating the code for an enum (assuming the recently introduced
--true-enumsoption is used), with the fallback templates in step 2 and 4 being namedenum-bar.jinja2andenum.jinja2in that case.On the command line, two new options are added:
--template-dirto specify an alternative directory for the templates;--template-variantto specify the variant to use.The existing option
--generate-recordsis kept for backwards compatibility, and is now equivalent to--template-variant=records.The
--template-fileoption can still be used to completely bypass the template selection mechanism and use a single template for all the objects to generate.