Skip to content

Added implementation for classpathFromResource in Refaster templates #139

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JohannisK
Copy link
Contributor

@JohannisK JohannisK commented May 2, 2025

What's changed?

Changed implementation in TemplateCode when generating refaster recipes to use classPathFromResource

What's your motivation?

Ensures Refaster template based recipes are able to match and use external types when running outside of build plugins.

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite May 2, 2025
builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))");
// builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))");
builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, ");
builder.append(jarNames.stream().map(jarName -> '"' + jarName + '"').collect(joining(", ")));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

jarNames is constructed from ClasspathJarNameDetector. Do we need to check if the resulting list is correct and complete, including dependencies from type tables?

@@ -95,13 +95,13 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = JavaTemplate
.builder("String.format(\"\\\"%s\\\"\", com.sun.tools.javac.util.Convert.quote(#{value:any(java.lang.String)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do we "know" that tools is in fact on the classpath or in a type table?
How do we handle situations where the names listed do not match the class path/typetables?

@timtebeek timtebeek changed the title Added implementation for classpathFromResource in refasten templates Added implementation for classpathFromResource in Refaster templates May 2, 2025
@JohannisK
Copy link
Contributor Author

There are several tests for TemplateProcessor that fail, because the ExecutionContext is not available there.

@timtebeek timtebeek marked this pull request as draft May 2, 2025 17:19
Comment on lines +67 to +70
String joinedJarNames = jarNames.stream().collect(joining(", ", "\"", "\""));
builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, ")
.append(joinedJarNames)
.append("))\n ");
Copy link
Member

Choose a reason for hiding this comment

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

Hmm; this now breaks the tests we have for the TemplateProcessor, as the ctx can not be found there, and it looks like that might not be easy to introduce where called from openrewrite/rewrite.

public class ParameterReuseRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.expression(this, "before", (String s) -> s.equals(s));
JavaTemplate.Builder after = Semantics.expression(this, "after", (String s) -> true);
};
}

Luckily it's not a feature we use often, but still it'd be a shame to break that here.

Copy link
Contributor

Choose a reason for hiding this comment

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

We do use it in rewrite-micrometer, IIRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants