Skip to content

Commit ebb9041

Browse files
committed
Code clean-up, formatting, docs, TEMPORARY supress warnings
1 parent e19422f commit ebb9041

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@
3838
import java.util.stream.Stream;
3939
import javax.lang.model.element.Modifier;
4040

41+
/** A {@link BugChecker} that flags classes with non-standard member ordering. */
4142
@AutoService(BugChecker.class)
4243
@BugPattern(
4344
summary = "Members should be ordered in a standard way.",
45+
explanation =
46+
"Members should be ordered in a standard way, which is: "
47+
+ "static member variables, non-static member variables, constructors, methods.",
4448
link = BUG_PATTERNS_BASE_URL + "MemberOrdering",
4549
linkType = CUSTOM,
4650
severity = WARNING,
4751
tags = STYLE)
48-
public class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMatcher {
52+
public final class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMatcher {
4953
private static final long serialVersionUID = 1L;
5054
/** A comparator that sorts members, constructors and methods in a standard order. */
5155
private static final Comparator<Tree> COMPARATOR =
@@ -56,8 +60,9 @@ public class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMa
5660
return 0;
5761
case METHOD:
5862
return 1;
63+
default:
64+
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
5965
}
60-
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
6166
})
6267
.thenComparing(
6368
(Tree memberTree) -> {
@@ -66,13 +71,16 @@ public class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMa
6671
return isStatic((JCVariableDecl) memberTree) ? 0 : 1;
6772
case METHOD:
6873
return isConstructor((JCMethodDecl) memberTree) ? 0 : 1;
74+
default:
75+
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
6976
}
70-
throw new IllegalStateException("Unexpected kind: " + memberTree.getKind());
7177
});
72-
// XXX: Evaluate alternative implementation.
78+
79+
// todo: Evaluate alternative implementation.
7380
/** A comparator that sorts members, constructors and methods in a standard order. */
81+
@SuppressWarnings("unused")
7482
private static final Comparator<Tree> SQUASHED_COMPARATOR =
75-
Comparator.comparing(
83+
comparing(
7684
(Tree memberTree) -> {
7785
if (memberTree.getKind() == VARIABLE) {
7886
if (isStatic((JCVariableDecl) memberTree)) {
@@ -96,9 +104,7 @@ public class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMa
96104
});
97105

98106
/** Instantiates a new {@link MemberOrdering} instance. */
99-
public MemberOrdering() {
100-
super();
101-
}
107+
public MemberOrdering() {}
102108

103109
@Override
104110
public Description matchClass(ClassTree tree, VisitorState state) {
@@ -134,7 +140,7 @@ private static SuggestedFix swapMembersIncludingComments(
134140
for (int i = 0; i < members.size(); i++) {
135141
Tree original = members.get(i);
136142
Tree correct = sortedMembers.get(i);
137-
// XXX: Technically not necessary, but avoids redundant replacements.
143+
// xxx: Technically not necessary, but avoids redundant replacements.
138144
if (!original.equals(correct)) {
139145
var replacement =
140146
Stream.concat(
@@ -167,7 +173,7 @@ private static boolean isConstructor(JCMethodDecl methodDecl) {
167173
return getSymbol(methodDecl).isConstructor();
168174
}
169175

170-
public static SuggestedFix replaceIncludingComments(
176+
private static SuggestedFix replaceIncludingComments(
171177
ClassTree classTree, Tree member, String replacement, VisitorState state) {
172178
Optional<Tree> previousMember = getPreviousMember(member, classTree);
173179
ImmutableList<ErrorProneToken> tokens =

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void identification() {
2121
"Members, constructors and methods should follow standard ordering.")))
2222
.addSourceLines(
2323
"A.java",
24-
"",
2524
"// BUG: Diagnostic matches: MemberOrdering",
2625
"class A {",
2726
" char a = 'a';",
@@ -30,22 +29,23 @@ void identification() {
3029
"",
3130
" void m2() {}",
3231
"",
33-
" public A () {}",
32+
" public A() {}",
3433
"",
3534
" private static String BAR = \"bar\";",
3635
" char b = 'b';",
3736
"",
3837
" void m1() {",
3938
" System.out.println(\"foo\");",
4039
" }",
40+
"",
4141
" static int TWO = 2;",
4242
"",
4343
" class Inner {}",
44+
"",
4445
" static class StaticInner {}",
4546
"}")
4647
.addSourceLines(
4748
"B.java",
48-
"",
4949
"class B {",
5050
" private static String FOO = \"foo\";",
5151
" static int ONE = 1;",
@@ -56,14 +56,17 @@ void identification() {
5656
" char a = 'a';",
5757
"",
5858
" char b = 'b';",
59-
" public B () {}",
59+
"",
60+
" public B() {}",
6061
"",
6162
" void m1() {",
6263
" System.out.println(\"foo\");",
6364
" }",
65+
"",
6466
" void m2() {}",
6567
"",
6668
" class Inner {}",
69+
"",
6770
" static class StaticInner {}",
6871
"}")
6972
.doTest();
@@ -74,7 +77,6 @@ void replacementFirstSuggestedFix() {
7477
BugCheckerRefactoringTestHelper.newInstance(MemberOrdering.class, getClass())
7578
.addInputLines(
7679
"A.java",
77-
"",
7880
"class A {",
7981
" private static final int X = 1;",
8082
" char a = 'a';",
@@ -83,14 +85,15 @@ void replacementFirstSuggestedFix() {
8385
"",
8486
" void m2() {}",
8587
"",
86-
" public A () {}",
88+
" public A() {}",
8789
"",
8890
" private static String BAR = \"bar\";",
8991
" char b = 'b';",
9092
"",
9193
" void m1() {",
9294
" System.out.println(\"foo\");",
9395
" }",
96+
"",
9497
" static int TWO = 2;",
9598
"",
9699
" class Inner {}",
@@ -99,7 +102,6 @@ void replacementFirstSuggestedFix() {
99102
"}")
100103
.addOutputLines(
101104
"A.java",
102-
"",
103105
"@SuppressWarnings(\"MemberOrdering\")",
104106
"class A {",
105107
" private static final int X = 1;",
@@ -109,14 +111,15 @@ void replacementFirstSuggestedFix() {
109111
"",
110112
" void m2() {}",
111113
"",
112-
" public A () {}",
114+
" public A() {}",
113115
"",
114116
" private static String BAR = \"bar\";",
115117
" char b = 'b';",
116118
"",
117119
" void m1() {",
118120
" System.out.println(\"foo\");",
119121
" }",
122+
"",
120123
" static int TWO = 2;",
121124
"",
122125
" class Inner {}",
@@ -126,6 +129,7 @@ void replacementFirstSuggestedFix() {
126129
.doTest(TestMode.TEXT_MATCH);
127130
}
128131

132+
@SuppressWarnings("ErrorProneTestHelperSourceFormat")
129133
@Test
130134
void replacementSecondSuggestedFix() {
131135
BugCheckerRefactoringTestHelper.newInstance(MemberOrdering.class, getClass())
@@ -152,12 +156,10 @@ void replacementSecondSuggestedFix() {
152156
" static int TWO = 2;",
153157
"",
154158
" class Inner {}",
155-
"",
156159
" static class StaticInner {}",
157160
"}")
158161
.addOutputLines(
159162
"A.java",
160-
"",
161163
"class A {",
162164
" private static final int X = 1;",
163165
" private static String FOO = \"foo\";",
@@ -177,12 +179,12 @@ void replacementSecondSuggestedFix() {
177179
" }",
178180
"",
179181
" class Inner {}",
180-
"",
181182
" static class StaticInner {}",
182183
"}")
183184
.doTest(TestMode.TEXT_MATCH);
184185
}
185186

187+
@SuppressWarnings("ErrorProneTestHelperSourceFormat")
186188
@Test
187189
void replacementSecondSuggestedFixWithDefaultConstructor() {
188190
BugCheckerRefactoringTestHelper.newInstance(MemberOrdering.class, getClass())
@@ -208,32 +210,29 @@ void replacementSecondSuggestedFixWithDefaultConstructor() {
208210
.doTest(TestMode.TEXT_MATCH);
209211
}
210212

213+
@SuppressWarnings("ErrorProneTestHelperSourceFormat")
211214
@Test
212215
void replacementSecondSuggestedFixWithComments() {
213216
BugCheckerRefactoringTestHelper.newInstance(MemberOrdering.class, getClass())
214217
.setFixChooser(SECOND)
215218
.addInputLines(
216219
"A.java",
217-
"",
218220
"class A {",
219221
" // `m1()` comment.",
220222
" void m1() {",
221223
" // Print line 'foo' to stdout.",
222224
" System.out.println(\"foo\");",
223225
" }",
224-
"",
225226
" // foo",
226227
" /** Instantiates a new {@link A} instance. */",
227228
" public A () {}",
228229
"}")
229230
.addOutputLines(
230231
"A.java",
231-
"",
232232
"class A {",
233233
" // foo",
234234
" /** Instantiates a new {@link A} instance. */",
235235
" public A () {}",
236-
"",
237236
" // `m1()` comment.",
238237
" void m1() {",
239238
" // Print line 'foo' to stdout.",

0 commit comments

Comments
 (0)