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

Sort preconditions by uses-type and uses-method first #122

Merged
merged 3 commits into from
Dec 20, 2024
Merged
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 @@ -25,8 +25,12 @@
import static org.openrewrite.java.template.internal.StringUtils.indent;

public abstract class Precondition {
private static final Comparator<String> BY_USES_TYPE_FIRST = Comparator
.comparing((String s) -> !s.startsWith("new UsesType"))
private static final Comparator<String> BY_USES_TYPE_METHOD_FIRST = Comparator
.comparing((String s) -> {
if (s.startsWith("new UsesType")) return 0;
if (s.startsWith("new UsesMethod")) return 1;
return 2;
})
.thenComparing(Comparator.naturalOrder());

abstract boolean fitsInto(Precondition p);
Expand Down Expand Up @@ -194,7 +198,7 @@ private static String joinPreconditions(Collection<Precondition> rules, String o
} else if (rules.size() == 1) {
return rules.iterator().next().toString();
}
String preconditions = rules.stream().map(Object::toString).sorted(BY_USES_TYPE_FIRST).collect(joining(",\n"));
String preconditions = rules.stream().map(Object::toString).sorted(BY_USES_TYPE_METHOD_FIRST).collect(joining(",\n"));
return "Preconditions." + op + "(\n" +
indent(preconditions, 4) + "\n" +
")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,29 @@
class PreconditionTest {
@Test
void toStringWithInden() {
String result = new Or(
new And(
new Or(new Rule("A"), new Rule("B")),
new Or(new Rule("C"), new Rule("D"))
String result = new And(
new And(new Rule("new UsesMethod<>(\"java.lang.String valueOf(..)\", true)")),
new Or(
new And(new Rule("new UsesMethod<>(\"java.util.HashMap <constructor>(..)\", true)"), new Rule("new UsesType<>(\"java.util.HashMap\", true)")),
new Or(new Rule("new UsesType<>(\"java.util.LinkedHashMap\", true)"))
),
new And(new Rule("X"), new Rule("Y"), new Rule("Z"))
new Rule("new UsesType<>(\"java.util.Map\", true)"),
new Rule("new UsesType<>(\"java.util.List\", true)")
).toString();

assertThat(result).isEqualTo("Preconditions.or(\n" +
" Preconditions.and(\n" +
" Preconditions.or(\n" +
" A,\n" +
" B\n" +
" ),\n" +
" Preconditions.or(\n" +
" C,\n" +
" D\n" +
" )\n" +
" ),\n" +
" Preconditions.and(\n" +
" X,\n" +
" Y,\n" +
" Z\n" +
" )\n" +
")");
assertThat(result).isEqualTo(
"Preconditions.and(\n" +
" new UsesType<>(\"java.util.List\", true),\n" +
" new UsesType<>(\"java.util.Map\", true),\n" +
" new UsesMethod<>(\"java.lang.String valueOf(..)\", true),\n" +
" Preconditions.or(\n" +
" new UsesType<>(\"java.util.LinkedHashMap\", true),\n" +
" Preconditions.and(\n" +
" new UsesType<>(\"java.util.HashMap\", true),\n" +
" new UsesMethod<>(\"java.util.HashMap <constructor>(..)\", true)\n" +
" )\n" +
" )\n" +
")");
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/refaster/PreconditionsVerifierRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.or(
new UsesMethod<>("java.lang.String valueOf(..)", true),
Preconditions.and(
new UsesType<>("com.sun.tools.javac.util.Convert", true),
new UsesMethod<>("com.sun.tools.javac.util.Convert quote(..)", true)
),
new UsesMethod<>("java.lang.String valueOf(..)", true)
)
),
javaVisitor
);
Expand Down Expand Up @@ -546,11 +546,11 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.and(
new UsesMethod<>("java.io.PrintStream println(..)", true),
Preconditions.or(
new UsesType<>("java.util.List", true),
new UsesType<>("java.util.Map", true)
),
new UsesMethod<>("java.io.PrintStream println(..)", true)
)
),
javaVisitor
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/ShouldAddImportsRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.or(
new UsesMethod<>("java.lang.Integer compare(..)", true),
Preconditions.and(
new UsesType<>("java.util.Objects", true),
new UsesMethod<>("java.util.Objects equals(..)", true)
),
new UsesMethod<>("java.lang.Integer compare(..)", true)
)
),
javaVisitor
);
Expand Down
Loading