|
8 | 8 | import static com.google.errorprone.util.ASTHelpers.getStartPosition;
|
9 | 9 | import static com.google.errorprone.util.ASTHelpers.getSymbol;
|
10 | 10 | 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; |
13 | 11 | import static com.sun.tools.javac.parser.Tokens.TokenKind.LBRACE;
|
14 | 12 | import static java.util.Comparator.comparing;
|
15 | 13 | import static java.util.stream.Collectors.joining;
|
|
44 | 42 | summary = "Members should be ordered in a standard way.",
|
45 | 43 | explanation =
|
46 | 44 | "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.", |
48 | 46 | link = BUG_PATTERNS_BASE_URL + "MemberOrdering",
|
49 | 47 | linkType = CUSTOM,
|
50 | 48 | severity = WARNING,
|
51 | 49 | tags = STYLE)
|
52 | 50 | public final class MemberOrdering extends BugChecker implements BugChecker.ClassTreeMatcher {
|
53 | 51 | 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. */ |
55 | 53 | 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 = |
83 | 54 | comparing(
|
84 | 55 | (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()); |
102 | 63 | }
|
103 |
| - throw new IllegalStateException("Unexpected kind: " + memberTree.getKind()); |
104 | 64 | });
|
105 | 65 |
|
106 | 66 | /** Instantiates a new {@link MemberOrdering} instance. */
|
|
0 commit comments