forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalParametersCheck.java
208 lines (190 loc) · 7.2 KB
/
FinalParametersCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2016 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
* Check that method/constructor/catch/foreach parameters are final.
* The user can set the token set to METHOD_DEF, CONSTRUCTOR_DEF,
* LITERAL_CATCH, FOR_EACH_CLAUSE or any combination of these token
* types, to control the scope of this check.
* Default scope is both METHOD_DEF and CONSTRUCTOR_DEF.
* <p>
* Check has an option <b>ignorePrimitiveTypes</b> which allows ignoring lack of
* final modifier at
* <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">
* primitive data type</a> parameter. Default value <b>false</b>.
* </p>
* E.g.:
* <p>
* {@code
* private void foo(int x) { ... } //parameter is of primitive type
* }
* </p>
*
* @author lkuehne
* @author o_sukhodolsky
* @author Michael Studman
* @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
*/
public class FinalParametersCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "final.parameter";
/**
* Contains
* <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">
* primitive datatypes</a>.
*/
private final Set<Integer> primitiveDataTypes = Collections.unmodifiableSet(
Stream.of(
TokenTypes.LITERAL_BYTE,
TokenTypes.LITERAL_SHORT,
TokenTypes.LITERAL_INT,
TokenTypes.LITERAL_LONG,
TokenTypes.LITERAL_FLOAT,
TokenTypes.LITERAL_DOUBLE,
TokenTypes.LITERAL_BOOLEAN,
TokenTypes.LITERAL_CHAR)
.collect(Collectors.toSet()));
/**
* Option to ignore primitive types as params.
*/
private boolean ignorePrimitiveTypes;
/**
* Sets ignoring primitive types as params.
* @param ignorePrimitiveTypes true or false.
*/
public void setIgnorePrimitiveTypes(boolean ignorePrimitiveTypes) {
this.ignorePrimitiveTypes = ignorePrimitiveTypes;
}
@Override
public int[] getDefaultTokens() {
return new int[] {
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
};
}
@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.LITERAL_CATCH,
TokenTypes.FOR_EACH_CLAUSE,
};
}
@Override
public int[] getRequiredTokens() {
return CommonUtils.EMPTY_INT_ARRAY;
}
@Override
public void visitToken(DetailAST ast) {
// don't flag interfaces
final DetailAST container = ast.getParent().getParent();
if (container.getType() != TokenTypes.INTERFACE_DEF) {
if (ast.getType() == TokenTypes.LITERAL_CATCH) {
visitCatch(ast);
}
else if (ast.getType() == TokenTypes.FOR_EACH_CLAUSE) {
visitForEachClause(ast);
}
else {
visitMethod(ast);
}
}
}
/**
* Checks parameters of the method or ctor.
* @param method method or ctor to check.
*/
private void visitMethod(final DetailAST method) {
final DetailAST modifiers =
method.findFirstToken(TokenTypes.MODIFIERS);
// exit on fast lane if there is nothing to check here
if (method.branchContains(TokenTypes.PARAMETER_DEF)
// ignore abstract and native methods
&& !modifiers.branchContains(TokenTypes.ABSTRACT)
&& !modifiers.branchContains(TokenTypes.LITERAL_NATIVE)) {
// we can now be sure that there is at least one parameter
final DetailAST parameters =
method.findFirstToken(TokenTypes.PARAMETERS);
DetailAST child = parameters.getFirstChild();
while (child != null) {
// children are PARAMETER_DEF and COMMA
if (child.getType() == TokenTypes.PARAMETER_DEF) {
checkParam(child);
}
child = child.getNextSibling();
}
}
}
/**
* Checks parameter of the catch block.
* @param catchClause catch block to check.
*/
private void visitCatch(final DetailAST catchClause) {
checkParam(catchClause.findFirstToken(TokenTypes.PARAMETER_DEF));
}
/**
* Checks parameter of the for each clause.
* @param forEachClause for each clause to check.
*/
private void visitForEachClause(final DetailAST forEachClause) {
checkParam(forEachClause.findFirstToken(TokenTypes.VARIABLE_DEF));
}
/**
* Checks if the given parameter is final.
* @param param parameter to check.
*/
private void checkParam(final DetailAST param) {
if (!param.branchContains(TokenTypes.FINAL) && !isIgnoredParam(param)
&& !CheckUtils.isReceiverParameter(param)) {
final DetailAST paramName = param.findFirstToken(TokenTypes.IDENT);
final DetailAST firstNode = CheckUtils.getFirstNode(param);
log(firstNode.getLineNo(), firstNode.getColumnNo(),
MSG_KEY, paramName.getText());
}
}
/**
* Checks for skip current param due to <b>ignorePrimitiveTypes</b> option.
* @param paramDef {@link TokenTypes#PARAMETER_DEF PARAMETER_DEF}
* @return true if param has to be skipped.
*/
private boolean isIgnoredParam(DetailAST paramDef) {
boolean result = false;
if (ignorePrimitiveTypes) {
final DetailAST parameterType = paramDef
.findFirstToken(TokenTypes.TYPE).getFirstChild();
if (primitiveDataTypes.contains(parameterType.getType())) {
result = true;
}
}
return result;
}
}