Skip to content

Commit

Permalink
Tolerate but ignore UseImportPolicy to cover additional recipes (#53)
Browse files Browse the repository at this point in the history
* Document what's generated for recipes with new static imports

* Expect `maybeAddImport("java.nio.file.Files", "exists")`

And remove `new UsesMethod<>("java.io.File exists(..)")`

* Add UseImportPolicy as present on similar cases

* UsesMethod should be present; Update description

* Do not skip `UseImportPolicy`

* Remove Javadocs from reference

* Maybe add static imports as needed

* Use both processors for both tests

* Further reduce diff

* Remove for loops with contains checks

* Drop field also super class

* More symmetry in methods

* Only remove/add if there are any static imports

* Skip if statements for now as seen in AssortedRules

* Use `String[]` instead of `Array` as lambda type

* Count messages and early return on invalid

* Do not add static imports; merely ignore UseImportPolicy
  • Loading branch information
timtebeek authored Dec 27, 2023
1 parent 7516697 commit 7993e10
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
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();
}
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);
}
}
}
54 changes: 52 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,52 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
}
}

@SuppressWarnings("all")
@NonNullApi
public static class FileExistsRecipe extends Recipe {

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

}

0 comments on commit 7993e10

Please sign in to comment.