-
Notifications
You must be signed in to change notification settings - Fork 354
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
Make JavaTemplate
engine extensible
#3475
Conversation
To allow the `JavaTemplate` mechanism to be used for other languages like Kotlin, the provided parser builder must provide a way to add a classpath entry. This is because the internal `__M__` and `__P__` types are required for the parameter substitution and rather than including them as source code in the compilation unit, the parser will load them from the classpath. To this end this PR introduces new `JvmParser` and `JvmParser.Builder` types and the `JavaParser.Builder#classpath()` methods are moved to `JvmParser.Builder`. In order to not increase the API surface area, no corresponding getter was added to `JvmParser.Builder`. Instead, there is a new `Internals` class providing a static `getClasspath(JvmParser.Builder)` method. Issue: openrewrite/rewrite-kotlin#218
Converted to draft as Kotlin support most likely will require a dedicated |
i worry about the impact on classloading of |
Yes, now that we have a KotlinTemplate class that should be possible. I will work that out later today. |
JvmParser
as supertype to JavaParser
JavaTemplate
engine extensible
public Builder javaParser(JavaParser.Builder<?, ?> javaParser) { | ||
this.javaParser = javaParser; | ||
public Builder javaParser(JavaParser.Builder<?, ?> parser) { | ||
this.parser = parser.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this clone()
now when it wasn't before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to call clone before adding the classpath entry for the __M__
and __P__
classes. But it could also be done slightly later. Also, we call clone() later anyway before invoking the parser.
I also like to do defensive copying in builders.
rewrite-java/src/main/java/org/openrewrite/java/JavaTemplate.java
Outdated
Show resolved
Hide resolved
# Conflicts: # rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java # rewrite-java/src/main/java/org/openrewrite/java/JavaTemplate.java # rewrite-java/src/main/java/org/openrewrite/java/ReplaceConstantWithAnotherConstant.java # rewrite-java/src/main/java/org/openrewrite/java/internal/template/Substitutions.java
rewrite-java/src/main/java/org/openrewrite/java/internal/template/__M__.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/internal/template/__P__.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/internal/template/__M__.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/internal/template/__P__.java
Outdated
Show resolved
Hide resolved
# Conflicts: # rewrite-java/src/main/java/org/openrewrite/java/JavaTemplate.java # rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java # rewrite-java/src/main/java/org/openrewrite/java/internal/template/Substitutions.java
To allow the
JavaTemplate
mechanism to be used for other languages like Kotlin, theJavaTemplateParser
must accept a genericParser.Builder
(i.e. not requireJavaParser.Builder
). This parser is expected to be preconfigured with a classpath entry containing the internal__M__
and__P__
types used by the template engine, as these are required for the parameter substitution.Some of the classes and members have been declared as
protected
, but this may change again in the future.Issue: openrewrite/rewrite-kotlin#218