-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 5 commits
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 d1ce2f7
Expect `maybeAddImport("java.nio.file.Files", "exists")`
timtebeek 895cd02
Add UseImportPolicy as present on similar cases
timtebeek 2b1fdd0
UsesMethod should be present; Update description
timtebeek 052dad2
Do not skip `UseImportPolicy`
timtebeek 512dfe8
Remove Javadocs from reference
timtebeek 1636aee
Maybe add static imports as needed
timtebeek ed9b4da
Use both processors for both tests
timtebeek deff084
Merge branch 'main' into support-adding-new-static-imports
timtebeek ce0e0da
Merge remote-tracking branch 'origin/main' into support-adding-new-st…
timtebeek 00a2131
Further reduce diff
timtebeek 6cd955e
Remove for loops with contains checks
timtebeek 6abfa6d
Drop field also super class
timtebeek ebced83
More symmetry in methods
timtebeek 51ccd94
Only remove/add if there are any static imports
timtebeek 1e403a1
Skip if statements for now as seen in AssortedRules
timtebeek eb6f602
Use `String[]` instead of `Array` as lambda type
timtebeek 95485b2
Count messages and early return on invalid
timtebeek 1bd4a65
Merge branch 'main' into support-adding-new-static-imports
timtebeek d84ea61
Do not add static imports; merely ignore UseImportPolicy
timtebeek 14dfa14
Merge branch 'main' into support-adding-new-static-imports
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 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
22 changes: 22 additions & 0 deletions
22
src/test/java/com/google/errorprone/refaster/ImportPolicy.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,22 @@ | ||
/* | ||
* Copyright 2023 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 com.google.errorprone.refaster; | ||
|
||
public enum ImportPolicy { | ||
IMPORT_TOP_LEVEL, | ||
IMPORT_CLASS_DIRECTLY, | ||
STATIC_IMPORT_ALWAYS | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/google/errorprone/refaster/annotation/UseImportPolicy.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,28 @@ | ||
/* | ||
* Copyright 2023 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 com.google.errorprone.refaster.annotation; | ||
|
||
import com.google.errorprone.refaster.ImportPolicy; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface UseImportPolicy { | ||
ImportPolicy value(); | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,12 +34,13 @@ | |
import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*; | ||
|
||
import java.util.Objects; | ||
import java.nio.file.Path; | ||
|
||
import static java.nio.file.Files.exists; | ||
import static java.util.Objects.hash; | ||
|
||
@SuppressWarnings("all") | ||
public class ShouldAddImportsRecipes extends Recipe { | ||
|
||
public ShouldAddImportsRecipes() {} | ||
|
||
@Override | ||
|
@@ -57,7 +58,8 @@ public List<Recipe> getRecipeList() { | |
return Arrays.asList( | ||
new StringValueOfRecipe(), | ||
new ObjectsEqualsRecipe(), | ||
new StaticImportObjectsHashRecipe() | ||
new StaticImportObjectsHashRecipe(), | ||
new FileExistsRecipe() | ||
); | ||
} | ||
|
||
|
@@ -210,4 +212,59 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { | |
} | ||
} | ||
|
||
/** | ||
* OpenRewrite recipe created for Refaster template {@code ShouldAddImports.FileExists}. | ||
*/ | ||
@SuppressWarnings("all") | ||
@NonNullApi | ||
public static class FileExistsRecipe extends Recipe { | ||
|
||
/** | ||
* Instantiates a new instance. | ||
*/ | ||
public FileExistsRecipe() {} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Refaster template `ShouldAddImports.FileExists`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Recipe created for the following Refaster template:\n```java\npublic static class FileExists {\n \n @BeforeTemplate()\n boolean before(Path path) {\n return path.toFile().exists();\n }\n \n @AfterTemplate()\n @UseImportPolicy(value = ImportPolicy.STATIC_IMPORT_ALWAYS)\n boolean after(Path path) {\n return exists(path);\n }\n}\n```\n."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() { | ||
final JavaTemplate before = Semantics.expression(this, "before", (java.nio.file.Path path) -> path.toFile().exists()).build(); | ||
final JavaTemplate after = Semantics.expression(this, "after", (java.nio.file.Path path) -> exists(path)).build(); | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { | ||
JavaTemplate.Matcher matcher; | ||
if ((matcher = before.matcher(getCursor())).find()) { | ||
maybeAddImport("java.nio.file.Files", "exists"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this |
||
return embed( | ||
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), | ||
getCursor(), | ||
ctx, | ||
SHORTEN_NAMES, SIMPLIFY_BOOLEANS | ||
); | ||
} | ||
return super.visitMethodInvocation(elem, ctx); | ||
} | ||
|
||
}; | ||
return Preconditions.check( | ||
Preconditions.and( | ||
new UsesType<>("java.nio.file.Path", true), | ||
new UsesMethod<>("java.io.File exists(..)"), | ||
new UsesMethod<>("java.nio.file.Path toFile(..)") | ||
), | ||
javaVisitor | ||
); | ||
} | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just to show we are able to handle/ignore this annotation; I don't plan to support it's individual values.