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

Generate more precise UsesMethod predicates #77

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -19,6 +19,7 @@
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
Expand Down Expand Up @@ -539,9 +540,29 @@ private String generatePreconditions(List<TemplateDescriptor> beforeTemplates, i
if (method.owner.getQualifiedName().toString().startsWith("com.google.errorprone.refaster.")) {
continue;
}
String methodName = method.name.toString();
methodName = methodName.equals("<init>") ? "<constructor>" : methodName;
usesVisitors.add("new UsesMethod<>(\"" + method.owner.getQualifiedName().toString() + ' ' + methodName + "(..)\")");
StringBuilder sb = new StringBuilder();
sb.append("new UsesMethod<>(\"");
sb.append(method.owner.getQualifiedName().toString());
sb.append(' ');
if (method.name.toString().equals("<init>")) {
sb.append("<constructor>");
} else {
sb.append(method.name.toString());
}
com.sun.tools.javac.util.List<Type> parameterTypes = method.type.getParameterTypes();
if (parameterTypes.isEmpty()) {
sb.append("()");
} else {
if ((method.flags() & Flags.VARARGS) == 0 && parameterTypes.stream().allMatch(t -> t instanceof Type.JCPrimitiveType || t instanceof Type.ClassType)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There should probably also be a predicate here that checks that the method indeed has overloads.

StringJoiner joiner = new StringJoiner(", ", "(", ")");
parameterTypes.forEach(t -> joiner.add(t.tsym.type.toString()));
sb.append(joiner);
} else {
sb.append("(..)");
}
}
sb.append("\")");
usesVisitors.add(sb.toString());
}

preconditions.put(beforeTemplate.method.name.toString() + (arity == 1 ? "" : "$" + i), usesVisitors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void skipRecipeGeneration(String recipeName) {
"ShouldSupportNestedClasses",
"SimplifyTernary",
"RefasterAnyOf",
"UsesMethodPrecondition",
})
void nestedRecipes(String recipeName) {
Compilation compilation = compileResource("refaster/" + recipeName + ".java");
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/EscapesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
Preconditions.and(
new UsesType<>("com.sun.tools.javac.util.Convert", true),
new UsesMethod<>("java.lang.String format(..)"),
new UsesMethod<>("com.sun.tools.javac.util.Convert quote(..)")
new UsesMethod<>("com.sun.tools.javac.util.Convert quote(java.lang.String)")
),
javaVisitor
);
Expand Down Expand Up @@ -170,7 +170,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String split(..)"),
new UsesMethod<>("java.lang.String split(java.lang.String)"),
javaVisitor
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/GenericsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.util.List", true),
new UsesMethod<>("java.util.Iterator next(..)"),
new UsesMethod<>("java.util.List iterator(..)")
new UsesMethod<>("java.util.Iterator next()"),
new UsesMethod<>("java.util.List iterator()")
),
javaVisitor
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.and(
new UsesMethod<>("java.lang.String isEmpty(..)"),
new UsesMethod<>("java.lang.String substring(..)")
new UsesMethod<>("java.lang.String isEmpty()"),
new UsesMethod<>("java.lang.String substring(int)")
),
javaVisitor
);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/MethodThrowsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
new UsesType<>("java.nio.file.Files", true),
new UsesType<>("java.nio.charset.StandardCharsets", true),
new UsesType<>("java.nio.file.Path", true),
new UsesMethod<>("java.nio.file.Files readAllLines(..)")
new UsesMethod<>("java.nio.file.Files readAllLines(java.nio.file.Path, java.nio.charset.Charset)")
),
javaVisitor
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
Preconditions.and(
new UsesType<>("java.nio.file.Files", true),
new UsesType<>("java.nio.file.Path", true),
new UsesMethod<>("java.nio.file.Files delete(..)")
new UsesMethod<>("java.nio.file.Files delete(java.nio.file.Path)")
),
javaVisitor
);
Expand Down Expand Up @@ -168,7 +168,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String isEmpty(..)"),
new UsesMethod<>("java.lang.String isEmpty()"),
javaVisitor
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public J visitNewClass(J.NewClass elem, ExecutionContext ctx) {
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.HashMap", true),
new UsesMethod<>("java.util.HashMap <constructor>(..)")
new UsesMethod<>("java.util.HashMap <constructor>(int)")
),
Preconditions.and(
new UsesType<>("java.util.LinkedHashMap", true),
new UsesMethod<>("java.util.LinkedHashMap <constructor>(..)")
new UsesMethod<>("java.util.LinkedHashMap <constructor>(int)")
)
)
),
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/refaster/RefasterAnyOfRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String length(..)"),
new UsesMethod<>("java.lang.String length()"),
javaVisitor
);
}
Expand Down Expand Up @@ -193,11 +193,11 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.LinkedList", true),
new UsesMethod<>("java.util.LinkedList <constructor>(..)")
new UsesMethod<>("java.util.LinkedList <constructor>()")
),
Preconditions.and(
new UsesType<>("java.util.Collections", true),
new UsesMethod<>("java.util.Collections emptyList(..)")
new UsesMethod<>("java.util.Collections emptyList()")
)
)
),
Expand Down
10 changes: 5 additions & 5 deletions src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String valueOf(..)"),
new UsesMethod<>("java.lang.String valueOf(java.lang.Object)"),
javaVisitor
);
}
Expand Down Expand Up @@ -180,9 +180,9 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.Objects", true),
new UsesMethod<>("java.util.Objects equals(..)")
new UsesMethod<>("java.util.Objects equals(java.lang.Object, java.lang.Object)")
),
new UsesMethod<>("java.lang.Integer compare(..)")
new UsesMethod<>("java.lang.Integer compare(int, int)")
),
javaVisitor
);
Expand Down Expand Up @@ -294,8 +294,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext 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(..)")
new UsesMethod<>("java.io.File exists()"),
new UsesMethod<>("java.nio.file.Path toFile()")
),
javaVisitor
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String length(..)"),
new UsesMethod<>("java.lang.String length()"),
javaVisitor
);
}
Expand Down Expand Up @@ -165,7 +165,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String length(..)"),
new UsesMethod<>("java.lang.String length()"),
javaVisitor
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/SimplifyBooleansRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String replaceAll(..)"),
new UsesMethod<>("java.lang.String replaceAll(java.lang.String, java.lang.String)"),
javaVisitor
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {

};
return Preconditions.check(
new UsesMethod<>("java.lang.String length(..)"),
new UsesMethod<>("java.lang.String length()"),
javaVisitor
);
}
Expand Down
75 changes: 75 additions & 0 deletions src/test/resources/refaster/UsesMethodPrecondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 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 foo;

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

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Formatter;
import java.util.List;

public class UsesMethodPrecondition {
public static class Primitive {
@BeforeTemplate
BigDecimal before(double d) {
return new BigDecimal(d);
}

@AfterTemplate
BigDecimal after(double d) {
return BigDecimal.valueOf(d);
}
}

public static class Class {
@BeforeTemplate
String before(String s1, String s2) {
return s1.concat(s2);
}

@AfterTemplate
String after(String s1, String s2) {
return s1 + s2;
}
}

public static class Parameterized {
@BeforeTemplate
List<String> before(String s) {
return Collections.singletonList(s);
}

@AfterTemplate
List<String> after(String s) {
return Arrays.asList(s);
}
}

public static class Varargs {
@BeforeTemplate
String before(String format, String arg0) {
return new Formatter().format(format, arg0).toString();
}

@AfterTemplate
String after(String format, String arg0) {
return String.format(format, arg0);
}
}
}
Loading