1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks;
21
22 import java.util.BitSet;
23
24 import com.puppycrawl.tools.checkstyle.StatelessCheck;
25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
31
32 /**
33 * <div>
34 * Checks that parameters for methods, constructors, catch and for-each blocks are final.
35 * Interface, abstract, and native methods are not checked: the final keyword
36 * does not make sense for interface, abstract, and native method parameters as
37 * there is no code that could modify the parameter.
38 * </div>
39 *
40 * <p>
41 * Rationale: Changing the value of parameters during the execution of the method's
42 * algorithm can be confusing and should be avoided. A great way to let the Java compiler
43 * prevent this coding style is to declare parameters final.
44 * </p>
45 *
46 * @since 3.0
47 */
48 @StatelessCheck
49 public class FinalParametersCheck extends AbstractCheck {
50
51 /**
52 * A key is pointing to the warning message text in "messages.properties" file.
53 */
54 public static final String MSG_KEY = "final.parameter";
55
56 /**
57 * Contains
58 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">
59 * primitive datatypes</a>.
60 */
61 private final BitSet primitiveDataTypes = TokenUtil.asBitSet(
62 TokenTypes.LITERAL_BYTE,
63 TokenTypes.LITERAL_SHORT,
64 TokenTypes.LITERAL_INT,
65 TokenTypes.LITERAL_LONG,
66 TokenTypes.LITERAL_FLOAT,
67 TokenTypes.LITERAL_DOUBLE,
68 TokenTypes.LITERAL_BOOLEAN,
69 TokenTypes.LITERAL_CHAR
70 );
71
72 /**
73 * Ignore primitive types as parameters.
74 */
75 private boolean ignorePrimitiveTypes;
76
77 /**
78 * Ignore <a href="https://docs.oracle.com/en/java/javase/21/docs/specs/unnamed-jls.html">
79 * unnamed parameters</a>.
80 */
81 private boolean ignoreUnnamedParameters = true;
82
83 /**
84 * Creates a new {@code FinalParametersCheck} instance.
85 */
86 public FinalParametersCheck() {
87 // no code by default
88 }
89
90 /**
91 * Setter to ignore primitive types as parameters.
92 *
93 * @param ignorePrimitiveTypes true or false.
94 * @since 6.2
95 */
96 public void setIgnorePrimitiveTypes(boolean ignorePrimitiveTypes) {
97 this.ignorePrimitiveTypes = ignorePrimitiveTypes;
98 }
99
100 /**
101 * Setter to ignore
102 * <a href="https://docs.oracle.com/en/java/javase/21/docs/specs/unnamed-jls.html">
103 * unnamed parameters</a>.
104 *
105 * @param ignoreUnnamedParameters true or false.
106 * @since 10.18.0
107 */
108 public void setIgnoreUnnamedParameters(boolean ignoreUnnamedParameters) {
109 this.ignoreUnnamedParameters = ignoreUnnamedParameters;
110 }
111
112 @Override
113 public int[] getDefaultTokens() {
114 return new int[] {
115 TokenTypes.METHOD_DEF,
116 TokenTypes.CTOR_DEF,
117 };
118 }
119
120 @Override
121 public int[] getAcceptableTokens() {
122 return new int[] {
123 TokenTypes.METHOD_DEF,
124 TokenTypes.CTOR_DEF,
125 TokenTypes.LITERAL_CATCH,
126 TokenTypes.FOR_EACH_CLAUSE,
127 TokenTypes.PATTERN_VARIABLE_DEF,
128 };
129 }
130
131 @Override
132 public int[] getRequiredTokens() {
133 return CommonUtil.EMPTY_INT_ARRAY;
134 }
135
136 @Override
137 public void visitToken(DetailAST ast) {
138 if (ast.getType() == TokenTypes.LITERAL_CATCH) {
139 visitCatch(ast);
140 }
141 else if (ast.getType() == TokenTypes.FOR_EACH_CLAUSE) {
142 visitForEachClause(ast);
143 }
144 else if (ast.getType() == TokenTypes.PATTERN_VARIABLE_DEF) {
145 visitPatternVariableDef(ast);
146 }
147 else {
148 visitMethod(ast);
149 }
150 }
151
152 /**
153 * Checks parameter of the pattern variable definition.
154 *
155 * @param patternVariableDef pattern variable definition to check
156 */
157 private void visitPatternVariableDef(final DetailAST patternVariableDef) {
158 checkParam(patternVariableDef);
159 }
160
161 /**
162 * Checks parameters of the method or ctor.
163 *
164 * @param method method or ctor to check.
165 */
166 private void visitMethod(final DetailAST method) {
167 // skip if there is no method body
168 // - abstract method
169 // - interface method (not implemented)
170 // - native method
171 if (method.findFirstToken(TokenTypes.SLIST) != null) {
172 final DetailAST parameters =
173 method.findFirstToken(TokenTypes.PARAMETERS);
174 TokenUtil.forEachChild(parameters, TokenTypes.PARAMETER_DEF, this::checkParam);
175 }
176 }
177
178 /**
179 * Checks parameter of the catch block.
180 *
181 * @param catchClause catch block to check.
182 */
183 private void visitCatch(final DetailAST catchClause) {
184 checkParam(catchClause.findFirstToken(TokenTypes.PARAMETER_DEF));
185 }
186
187 /**
188 * Checks parameter of the for each clause.
189 *
190 * @param forEachClause for each clause to check.
191 */
192 private void visitForEachClause(final DetailAST forEachClause) {
193 final DetailAST variableDef = forEachClause.findFirstToken(TokenTypes.VARIABLE_DEF);
194 if (variableDef != null) {
195 // can be missing for record pattern def
196 // (only available as a preview feature in Java 20, never released)
197 checkParam(variableDef);
198 }
199 }
200
201 /**
202 * Checks if the given parameter is final.
203 *
204 * @param param parameter to check.
205 */
206 private void checkParam(final DetailAST param) {
207 if (param.findFirstToken(TokenTypes.MODIFIERS).findFirstToken(TokenTypes.FINAL) == null
208 && !isIgnoredPrimitiveParam(param)
209 && !isIgnoredUnnamedParam(param)
210 && !CheckUtil.isReceiverParameter(param)) {
211 final DetailAST paramName = TokenUtil.getIdent(param);
212 final DetailAST firstNode = CheckUtil.getFirstNode(param);
213 log(firstNode,
214 MSG_KEY, paramName.getText());
215 }
216 }
217
218 /**
219 * Checks for skip current param due to <b>ignorePrimitiveTypes</b> option.
220 *
221 * @param paramDef {@link TokenTypes#PARAMETER_DEF PARAMETER_DEF}
222 * @return true if param has to be skipped.
223 */
224 private boolean isIgnoredPrimitiveParam(DetailAST paramDef) {
225 boolean result = false;
226 if (ignorePrimitiveTypes) {
227 final DetailAST type = paramDef.findFirstToken(TokenTypes.TYPE);
228 final DetailAST parameterType = type.getFirstChild();
229 final DetailAST arrayDeclarator = type
230 .findFirstToken(TokenTypes.ARRAY_DECLARATOR);
231 if (arrayDeclarator == null
232 && primitiveDataTypes.get(parameterType.getType())) {
233 result = true;
234 }
235 }
236 return result;
237 }
238
239 /**
240 * Checks for skip current param due to <b>ignoreUnnamedParameters</b> option.
241 *
242 * @param paramDef parameter to check
243 * @return true if the parameter should be skipped due to the ignoreUnnamedParameters option.
244 */
245 private boolean isIgnoredUnnamedParam(final DetailAST paramDef) {
246 final DetailAST paramName = paramDef.findFirstToken(TokenTypes.IDENT);
247 return ignoreUnnamedParameters && paramName != null && "_".equals(paramName.getText());
248 }
249
250 }