Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Javatemplate in RefasterTemplateProcessor #57

Merged
merged 30 commits into from
Mar 4, 2024

Conversation

timtebeek
Copy link
Contributor

WIP to replace directly create JavaTemplate in RefasterTemplateProcessor instead of indirection.

@timtebeek timtebeek added the enhancement New feature or request label Dec 27, 2023
@timtebeek timtebeek self-assigned this Dec 27, 2023
@timtebeek
Copy link
Contributor Author

@knutwannheden As expected TemplateProcessorTest now fails, as we no longer generate the Semantics statements that operates on; how would you like to handle that?

  • Should we adapt the test inputs to use Semantics directly to trigger similar output?
  • Should we adapt TemplateProcessor to read @Before/AfterTemplate annotated methods?
  • Should we retain PatternBuilder at all now that the indirection isn't techically needed anymore?

@timtebeek timtebeek marked this pull request as ready for review January 2, 2024 22:10
@timtebeek timtebeek marked this pull request as draft January 2, 2024 22:10
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that we finally got this working!

final JavaTemplate before = Semantics.expression(this, "before", (String[] strings) -> String.join(", ", strings)).build();
final JavaTemplate after = Semantics.expression(this, "after", (String[] strings) -> String.join(":", strings)).build();
final JavaTemplate before = JavaTemplate
.builder("String.join(\", \", #{strings:any(java.lang.String[])})")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised the String reference isn't qualified here. Will look into that separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, we have an explicit exception for java.lang:

print(sym.packge().fullname.contentEquals("java.lang") ? sym.name : sym.getQualifiedName());

It does make the template code a bit more readable, but it could cause problems when the Java class an after template gets embedded into is in a package which has a class by the same name, which is unusual but not unheard of.

I am a bit on the fence on this one. java.lang is a special case also in the ShortenFullyQualifiedTypeReferences recipe (for basically the same reason). But I think in probably around 90% of the cases the name would end up getting shortened, because the compilation unit already has another (unqualified) reference to the same type in which case the recipe knows it can shorten the reference.

As I pointed out before, this isn't directly related to your PR. It is just something that caught my eye.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can tackle this separately; there's a few other items too I'd been looking to do after this lands, since any changes previously were likely to conflict with this one.

.build();
final JavaTemplate after = JavaTemplate
.builder("String.join(\":\", #{strings:any(java.lang.String[])})")
.build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only downside I see with this refactoring is that the generated code is now harder to read and that the Semantics mechanism isn't visible anymore. But I think that is acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the main motivator for me was PRs like this one that aim to use Java 9+ constructs in recipes, which previously meant that code was in the generated recipes as well(likely?) bumping the Java version required to run the recipe.

With this change I think we should be good to have the generated recipes included with the other Java 8 compatible classes.

@timtebeek timtebeek merged commit ab2dee2 into main Mar 4, 2024
1 check passed
@timtebeek timtebeek deleted the inline-javatemplate-in-refaster-template-processor branch March 4, 2024 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
2 participants