-
Notifications
You must be signed in to change notification settings - Fork 8
Add additional jar names to classpathFromResources for types referenced from types used
#160
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
Merged
Merged
Changes from 45 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
902b7a7
Added implementation for classpathFromResource
JohannisK bf447cf
Join `jarNames` without `Stream.map(..)`
timtebeek fc56e32
Update expectations
timtebeek b44be87
Do not use `classpathFromResources` for TemplateProcessor just yet
timtebeek 026d86d
Merge branch 'main' into 1136-use-classpathfromresources-instead-of-r…
timtebeek 979e22c
Update expectations for NoGuavaRefasterRecipes.java
timtebeek 06e4ab1
Add an option to generate classpathFromResources
timtebeek 9d7ca4d
Adapt Semantics, PatternBuilder and TemplateProcessor
timtebeek 013c36d
Apply formatter
timtebeek 979aedc
Determine classpathFromResources from first argument
timtebeek ff581ba
Correctly concatenate more than one jar name
timtebeek 77f2aa6
Add transitive jars to classpathFromResources too
timtebeek 867a659
Run CI for pull request targeted at any branch
timtebeek 436f996
Merge branch 'main' into transitive-jarnames
timtebeek f561654
Update ci.yml
timtebeek 73e8f11
Merge branch 'main' into transitive-jarnames
timtebeek cbc503f
Expand to detect more types
timtebeek 854d798
Annotated types come with a classpath dependency
timtebeek c2526b4
Annotated types come with a classpath dependency
timtebeek 8d76c92
Merge branch 'main' into transitive-jarnames
timtebeek f4b16f6
Add ClasspathJarNameDetectorTest
timtebeek 93d8d9a
Reuse `ImportDetector.imports`
timtebeek 053233f
Organize imports
timtebeek ee55b6b
Improve formatting in ClasspathJarNameDetectorTest
timtebeek 8c30c79
Accept suggestions to make the tests pass
timtebeek e508f29
Prune dead code branch according to test coverage
timtebeek 694c92d
Make better use of visitor instead of instanceof checks in scan
timtebeek d32e934
Remove unnecessary method and nullability warnings
timtebeek fdc494e
Minimize what we actually need for current tests
timtebeek b28c470
Trim empty lines
timtebeek c19f3f4
Avoid nullability issue in `detectJUnitAndOpenTest4JFromStatement`
timtebeek 17850a4
Swap test order
timtebeek ad52cd1
Merge branch 'main' into transitive-jarnames
timtebeek d328715
Merge branch 'main' into transitive-jarnames
timtebeek eff6676
Merge branch 'main' into transitive-jarnames
timtebeek f49b773
Adopt text blocks for ClasspathJarNameDetectorTest
timtebeek 304d80a
Apply suggestions from code review
timtebeek b784da7
Add exports for ClasspathJarNameDetectorTest compilation
timtebeek 1acb5c4
Slight polish
timtebeek fc7b9ba
Add a test showing we need transitive dependencies too
timtebeek a241c1b
Also look at super class and interfaces
timtebeek d938e9d
Fix execution in IntelliJ
timtebeek 693fb9c
Add another test for extraction based on first statement
timtebeek e013a4a
Show extraction works even without imports
timtebeek b5c2e7c
Start anew
timtebeek bc02ba4
Apply formatter
timtebeek 3da3a53
Use a private static class to minimize nullable warnings
timtebeek 84fe2ec
Move where ImportsDetector is invoked
timtebeek e5ce73a
Remove the need for ImportDetector in ClasspathJarNameDetector
timtebeek e702d41
Organize imports
timtebeek 50dd501
Collapse loops and visit in logical order
timtebeek a5c394b
Have ClasspathJarNameDetector directly extend TreeScanner
timtebeek 3071974
Inline the static `jarNameFor`
timtebeek 34b646f
Add major version to classpath entries to avoid conflicts
timtebeek 6bfcdb8
Also suffix JSpecify versions
timtebeek d670df4
Use `replaceFirst` as suggested
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/test/java/org/openrewrite/java/template/internal/ClasspathJarNameDetectorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.template.internal; | ||
|
|
||
| import com.sun.source.util.JavacTask; | ||
| import com.sun.tools.javac.api.JavacTool; | ||
| import com.sun.tools.javac.tree.JCTree; | ||
| import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; | ||
| import org.intellij.lang.annotations.Language; | ||
| import org.junit.jupiter.api.Test; | ||
|
timtebeek marked this conversation as resolved.
|
||
|
|
||
| import javax.tools.*; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Set; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.singletonList; | ||
| import static org.openrewrite.java.template.internal.ClasspathJarNameDetector.classpathFor; | ||
|
|
||
| class ClasspathJarNameDetectorTest { | ||
|
|
||
| @Test | ||
| void detectsJarNamesFromImports() throws IOException { | ||
| Set<String> jarNames = compileAndExtractJarNames(""" | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
| class Test { | ||
| List<String> list = new ArrayList<>(); | ||
| } | ||
| """); | ||
|
|
||
| // JDK classes should not be included in the jar names | ||
| assertThat(jarNames).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void detectJUnit() throws IOException { | ||
| Set<String> jarNames = compileAndExtractJarNames(""" | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Assertions; | ||
| class TestClass { | ||
| @Test | ||
| void testMethod() { | ||
| Assertions.assertEquals(1, 1); | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| assertThat(jarNames).containsExactly("junit-jupiter-api"); | ||
| } | ||
|
|
||
| @Test | ||
| void detectJUnitAndOpenTest4J() throws IOException { | ||
| Set<String> jarNames = compileAndExtractJarNames(""" | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Assertions; | ||
| class TestClass { | ||
| @Test | ||
| void testMethod() { | ||
| Assertions.assertAll("This throws org.opentest4j.MultipleFailuresError"); | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| assertThat(jarNames).containsExactly("junit-jupiter-api", "opentest4j"); | ||
| } | ||
|
|
||
| @Test | ||
| void detectJUnitAndOpenTest4JFromStatement() throws IOException { | ||
| JCCompilationUnit compilationUnit = compile(""" | ||
| import org.junit.jupiter.api.Assertions; | ||
| class TestClass { | ||
| void testMethod() { | ||
| Assertions.assertAll("heading"); | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| Set<String> jarNames = classpathFor(firstStatement(compilationUnit), emptyList()); | ||
|
|
||
| // assertAll throws an exception that is defined in opentest4j, so we need both junit-jupiter-api and opentest4j | ||
| assertThat(jarNames).containsExactly("junit-jupiter-api", "opentest4j"); | ||
| } | ||
|
|
||
| @Test | ||
| void detectTransitiveDependencyThroughInheritance() throws IOException { | ||
| JCCompilationUnit compilationUnit = compile(""" | ||
| import org.openrewrite.java.JavaVisitor; | ||
| class TestClass { | ||
| void testMethod() { | ||
| String language = new JavaVisitor<>().getLanguage(); | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| Set<String> jarNames = classpathFor(firstStatement(compilationUnit), emptyList()); | ||
|
|
||
| // JavaVisitor from rewrite-java extends TreeVisitor from rewrite-core, both are needed | ||
| assertThat(jarNames).containsExactly("rewrite-java", "rewrite-core"); | ||
| } | ||
|
|
||
| private static JCTree.JCStatement firstStatement(JCCompilationUnit compilationUnit) { | ||
| // Just the first statement of the method body, not the complete compilation unit, just like the processor does | ||
| JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl) compilationUnit.getTypeDecls().getFirst(); | ||
| JCTree.JCMethodDecl methodDecl = classDecl.getMembers().stream() | ||
| .filter(JCTree.JCMethodDecl.class::isInstance) | ||
| .map(JCTree.JCMethodDecl.class::cast) | ||
| .filter(member -> !member.sym.isConstructor()) | ||
| .findFirst() | ||
| .orElseThrow(); | ||
| return methodDecl.body.getStatements().getFirst(); | ||
| } | ||
|
|
||
| private Set<String> compileAndExtractJarNames(@Language("java") String source) throws IOException { | ||
| JCCompilationUnit compilationUnit = compile(source); | ||
| return classpathFor( | ||
| compilationUnit.getTypeDecls().getFirst(), | ||
| ImportDetector.imports(compilationUnit) | ||
| ); | ||
| } | ||
|
|
||
| private static JCCompilationUnit compile(@Language("java") String source) throws IOException { | ||
| JavaCompiler compiler = JavacTool.create(); | ||
| DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); | ||
| try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, StandardCharsets.UTF_8)) { | ||
| JavaFileObject sourceFile = new SimpleJavaFileObject( | ||
| URI.create("string:///Test.java"), | ||
| JavaFileObject.Kind.SOURCE) { | ||
| @Override | ||
| public CharSequence getCharContent(boolean ignoreEncodingErrors) { | ||
| return source; | ||
| } | ||
| }; | ||
|
|
||
| JavacTask task = (JavacTask) compiler.getTask( | ||
| null, | ||
| fileManager, | ||
| diagnostics, | ||
| singletonList("-proc:none"), | ||
| null, | ||
| singletonList(sourceFile)); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Iterable<? extends JCCompilationUnit> compilationUnits = (Iterable<? extends JCCompilationUnit>) task.parse(); | ||
| task.analyze(); | ||
|
|
||
| return compilationUnits.iterator().next(); | ||
| } | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/test/resources/refaster/ClasspathFromResourcesTransitive.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package foo; | ||
|
|
||
| import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
| import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
| import org.openrewrite.java.JavaVisitor; | ||
|
|
||
| public class ClasspathFromResourcesTransitive { | ||
| @BeforeTemplate | ||
| String before(JavaVisitor visitor) { | ||
| return visitor.getLanguage(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.