Skip to content

Commit 11816d9

Browse files
committed
Apply my suggestions
- phrasing: last , -> and - Reasonably concise `COMPARATOR` and its docs
1 parent 8a9c219 commit 11816d9

File tree

2 files changed

+15
-52
lines changed

2 files changed

+15
-52
lines changed

error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MemberOrdering.java

+9-49
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
99
import static com.google.errorprone.util.ASTHelpers.getSymbol;
1010
import static com.google.errorprone.util.ASTHelpers.isGeneratedConstructor;
11-
import static com.sun.source.tree.Tree.Kind.METHOD;
12-
import static com.sun.source.tree.Tree.Kind.VARIABLE;
1311
import static com.sun.tools.javac.parser.Tokens.TokenKind.LBRACE;
1412
import static java.util.Comparator.comparing;
1513
import static java.util.stream.Collectors.joining;
@@ -44,63 +42,25 @@
4442
summary = "Members should be ordered in a standard way.",
4543
explanation =
4644
"Members should be ordered in a standard way, which is: "
47-
+ "static member variables, non-static member variables, constructors, methods.",
45+
+ "static member variables, non-static member variables, constructors and methods.",
4846
link = BUG_PATTERNS_BASE_URL + "MemberOrdering",
4947
linkType = CUSTOM,
5048
severity = WARNING,
5149
tags = STYLE)
5250
public final class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMatcher {
5351
private static final long serialVersionUID = 1L;
54-
/** A comparator that sorts members, constructors and methods in a standard order. */
52+
/** A comparator that sorts variable and method (incl. constructors) in a standard order. */
5553
private static final Comparator<Tree> COMPARATOR =
56-
comparing(
57-
(Tree memberTree) -> {
58-
switch (memberTree.getKind()) {
59-
case VARIABLE:
60-
return 0;
61-
case METHOD:
62-
return 1;
63-
default:
64-
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
65-
}
66-
})
67-
.thenComparing(
68-
(Tree memberTree) -> {
69-
switch (memberTree.getKind()) {
70-
case VARIABLE:
71-
return isStatic((JCVariableDecl) memberTree) ? 0 : 1;
72-
case METHOD:
73-
return isConstructor((JCMethodDecl) memberTree) ? 0 : 1;
74-
default:
75-
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
76-
}
77-
});
78-
79-
// todo: Evaluate alternative implementation.
80-
/** A comparator that sorts members, constructors and methods in a standard order. */
81-
@SuppressWarnings("unused")
82-
private static final Comparator<Tree> SQUASHED_COMPARATOR =
8354
comparing(
8455
(Tree memberTree) -> {
85-
if (memberTree.getKind() == VARIABLE) {
86-
if (isStatic((JCVariableDecl) memberTree)) {
87-
// 1. static variables.
88-
return 1;
89-
} else {
90-
// 2. non-static variables.
91-
return 2;
92-
}
93-
}
94-
if (memberTree.getKind() == METHOD) {
95-
if (isConstructor((JCMethodDecl) memberTree)) {
96-
// 3. constructors.
97-
return 3;
98-
} else {
99-
// 4. methods.
100-
return 4;
101-
}
56+
switch (memberTree.getKind()) {
57+
case VARIABLE:
58+
return isStatic((JCVariableDecl) memberTree) ? 1 : 2;
59+
case METHOD:
60+
return isConstructor((JCMethodDecl) memberTree) ? 3 : 4;
61+
default:
62+
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
10263
}
103-
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
10464
});
10565

10666
/** Instantiates a new {@link MemberOrdering} instance. */

error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MemberOrderingTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,18 @@ void replacementSecondSuggestedFixConsidersDefaultConstructor() {
197197
" void m1() {}",
198198
"",
199199
" char c = 'c';",
200+
"",
200201
" private static final String foo = \"foo\";",
202+
"",
201203
" static int one = 1;",
202204
"}")
203205
.addOutputLines(
204206
"A.java",
205207
"class A {",
206208
" private static final String foo = \"foo\";",
209+
"",
207210
" static int one = 1;",
211+
"",
208212
" char c = 'c';",
209213
"",
210214
" void m1() {}",
@@ -286,9 +290,10 @@ void replacementSecondSuggestedFixDoesNotModifyWhitespace() {
286290
.doTest();
287291
}
288292

293+
// todo: Actually verify that whitespace is preserved.
289294
@SuppressWarnings("ErrorProneTestHelperSourceFormat")
290295
@Test
291-
void xxx() { // todo: Actually test that the whitespace is preserved.
296+
void xxx() {
292297
BugCheckerRefactoringTestHelper.newInstance(MemberOrdering.class, getClass())
293298
.setFixChooser(SECOND)
294299
.addInputLines(
@@ -334,6 +339,4 @@ void xxx() { // todo: Actually test that the whitespace is preserved.
334339
}
335340

336341
// todo: Test if second replacement considers annotations.
337-
// todo: Chose between with, handles, considers, respects and regards for
338-
// replacementSecondSuggestedFixXxxSomething
339342
}

0 commit comments

Comments
 (0)