001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks; 021 022import java.util.Arrays; 023import java.util.Collections; 024import java.util.Set; 025import java.util.stream.Collectors; 026 027import com.puppycrawl.tools.checkstyle.StatelessCheck; 028import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 032import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 033 034/** 035 * Check that method/constructor/catch/foreach parameters are final. 036 * The user can set the token set to METHOD_DEF, CONSTRUCTOR_DEF, 037 * LITERAL_CATCH, FOR_EACH_CLAUSE or any combination of these token 038 * types, to control the scope of this check. 039 * Default scope is both METHOD_DEF and CONSTRUCTOR_DEF. 040 * <p> 041 * Check has an option <b>ignorePrimitiveTypes</b> which allows ignoring lack of 042 * final modifier at 043 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html"> 044 * primitive data type</a> parameter. Default value <b>false</b>. 045 * </p> 046 * E.g.: 047 * <p> 048 * {@code 049 * private void foo(int x) { ... } //parameter is of primitive type 050 * } 051 * </p> 052 * 053 */ 054@StatelessCheck 055public class FinalParametersCheck extends AbstractCheck { 056 057 /** 058 * A key is pointing to the warning message text in "messages.properties" 059 * file. 060 */ 061 public static final String MSG_KEY = "final.parameter"; 062 063 /** 064 * Contains 065 * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html"> 066 * primitive datatypes</a>. 067 */ 068 private final Set<Integer> primitiveDataTypes = Collections.unmodifiableSet( 069 Arrays.stream(new Integer[] { 070 TokenTypes.LITERAL_BYTE, 071 TokenTypes.LITERAL_SHORT, 072 TokenTypes.LITERAL_INT, 073 TokenTypes.LITERAL_LONG, 074 TokenTypes.LITERAL_FLOAT, 075 TokenTypes.LITERAL_DOUBLE, 076 TokenTypes.LITERAL_BOOLEAN, 077 TokenTypes.LITERAL_CHAR, }) 078 .collect(Collectors.toSet())); 079 080 /** 081 * Option to ignore primitive types as params. 082 */ 083 private boolean ignorePrimitiveTypes; 084 085 /** 086 * Sets ignoring primitive types as params. 087 * @param ignorePrimitiveTypes true or false. 088 */ 089 public void setIgnorePrimitiveTypes(boolean ignorePrimitiveTypes) { 090 this.ignorePrimitiveTypes = ignorePrimitiveTypes; 091 } 092 093 @Override 094 public int[] getDefaultTokens() { 095 return new int[] { 096 TokenTypes.METHOD_DEF, 097 TokenTypes.CTOR_DEF, 098 }; 099 } 100 101 @Override 102 public int[] getAcceptableTokens() { 103 return new int[] { 104 TokenTypes.METHOD_DEF, 105 TokenTypes.CTOR_DEF, 106 TokenTypes.LITERAL_CATCH, 107 TokenTypes.FOR_EACH_CLAUSE, 108 }; 109 } 110 111 @Override 112 public int[] getRequiredTokens() { 113 return CommonUtil.EMPTY_INT_ARRAY; 114 } 115 116 @Override 117 public void visitToken(DetailAST ast) { 118 // don't flag interfaces 119 final DetailAST container = ast.getParent().getParent(); 120 if (container.getType() != TokenTypes.INTERFACE_DEF) { 121 if (ast.getType() == TokenTypes.LITERAL_CATCH) { 122 visitCatch(ast); 123 } 124 else if (ast.getType() == TokenTypes.FOR_EACH_CLAUSE) { 125 visitForEachClause(ast); 126 } 127 else { 128 visitMethod(ast); 129 } 130 } 131 } 132 133 /** 134 * Checks parameters of the method or ctor. 135 * @param method method or ctor to check. 136 */ 137 private void visitMethod(final DetailAST method) { 138 final DetailAST modifiers = 139 method.findFirstToken(TokenTypes.MODIFIERS); 140 // exit on fast lane if there is nothing to check here 141 142 if (method.findFirstToken(TokenTypes.PARAMETERS) 143 .findFirstToken(TokenTypes.PARAMETER_DEF) != null 144 // ignore abstract and native methods 145 && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null 146 && modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) { 147 // we can now be sure that there is at least one parameter 148 final DetailAST parameters = 149 method.findFirstToken(TokenTypes.PARAMETERS); 150 DetailAST child = parameters.getFirstChild(); 151 while (child != null) { 152 // children are PARAMETER_DEF and COMMA 153 if (child.getType() == TokenTypes.PARAMETER_DEF) { 154 checkParam(child); 155 } 156 child = child.getNextSibling(); 157 } 158 } 159 } 160 161 /** 162 * Checks parameter of the catch block. 163 * @param catchClause catch block to check. 164 */ 165 private void visitCatch(final DetailAST catchClause) { 166 checkParam(catchClause.findFirstToken(TokenTypes.PARAMETER_DEF)); 167 } 168 169 /** 170 * Checks parameter of the for each clause. 171 * @param forEachClause for each clause to check. 172 */ 173 private void visitForEachClause(final DetailAST forEachClause) { 174 checkParam(forEachClause.findFirstToken(TokenTypes.VARIABLE_DEF)); 175 } 176 177 /** 178 * Checks if the given parameter is final. 179 * @param param parameter to check. 180 */ 181 private void checkParam(final DetailAST param) { 182 if (param.findFirstToken(TokenTypes.MODIFIERS).findFirstToken(TokenTypes.FINAL) == null 183 && !isIgnoredParam(param) 184 && !CheckUtil.isReceiverParameter(param)) { 185 final DetailAST paramName = param.findFirstToken(TokenTypes.IDENT); 186 final DetailAST firstNode = CheckUtil.getFirstNode(param); 187 log(firstNode, 188 MSG_KEY, paramName.getText()); 189 } 190 } 191 192 /** 193 * Checks for skip current param due to <b>ignorePrimitiveTypes</b> option. 194 * @param paramDef {@link TokenTypes#PARAMETER_DEF PARAMETER_DEF} 195 * @return true if param has to be skipped. 196 */ 197 private boolean isIgnoredParam(DetailAST paramDef) { 198 boolean result = false; 199 if (ignorePrimitiveTypes) { 200 final DetailAST parameterType = paramDef 201 .findFirstToken(TokenTypes.TYPE).getFirstChild(); 202 if (primitiveDataTypes.contains(parameterType.getType())) { 203 result = true; 204 } 205 } 206 return result; 207 } 208 209}