Skip to content

Commit

Permalink
toString improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 16, 2024
1 parent 6eccf0c commit b0a33a1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import lombok.Value;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.*;

import static java.util.stream.Collectors.toSet;
import static java.util.stream.Collectors.toCollection;

@RequiredArgsConstructor
public abstract class PreCondition {
private static final Comparator<String> BY_USES_TYPE_FIRST = Comparator
.comparing((String s) -> !s.startsWith("new UsesType"))
.thenComparing(Comparator.naturalOrder());

abstract boolean fitsInto(PreCondition p);

Expand Down Expand Up @@ -111,7 +112,7 @@ private static String joinPreconditions(Collection<PreCondition> rules, String o
return rules.iterator().next().toString();
}
String whitespace = String.format("%" + indent + "s", " ");
Set<String> preconditions = rules.stream().map(Object::toString).collect(toSet());
Set<String> preconditions = rules.stream().map(Object::toString).sorted(BY_USES_TYPE_FIRST).collect(toCollection(LinkedHashSet::new));
return "Preconditions." + op + "(\n"
+ whitespace + String.join(",\n" + whitespace, preconditions) + "\n"
+ whitespace.substring(0, indent - 4) + ')';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ void toStringWithInden() {
), 4).toString();

assertThat(result).isEqualTo("Preconditions.or(\n" +
" Preconditions.and(\n" +
" X,\n" +
" Y,\n" +
" Z\n" +
"),\n" +
" Preconditions.and(\n" +
" A,\n" +
" B,\n" +
" C\n" +
"),\n" +
" Preconditions.and(\n" +
" X,\n" +
" Y,\n" +
" Z\n" +
")\n" +
")");
}
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 @@ -117,8 +117,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
return Preconditions.check(
Preconditions.and(
new UsesType<>("com.sun.tools.javac.util.Convert", true),
new UsesMethod<>("java.lang.String format(..)", true),
new UsesMethod<>("com.sun.tools.javac.util.Convert quote(..)", true)
new UsesMethod<>("com.sun.tools.javac.util.Convert quote(..)", true),
new UsesMethod<>("java.lang.String format(..)", true)
),
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 @@ -85,8 +85,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.nio.file.Files", true),
new UsesType<>("java.nio.charset.StandardCharsets", true),
new UsesType<>("java.nio.file.Files", true),
new UsesType<>("java.nio.file.Path", true),
new UsesMethod<>("java.nio.file.Files readAllLines(..)", true)
),
Expand Down
21 changes: 10 additions & 11 deletions src/test/resources/refaster/NestedPreconditionsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,16 @@ public J visitNewClass(J.NewClass elem, ExecutionContext ctx) {

};
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.util.Map", true),
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.HashMap", true),
new UsesMethod<>("java.util.HashMap <constructor>(..)", true)
),
Preconditions.and(
new UsesType<>("java.util.LinkedHashMap", true),
new UsesMethod<>("java.util.LinkedHashMap <constructor>(..)", true)
)
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.HashMap", true),
new UsesType<>("java.util.Map", true),
new UsesMethod<>("java.util.HashMap <constructor>(..)", true)
),
Preconditions.and(
new UsesType<>("java.util.LinkedHashMap", true),
new UsesType<>("java.util.Map", true),
new UsesMethod<>("java.util.LinkedHashMap <constructor>(..)", true)
)
),
javaVisitor
Expand Down
11 changes: 7 additions & 4 deletions src/test/resources/refaster/PreConditionsVerifierRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,14 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
Preconditions.and(
new UsesMethod<>("java.io.PrintStream println(..)", true),
Preconditions.or(
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.List", true),
new UsesType<>("java.util.Map", true)
new UsesMethod<>("java.io.PrintStream println(..)", true)
),
Preconditions.and(
new UsesType<>("java.util.Map", true),
new UsesMethod<>("java.io.PrintStream println(..)", true)
)
),
javaVisitor
Expand Down
25 changes: 12 additions & 13 deletions src/test/resources/refaster/RefasterAnyOfRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {

};
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.util.List", true),
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.LinkedList", true),
new UsesMethod<>("java.util.LinkedList <constructor>(..)", true)
),
Preconditions.and(
new UsesType<>("java.util.Collections", true),
new UsesMethod<>("java.util.Collections emptyList(..)", true)
)
Preconditions.or(
Preconditions.and(
new UsesType<>("java.util.Collections", true),
new UsesType<>("java.util.List", true),
new UsesMethod<>("java.util.Collections emptyList(..)", true)
),
Preconditions.and(
new UsesType<>("java.util.LinkedList", true),
new UsesType<>("java.util.List", true),
new UsesMethod<>("java.util.LinkedList <constructor>(..)", true)
)
),
javaVisitor
Expand Down Expand Up @@ -271,8 +270,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.or(
new UsesMethod<>("java.lang.String valueOf(..)", true),
new UsesMethod<>("java.lang.String copyValueOf(..)", true)
new UsesMethod<>("java.lang.String copyValueOf(..)", true),
new UsesMethod<>("java.lang.String valueOf(..)", true)
),
javaVisitor
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/TwoVisitMethodsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
};
return Preconditions.check(
Preconditions.or(
new UsesMethod<>("java.lang.String length(..)", true),
new UsesMethod<>("java.lang.String equals(..)", true)
new UsesMethod<>("java.lang.String equals(..)", true),
new UsesMethod<>("java.lang.String length(..)", true)
),
javaVisitor
);
Expand Down

0 comments on commit b0a33a1

Please sign in to comment.