-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate classpath entries for FQN references (#19)
* Generate classpath entries for FQN references * Detect classpath and add to generated templates * Simplify test cases; we're testing imports not a full replacement * Reduce visitor duplication by reusing imports * Fix detection * Adopt a TreeSet to sort classpath entries * Sort after filtering
- Loading branch information
Showing
8 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
84 changes: 84 additions & 0 deletions
84
src/main/java/org/openrewrite/java/template/internal/ClasspathJarNameDetector.java
This file contains 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,84 @@ | ||
/* | ||
* Copyright 2022 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.tools.javac.code.Symbol; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import com.sun.tools.javac.tree.JCTree.JCFieldAccess; | ||
import com.sun.tools.javac.tree.TreeScanner; | ||
|
||
import javax.tools.JavaFileObject; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class ClasspathJarNameDetector { | ||
|
||
/** | ||
* Locate types that are directly referred to by name in the | ||
* given tree and therefore need an import in the template. | ||
* | ||
* @return The list of imports to add. | ||
*/ | ||
public static String classpathFor(JCTree input, List<Symbol> imports) { | ||
Set<String> jarNames = new LinkedHashSet<>(); | ||
|
||
for (Symbol anImport : imports) { | ||
jarNames.add(jarNameFor(anImport)); | ||
} | ||
|
||
// Detect fully qualified classes | ||
new TreeScanner() { | ||
@Override | ||
public void scan(JCTree tree) { | ||
if (tree instanceof JCFieldAccess && | ||
((JCFieldAccess) tree).sym instanceof Symbol.ClassSymbol && | ||
Character.isUpperCase(((JCFieldAccess) tree).getIdentifier().toString().charAt(0))) { | ||
jarNames.add(jarNameFor(((JCFieldAccess) tree).sym)); | ||
} | ||
super.scan(tree); | ||
} | ||
}.scan(input); | ||
|
||
return jarNames.stream() | ||
.filter(Objects::nonNull) | ||
.map(jarName -> '"' + jarName + '"') | ||
.sorted() | ||
.collect(Collectors.joining(", ")); | ||
} | ||
|
||
|
||
private static 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(); | ||
} | ||
JavaFileObject classfile = enclClass.classfile; | ||
if (classfile != null) { | ||
String uriStr = classfile.toUri().toString(); | ||
Matcher matcher = Pattern.compile("([^/]*)?\\.jar!/").matcher(uriStr); | ||
if (matcher.find()) { | ||
String jarName = matcher.group(1); | ||
return jarName.replaceAll("-\\d.*$", ""); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains 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 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
10 changes: 10 additions & 0 deletions
10
src/test/resources/recipes/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_after.java
This file contains 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,10 @@ | ||
package foo; | ||
import org.openrewrite.java.*; | ||
|
||
public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_after { | ||
public static JavaTemplate.Builder getTemplate(JavaVisitor<?> visitor) { | ||
return JavaTemplate | ||
.builder("org.slf4j.LoggerFactory.getLogger(#{any(java.lang.String)})") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/resources/recipes/ShouldAddClasspathRecipe$FullyQualifiedRecipe$1_before.java
This file contains 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,9 @@ | ||
package foo; | ||
import org.openrewrite.java.*; | ||
|
||
public class ShouldAddClasspathRecipes$FullyQualifiedRecipe$1_before { | ||
public static JavaTemplate.Builder getTemplate(JavaVisitor<?> visitor) { | ||
return JavaTemplate | ||
.builder("System.out.println(#{any(java.lang.String)})"); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...pes/ShouldAddClasspathRecipe$1_after.java → ...pathRecipe$UnqualifiedRecipe$1_after.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...es/ShouldAddClasspathRecipe$1_before.java → ...athRecipe$UnqualifiedRecipe$1_before.java
This file contains 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