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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.TreeScanner;
import org.jspecify.annotations.Nullable;

import javax.tools.JavaFileObject;
import java.util.LinkedHashSet;
Expand All @@ -38,7 +39,7 @@ public class ClasspathJarNameDetector {
public static Set<String> classpathFor(JCTree input, List<Symbol> imports) {
Set<String> jarNames = new LinkedHashSet<String>() {
@Override
public boolean add(String s) {
public boolean add(@Nullable String s) {
return s != null && super.add(s);
}
};
Expand All @@ -64,7 +65,7 @@ public void scan(JCTree tree) {
}


private static String jarNameFor(Symbol anImport) {
private static @Nullable String jarNameFor(Symbol anImport) {
Symbol.ClassSymbol enclClass = anImport instanceof Symbol.ClassSymbol ? (Symbol.ClassSymbol) anImport : anImport.enclClass();
while (enclClass.enclClass() != null && enclClass.enclClass() != enclClass) {
enclClass = enclClass.enclClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ public static <T extends JCTree> String process(T tree, List<JCTree.JCVariableDe
jarNames.addAll(ClasspathJarNameDetector.classpathFor(parameter, ImportDetector.imports(parameter)));
}
if (!jarNames.isEmpty()) {
// It might be preferable to enumerate exactly the needed dependencies rather than the full classpath
// But this is expedient
// See https://github.com/openrewrite/rewrite-templating/issues/86
// String classpath = jarNames.stream().map(jarName -> '"' + jarName + '"').sorted().collect(joining(", "));
// builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpath(").append(classpath).append("))");
builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))");
String joinedJarNames = jarNames.stream().collect(joining(", ", "\"", "\""));
builder.append("\n .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, ")
.append(joinedJarNames)
.append("))\n ");
Comment on lines +67 to +70
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.


}
return builder.toString();
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/ArraysRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Binary file modified src/test/resources/refaster/CharacterEscapeAnnotationRecipe.java
Binary file not shown.
6 changes: 4 additions & 2 deletions src/test/resources/refaster/EscapesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if (before == null) {
before = JavaTemplate.builder("String.format(\"\\\"%s\\\"\", com.sun.tools.javac.util.Convert.quote(#{value:any(java.lang.String)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
if ((matcher = before.matcher(getCursor())).find()) {
maybeRemoveImport("com.sun.tools.javac.util.Convert");
if (after == null) {
after = JavaTemplate.builder("com.sun.tools.javac.util.Constants.format(#{value:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/FindListAddRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/GenericsRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/LambdasRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/MethodThrowsRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/NewBufferedWriterRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/ParametersRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/PicnicRulesRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
21 changes: 14 additions & 7 deletions src/test/resources/refaster/PreconditionsVerifierRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,14 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if (before == null) {
before = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(#{value:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
if ((matcher = before.matcher(getCursor())).find()) {
if (after == null) {
after = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(String.valueOf(#{value:any(java.lang.Object)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
Expand All @@ -201,7 +203,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
if ((matcher = before0.matcher(getCursor())).find()) {
if (after == null) {
after = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(String.valueOf(#{value:any(java.lang.Object)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
Expand Down Expand Up @@ -264,12 +267,14 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if (before == null) {
before = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(#{value:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
if ((matcher = before.matcher(getCursor())).find()) {
if (after == null) {
after = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(String.valueOf(#{value:any(java.lang.Object)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
Expand All @@ -280,12 +285,14 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
}
if (before0 == null) {
before0 = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(String.valueOf(#{value:any(int)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
if ((matcher = before0.matcher(getCursor())).find()) {
if (after == null) {
after = JavaTemplate.builder("com.sun.tools.javac.util.Convert.quote(String.valueOf(#{value:any(java.lang.Object)}))")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())).build();
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tools"))
.build();
}
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/SimplifyBooleansRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/SimplifyTernaryRecipes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/TwoVisitMethodsRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/UnnamedPackageRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2025 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading