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

Make JavaTemplate engine extensible #3475

Merged
merged 19 commits into from
Mar 4, 2024
Merged

Make JavaTemplate engine extensible #3475

merged 19 commits into from
Mar 4, 2024

Conversation

knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Aug 14, 2023

To allow the JavaTemplate mechanism to be used for other languages like Kotlin, the JavaTemplateParser must accept a generic Parser.Builder (i.e. not require JavaParser.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

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
@knutwannheden knutwannheden marked this pull request as draft August 14, 2023 08:14
@knutwannheden
Copy link
Contributor Author

Converted to draft as Kotlin support most likely will require a dedicated KotlinTemplate type.

@jkschneider
Copy link
Member

i worry about the impact on classloading of JvmParser. for now could we avoid the generalization and just create an identical classpath method on KotlinParser.Builder?

@knutwannheden
Copy link
Contributor Author

i worry about the impact on classloading of JvmParser. for now could we avoid the generalization and just create an identical classpath method on KotlinParser.Builder?

Yes, now that we have a KotlinTemplate class that should be possible. I will work that out later today.

@knutwannheden knutwannheden changed the title Add new JvmParser as supertype to JavaParser Make JavaTemplate engine extensible Aug 14, 2023
@knutwannheden knutwannheden marked this pull request as ready for review August 14, 2023 19:48
public Builder javaParser(JavaParser.Builder<?, ?> javaParser) {
this.javaParser = javaParser;
public Builder javaParser(JavaParser.Builder<?, ?> parser) {
this.parser = parser.clone();
Copy link
Member

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?

Copy link
Contributor Author

@knutwannheden knutwannheden Aug 15, 2023

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.

# 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
# 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
@knutwannheden knutwannheden merged commit 3936ba1 into main Mar 4, 2024
1 check passed
@knutwannheden knutwannheden deleted the jvmparser branch March 4, 2024 10:08
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
Development

Successfully merging this pull request may close these issues.

6 participants