1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.modifier;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import com.puppycrawl.tools.checkstyle.StatelessCheck;
27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 @StatelessCheck
92 public class ModifierOrderCheck
93 extends AbstractCheck {
94
95
96
97
98
99 public static final String MSG_ANNOTATION_ORDER = "annotation.order";
100
101
102
103
104
105 public static final String MSG_MODIFIER_ORDER = "mod.order";
106
107
108
109
110
111 private static final String[] JLS_ORDER = {
112 "public", "protected", "private", "abstract", "default", "static",
113 "sealed", "non-sealed", "final", "transient", "volatile",
114 "synchronized", "native", "strictfp",
115 };
116
117 @Override
118 public int[] getDefaultTokens() {
119 return getRequiredTokens();
120 }
121
122 @Override
123 public int[] getAcceptableTokens() {
124 return getRequiredTokens();
125 }
126
127 @Override
128 public int[] getRequiredTokens() {
129 return new int[] {TokenTypes.MODIFIERS};
130 }
131
132 @Override
133 public void visitToken(DetailAST ast) {
134 final List<DetailAST> mods = new ArrayList<>();
135 DetailAST modifier = ast.getFirstChild();
136 while (modifier != null) {
137 mods.add(modifier);
138 modifier = modifier.getNextSibling();
139 }
140
141 if (!mods.isEmpty()) {
142 final DetailAST error = checkOrderSuggestedByJls(mods);
143 if (error != null) {
144 if (error.getType() == TokenTypes.ANNOTATION) {
145 log(error,
146 MSG_ANNOTATION_ORDER,
147 error.getFirstChild().getText()
148 + error.getFirstChild().getNextSibling()
149 .getText());
150 }
151 else {
152 log(error, MSG_MODIFIER_ORDER, error.getText());
153 }
154 }
155 }
156 }
157
158
159
160
161
162
163
164
165
166 private static DetailAST checkOrderSuggestedByJls(List<DetailAST> modifiers) {
167 final Iterator<DetailAST> iterator = modifiers.iterator();
168
169
170 DetailAST modifier = skipAnnotations(iterator);
171
172 DetailAST offendingModifier = null;
173
174
175 if (modifier.getType() != TokenTypes.ANNOTATION) {
176 int index = 0;
177
178 while (modifier != null
179 && offendingModifier == null) {
180 if (modifier.getType() == TokenTypes.ANNOTATION) {
181 if (!isAnnotationOnType(modifier)) {
182
183 offendingModifier = modifier;
184 }
185 break;
186 }
187
188 while (index < JLS_ORDER.length
189 && !JLS_ORDER[index].equals(modifier.getText())) {
190 index++;
191 }
192
193 if (index == JLS_ORDER.length) {
194
195 offendingModifier = modifier;
196 }
197 else if (iterator.hasNext()) {
198 modifier = iterator.next();
199 }
200 else {
201
202 modifier = null;
203 }
204 }
205 }
206 return offendingModifier;
207 }
208
209
210
211
212
213
214
215 private static DetailAST skipAnnotations(Iterator<DetailAST> modifierIterator) {
216 DetailAST modifier;
217 do {
218 modifier = modifierIterator.next();
219 } while (modifierIterator.hasNext() && modifier.getType() == TokenTypes.ANNOTATION);
220 return modifier;
221 }
222
223
224
225
226
227
228
229 private static boolean isAnnotationOnType(DetailAST modifier) {
230 boolean annotationOnType = false;
231 final DetailAST modifiers = modifier.getParent();
232 final DetailAST definition = modifiers.getParent();
233 final int definitionType = definition.getType();
234 if (definitionType == TokenTypes.VARIABLE_DEF
235 || definitionType == TokenTypes.PARAMETER_DEF
236 || definitionType == TokenTypes.CTOR_DEF) {
237 annotationOnType = true;
238 }
239 else if (definitionType == TokenTypes.METHOD_DEF) {
240 final DetailAST typeToken = definition.findFirstToken(TokenTypes.TYPE);
241 final int methodReturnType = typeToken.getLastChild().getType();
242 if (methodReturnType != TokenTypes.LITERAL_VOID) {
243 annotationOnType = true;
244 }
245 }
246 return annotationOnType;
247 }
248
249 }