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 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 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 @@ -67,8 +67,7 @@ public class RefasterTemplateProcessor extends TypeAwareProcessor {
"com.google.errorprone.refaster.annotation.NotMatches",
"com.google.errorprone.refaster.annotation.OfKind",
"com.google.errorprone.refaster.annotation.Placeholder",
"com.google.errorprone.refaster.annotation.Repeated",
"com.google.errorprone.refaster.annotation.UseImportPolicy"
"com.google.errorprone.refaster.annotation.Repeated"
).collect(Collectors.toSet());

static ClassValue<List<String>> LST_TYPE_MAP = new ClassValue<List<String>>() {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/google/errorprone/refaster/ImportPolicy.java
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
}
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();
}
Comment on lines +24 to +28
Copy link
Contributor Author

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.

17 changes: 17 additions & 0 deletions src/test/resources/refaster/ShouldAddImports.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package foo;

import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;

import java.nio.file.Path;
import java.util.Objects;

import static java.nio.file.Files.exists;
import static java.util.Objects.hash;

public class ShouldAddImports {
Expand Down Expand Up @@ -63,4 +67,17 @@ int after(String s) {
return s.hashCode();
}
}

public static class FileExists {
@BeforeTemplate
boolean before(Path path) {
return path.toFile().exists();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
boolean after(Path path) {
return exists(path);
}
}
}
61 changes: 59 additions & 2 deletions src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +58,8 @@ public List<Recipe> getRecipeList() {
return Arrays.asList(
new StringValueOfRecipe(),
new ObjectsEqualsRecipe(),
new StaticImportObjectsHashRecipe()
new StaticImportObjectsHashRecipe(),
new FileExistsRecipe()
);
}

Expand Down Expand Up @@ -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");
Copy link
Contributor Author

@timtebeek timtebeek Dec 23, 2023

Choose a reason for hiding this comment

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

Note that this maybeAddImport is not necessary when we use Files.exists(path) in the @AfterTemplate instead of a static import there. But unfortunately in error-prone-support they use this style often, such as in MockitoRules.

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
);
}
}

}
Loading