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

Tolerate but ignore UseImportPolicy to cover additional recipes #53

Merged
merged 21 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ca83b59
Document what's generated for recipes with new static imports
timtebeek Dec 23, 2023
d1ce2f7
Expect `maybeAddImport("java.nio.file.Files", "exists")`
timtebeek Dec 23, 2023
895cd02
Add UseImportPolicy as present on similar cases
timtebeek Dec 23, 2023
2b1fdd0
UsesMethod should be present; Update description
timtebeek Dec 23, 2023
052dad2
Do not skip `UseImportPolicy`
timtebeek Dec 23, 2023
512dfe8
Remove Javadocs from reference
timtebeek Dec 23, 2023
1636aee
Maybe add static imports as needed
timtebeek Dec 23, 2023
ed9b4da
Use both processors for both tests
timtebeek Dec 23, 2023
deff084
Merge branch 'main' into support-adding-new-static-imports
timtebeek Dec 23, 2023
ce0e0da
Merge remote-tracking branch 'origin/main' into support-adding-new-st…
timtebeek Dec 23, 2023
00a2131
Further reduce diff
timtebeek Dec 23, 2023
6cd955e
Remove for loops with contains checks
timtebeek Dec 23, 2023
6abfa6d
Drop field also super class
timtebeek Dec 23, 2023
ebced83
More symmetry in methods
timtebeek Dec 23, 2023
51ccd94
Only remove/add if there are any static imports
timtebeek Dec 23, 2023
1e403a1
Skip if statements for now as seen in AssortedRules
timtebeek Dec 24, 2023
eb6f602
Use `String[]` instead of `Array` as lambda type
timtebeek Dec 24, 2023
95485b2
Count messages and early return on invalid
timtebeek Dec 24, 2023
1bd4a65
Merge branch 'main' into support-adding-new-static-imports
timtebeek Dec 27, 2023
d84ea61
Do not add static imports; merely ignore UseImportPolicy
timtebeek Dec 27, 2023
14dfa14
Merge branch 'main' into support-adding-new-static-imports
timtebeek Dec 27, 2023
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 @@ -254,10 +254,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
}

maybeRemoveImports(imports, recipe, entry.getValue(), descriptor.afterTemplate);
if (!staticImports.isEmpty()) {
maybeRemoveImports(staticImports, recipe, entry.getValue(), descriptor.afterTemplate);
maybeAddStaticImports(staticImports, recipe, entry.getValue(), descriptor.afterTemplate);
}
maybeRemoveImports(staticImports, recipe, entry.getValue(), descriptor.afterTemplate);

List<String> embedOptions = new ArrayList<>();
if (getType(descriptor.afterTemplate) == JCTree.JCParens.class) {
Expand Down Expand Up @@ -501,22 +498,6 @@ private void maybeRemoveImports(Map<JCTree.JCMethodDecl, Set<String>> importsByT
beforeImports.forEach(anImport -> recipe.append(" maybeRemoveImport(\"").append(anImport).append("\");\n"));
}

private void maybeAddStaticImports(Map<JCTree.JCMethodDecl, Set<String>> importsByTemplate, StringBuilder recipe, JCTree.JCMethodDecl beforeTemplate, JCTree.JCMethodDecl afterTemplate) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we really already remove this code?

Set<String> afterImports = getImportsAsStrings(importsByTemplate, afterTemplate);
afterImports.removeAll(getBeforeImportsAsStrings(importsByTemplate, beforeTemplate));
afterImports.removeIf(i -> i.startsWith("java.lang."));
afterImports.forEach(anImport -> {
int lastDot = anImport.lastIndexOf('.');
if (0 < lastDot) {
recipe.append(" maybeAddImport(\"")
.append(anImport, 0, lastDot)
.append("\", \"")
.append(anImport.substring(lastDot + 1))
.append("\");\n");
}
});
}

private Set<String> getBeforeImportsAsStrings(Map<JCTree.JCMethodDecl, Set<String>> importsByTemplate, JCTree.JCMethodDecl templateMethod) {
Set<String> beforeImports = getImportsAsStrings(importsByTemplate, templateMethod);
for (JCTree.JCMethodDecl beforeTemplate : importsByTemplate.keySet()) {
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeAddImport("java.nio.file.Files", "exists");
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
Expand Down
Loading