| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2018 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.naming; | |
| 21 | ||
| 22 | import java.util.Arrays; | |
| 23 | import java.util.HashSet; | |
| 24 | import java.util.LinkedList; | |
| 25 | import java.util.List; | |
| 26 | import java.util.Set; | |
| 27 | import java.util.stream.Collectors; | |
| 28 | ||
| 29 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
| 30 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 31 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 32 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 33 | import com.puppycrawl.tools.checkstyle.utils.CheckUtils; | |
| 34 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 35 | ||
| 36 | /** | |
| 37 | * <p> | |
| 38 | * The Check validate abbreviations(consecutive capital letters) length in | |
| 39 | * identifier name, it also allows to enforce camel case naming. Please read more at | |
| 40 | * <a href= | |
| 41 | * "http://checkstyle.sourceforge.net/reports/google-java-style-20170228.html#s5.3-camel-case"> | |
| 42 | * Google Style Guide</a> to get to know how to avoid long abbreviations in names. | |
| 43 | * </p> | |
| 44 | * <p> | |
| 45 | * {@code allowedAbbreviationLength} specifies how many consecutive capital letters are | |
| 46 | * allowed in the identifier. | |
| 47 | * A value of <i>3</i> indicates that up to 4 consecutive capital letters are allowed, | |
| 48 | * one after the other, before a violation is printed. The identifier 'MyTEST' would be | |
| 49 | * allowed, but 'MyTESTS' would not be. | |
| 50 | * A value of <i>0</i> indicates that only 1 consecutive capital letter is allowed. This | |
| 51 | * is what should be used to enforce strict camel casing. The identifier 'MyTest' would | |
| 52 | * be allowed, but 'MyTEst' would not be. | |
| 53 | * </p> | |
| 54 | * <p> | |
| 55 | * Option {@code allowedAbbreviationLength} indicates on the allowed amount of capital | |
| 56 | * letters in abbreviations in the classes, interfaces, | |
| 57 | * variables and methods names. Default value is '3'. | |
| 58 | * </p> | |
| 59 | * <p> | |
| 60 | * Option {@code allowedAbbreviations} - list of abbreviations that | |
| 61 | * must be skipped for checking. Abbreviations should be separated by comma, | |
| 62 | * no spaces are allowed. | |
| 63 | * </p> | |
| 64 | * <p> | |
| 65 | * Option {@code ignoreFinal} allow to skip variables with {@code final} modifier. | |
| 66 | * Default value is {@code true}. | |
| 67 | * </p> | |
| 68 | * <p> | |
| 69 | * Option {@code ignoreStatic} allow to skip variables with {@code static} modifier. | |
| 70 | * Default value is {@code true}. | |
| 71 | * </p> | |
| 72 | * <p> | |
| 73 | * Option {@code ignoreOverriddenMethod} - Allows to | |
| 74 | * ignore methods tagged with {@code @Override} annotation | |
| 75 | * (that usually mean inherited name). Default value is {@code true}. | |
| 76 | * </p> | |
| 77 | * Default configuration | |
| 78 | * <pre> | |
| 79 | * <module name="AbbreviationAsWordInName" /> | |
| 80 | * </pre> | |
| 81 | * <p> | |
| 82 | * To configure to check variables and classes identifiers, do not ignore | |
| 83 | * variables with static modifier | |
| 84 | * and allow no abbreviations (enforce camel case phrase) but allow XML and URL abbreviations. | |
| 85 | * </p> | |
| 86 | * <pre> | |
| 87 | * <module name="AbbreviationAsWordInName"> | |
| 88 | * <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/> | |
| 89 | * <property name="ignoreStatic" value="false"/> | |
| 90 | * <property name="allowedAbbreviationLength" value="1"/> | |
| 91 | * <property name="allowedAbbreviations" value="XML,URL"/> | |
| 92 | * </module> | |
| 93 | * </pre> | |
| 94 | * | |
| 95 | * @author Roman Ivanov, Daniil Yaroslvtsev, Baratali Izmailov | |
| 96 | */ | |
| 97 | @StatelessCheck | |
| 98 | public class AbbreviationAsWordInNameCheck extends AbstractCheck { | |
| 99 | ||
| 100 | /** | |
| 101 | * Warning message key. | |
| 102 | */ | |
| 103 | public static final String MSG_KEY = "abbreviation.as.word"; | |
| 104 | ||
| 105 | /** | |
| 106 | * The default value of "allowedAbbreviationLength" option. | |
| 107 | */ | |
| 108 | private static final int DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH = 3; | |
| 109 | ||
| 110 | /** | |
| 111 | * Variable indicates on the allowed amount of capital letters in | |
| 112 | * abbreviations in the classes, interfaces, variables and methods names. | |
| 113 | */ | |
| 114 | private int allowedAbbreviationLength = | |
| 115 | DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH; | |
| 116 | ||
| 117 | /** | |
| 118 | * Set of allowed abbreviation to ignore in check. | |
| 119 | */ | |
| 120 | private Set<String> allowedAbbreviations = new HashSet<>(); | |
| 121 | ||
| 122 | /** Allows to ignore variables with 'final' modifier. */ | |
| 123 | private boolean ignoreFinal = true; | |
| 124 | ||
| 125 | /** Allows to ignore variables with 'static' modifier. */ | |
| 126 | private boolean ignoreStatic = true; | |
| 127 | ||
| 128 | /** Allows to ignore methods with '@Override' annotation. */ | |
| 129 | private boolean ignoreOverriddenMethods = true; | |
| 130 | ||
| 131 | /** | |
| 132 | * Sets ignore option for variables with 'final' modifier. | |
| 133 | * @param ignoreFinal | |
| 134 | * Defines if ignore variables with 'final' modifier or not. | |
| 135 | */ | |
| 136 | public void setIgnoreFinal(boolean ignoreFinal) { | |
| 137 | this.ignoreFinal = ignoreFinal; | |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Sets ignore option for variables with 'static' modifier. | |
| 142 | * @param ignoreStatic | |
| 143 | * Defines if ignore variables with 'static' modifier or not. | |
| 144 | */ | |
| 145 | public void setIgnoreStatic(boolean ignoreStatic) { | |
| 146 | this.ignoreStatic = ignoreStatic; | |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * Sets ignore option for methods with "@Override" annotation. | |
| 151 | * @param ignoreOverriddenMethods | |
| 152 | * Defines if ignore methods with "@Override" annotation or not. | |
| 153 | */ | |
| 154 | public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods) { | |
| 155 | this.ignoreOverriddenMethods = ignoreOverriddenMethods; | |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 159 | * Allowed abbreviation length in names. | |
| 160 | * @param allowedAbbreviationLength | |
| 161 | * amount of allowed capital letters in abbreviation. | |
| 162 | */ | |
| 163 | public void setAllowedAbbreviationLength(int allowedAbbreviationLength) { | |
| 164 | this.allowedAbbreviationLength = allowedAbbreviationLength; | |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * Set a list of abbreviations that must be skipped for checking. | |
| 169 | * Abbreviations should be separated by comma, no spaces is allowed. | |
| 170 | * @param allowedAbbreviations | |
| 171 | * an string of abbreviations that must be skipped from checking, | |
| 172 | * each abbreviation separated by comma. | |
| 173 | */ | |
| 174 | public void setAllowedAbbreviations(String... allowedAbbreviations) { | |
| 175 |
1
1. setAllowedAbbreviations : negated conditional → KILLED |
if (allowedAbbreviations != null) { |
| 176 | this.allowedAbbreviations = | |
| 177 | Arrays.stream(allowedAbbreviations).collect(Collectors.toSet()); | |
| 178 | } | |
| 179 | } | |
| 180 | ||
| 181 | @Override | |
| 182 | public int[] getDefaultTokens() { | |
| 183 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 184 | TokenTypes.CLASS_DEF, | |
| 185 | TokenTypes.INTERFACE_DEF, | |
| 186 | TokenTypes.ENUM_DEF, | |
| 187 | TokenTypes.ANNOTATION_DEF, | |
| 188 | TokenTypes.ANNOTATION_FIELD_DEF, | |
| 189 | TokenTypes.PARAMETER_DEF, | |
| 190 | TokenTypes.VARIABLE_DEF, | |
| 191 | TokenTypes.METHOD_DEF, | |
| 192 | }; | |
| 193 | } | |
| 194 | ||
| 195 | @Override | |
| 196 | public int[] getAcceptableTokens() { | |
| 197 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
| 198 | TokenTypes.CLASS_DEF, | |
| 199 | TokenTypes.INTERFACE_DEF, | |
| 200 | TokenTypes.ENUM_DEF, | |
| 201 | TokenTypes.ANNOTATION_DEF, | |
| 202 | TokenTypes.ANNOTATION_FIELD_DEF, | |
| 203 | TokenTypes.PARAMETER_DEF, | |
| 204 | TokenTypes.VARIABLE_DEF, | |
| 205 | TokenTypes.METHOD_DEF, | |
| 206 | TokenTypes.ENUM_CONSTANT_DEF, | |
| 207 | }; | |
| 208 | } | |
| 209 | ||
| 210 | @Override | |
| 211 | public int[] getRequiredTokens() { | |
| 212 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 213 | } | |
| 214 | ||
| 215 | @Override | |
| 216 | public void visitToken(DetailAST ast) { | |
| 217 |
1
1. visitToken : negated conditional → KILLED |
if (!isIgnoreSituation(ast)) { |
| 218 | final DetailAST nameAst = ast.findFirstToken(TokenTypes.IDENT); | |
| 219 | final String typeName = nameAst.getText(); | |
| 220 | ||
| 221 | final String abbr = getDisallowedAbbreviation(typeName); | |
| 222 |
1
1. visitToken : negated conditional → KILLED |
if (abbr != null) { |
| 223 |
2
1. visitToken : Replaced integer addition with subtraction → KILLED 2. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::log → KILLED |
log(nameAst.getLineNo(), MSG_KEY, typeName, allowedAbbreviationLength + 1); |
| 224 | } | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * Checks if it is an ignore situation. | |
| 230 | * @param ast input DetailAST node. | |
| 231 | * @return true if it is an ignore situation found for given input DetailAST | |
| 232 | * node. | |
| 233 | * @noinspection SimplifiableIfStatement | |
| 234 | */ | |
| 235 | private boolean isIgnoreSituation(DetailAST ast) { | |
| 236 | final DetailAST modifiers = ast.getFirstChild(); | |
| 237 | ||
| 238 | final boolean result; | |
| 239 |
1
1. isIgnoreSituation : negated conditional → KILLED |
if (ast.getType() == TokenTypes.VARIABLE_DEF) { |
| 240 |
2
1. isIgnoreSituation : negated conditional → KILLED 2. isIgnoreSituation : negated conditional → KILLED |
if ((ignoreFinal || ignoreStatic) |
| 241 |
1
1. isIgnoreSituation : negated conditional → KILLED |
&& isInterfaceDeclaration(ast)) { |
| 242 | // field declarations in interface are static/final | |
| 243 | result = true; | |
| 244 | } | |
| 245 | else { | |
| 246 |
1
1. isIgnoreSituation : negated conditional → KILLED |
result = ignoreFinal |
| 247 |
2
1. isIgnoreSituation : negated conditional → KILLED 2. isIgnoreSituation : negated conditional → KILLED |
&& modifiers.findFirstToken(TokenTypes.FINAL) != null |
| 248 | || ignoreStatic | |
| 249 |
1
1. isIgnoreSituation : negated conditional → KILLED |
&& modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null; |
| 250 | } | |
| 251 | } | |
| 252 |
1
1. isIgnoreSituation : negated conditional → KILLED |
else if (ast.getType() == TokenTypes.METHOD_DEF) { |
| 253 |
2
1. isIgnoreSituation : negated conditional → KILLED 2. isIgnoreSituation : negated conditional → KILLED |
result = ignoreOverriddenMethods && hasOverrideAnnotation(modifiers); |
| 254 | } | |
| 255 | else { | |
| 256 | result = CheckUtils.isReceiverParameter(ast); | |
| 257 | } | |
| 258 |
1
1. isIgnoreSituation : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 259 | } | |
| 260 | ||
| 261 | /** | |
| 262 | * Check that variable definition in interface or @interface definition. | |
| 263 | * @param variableDefAst variable definition. | |
| 264 | * @return true if variable definition(variableDefAst) is in interface | |
| 265 | * or @interface definition. | |
| 266 | */ | |
| 267 | private static boolean isInterfaceDeclaration(DetailAST variableDefAst) { | |
| 268 | boolean result = false; | |
| 269 | final DetailAST astBlock = variableDefAst.getParent(); | |
| 270 | final DetailAST astParent2 = astBlock.getParent(); | |
| 271 | ||
| 272 |
1
1. isInterfaceDeclaration : negated conditional → KILLED |
if (astParent2.getType() == TokenTypes.INTERFACE_DEF |
| 273 |
1
1. isInterfaceDeclaration : negated conditional → KILLED |
|| astParent2.getType() == TokenTypes.ANNOTATION_DEF) { |
| 274 | result = true; | |
| 275 | } | |
| 276 |
1
1. isInterfaceDeclaration : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 277 | } | |
| 278 | ||
| 279 | /** | |
| 280 | * Checks that the method has "@Override" annotation. | |
| 281 | * @param methodModifiersAST | |
| 282 | * A DetailAST nod is related to the given method modifiers | |
| 283 | * (MODIFIERS type). | |
| 284 | * @return true if method has "@Override" annotation. | |
| 285 | */ | |
| 286 | private static boolean hasOverrideAnnotation(DetailAST methodModifiersAST) { | |
| 287 | boolean result = false; | |
| 288 | for (DetailAST child : getChildren(methodModifiersAST)) { | |
| 289 |
1
1. hasOverrideAnnotation : negated conditional → KILLED |
if (child.getType() == TokenTypes.ANNOTATION) { |
| 290 | final DetailAST annotationIdent = child.findFirstToken(TokenTypes.IDENT); | |
| 291 | ||
| 292 |
2
1. hasOverrideAnnotation : negated conditional → KILLED 2. hasOverrideAnnotation : negated conditional → KILLED |
if (annotationIdent != null && "Override".equals(annotationIdent.getText())) { |
| 293 | result = true; | |
| 294 | break; | |
| 295 | } | |
| 296 | } | |
| 297 | } | |
| 298 |
1
1. hasOverrideAnnotation : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return result; |
| 299 | } | |
| 300 | ||
| 301 | /** | |
| 302 | * Gets the disallowed abbreviation contained in given String. | |
| 303 | * @param str | |
| 304 | * the given String. | |
| 305 | * @return the disallowed abbreviation contained in given String as a | |
| 306 | * separate String. | |
| 307 | */ | |
| 308 | private String getDisallowedAbbreviation(String str) { | |
| 309 | int beginIndex = 0; | |
| 310 | boolean abbrStarted = false; | |
| 311 | String result = null; | |
| 312 | ||
| 313 |
3
1. getDisallowedAbbreviation : changed conditional boundary → KILLED 2. getDisallowedAbbreviation : Changed increment from 1 to -1 → KILLED 3. getDisallowedAbbreviation : negated conditional → KILLED |
for (int index = 0; index < str.length(); index++) { |
| 314 | final char symbol = str.charAt(index); | |
| 315 | ||
| 316 |
1
1. getDisallowedAbbreviation : negated conditional → KILLED |
if (Character.isUpperCase(symbol)) { |
| 317 |
1
1. getDisallowedAbbreviation : negated conditional → KILLED |
if (!abbrStarted) { |
| 318 | abbrStarted = true; | |
| 319 | beginIndex = index; | |
| 320 | } | |
| 321 | } | |
| 322 |
1
1. getDisallowedAbbreviation : negated conditional → KILLED |
else if (abbrStarted) { |
| 323 | abbrStarted = false; | |
| 324 | ||
| 325 |
1
1. getDisallowedAbbreviation : Replaced integer subtraction with addition → KILLED |
final int endIndex = index - 1; |
| 326 | // -1 as a first capital is usually beginning of next word | |
| 327 | result = getAbbreviationIfIllegal(str, beginIndex, endIndex); | |
| 328 |
1
1. getDisallowedAbbreviation : negated conditional → KILLED |
if (result != null) { |
| 329 | break; | |
| 330 | } | |
| 331 | beginIndex = -1; | |
| 332 | } | |
| 333 | } | |
| 334 | // if abbreviation at the end of name and it is not single character (example: scaleX) | |
| 335 |
3
1. getDisallowedAbbreviation : Replaced integer subtraction with addition → KILLED 2. getDisallowedAbbreviation : negated conditional → KILLED 3. getDisallowedAbbreviation : negated conditional → KILLED |
if (abbrStarted && beginIndex != str.length() - 1) { |
| 336 | final int endIndex = str.length(); | |
| 337 | result = getAbbreviationIfIllegal(str, beginIndex, endIndex); | |
| 338 | } | |
| 339 |
1
1. getDisallowedAbbreviation : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getDisallowedAbbreviation to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return result; |
| 340 | } | |
| 341 | ||
| 342 | /** | |
| 343 | * Get Abbreviation if it is illegal. | |
| 344 | * @param str name | |
| 345 | * @param beginIndex begin index | |
| 346 | * @param endIndex end index | |
| 347 | * @return true is abbreviation is bigger that required and not in ignore list | |
| 348 | */ | |
| 349 | private String getAbbreviationIfIllegal(String str, int beginIndex, int endIndex) { | |
| 350 | String result = null; | |
| 351 |
1
1. getAbbreviationIfIllegal : Replaced integer subtraction with addition → KILLED |
final int abbrLength = endIndex - beginIndex; |
| 352 |
2
1. getAbbreviationIfIllegal : changed conditional boundary → KILLED 2. getAbbreviationIfIllegal : negated conditional → KILLED |
if (abbrLength > allowedAbbreviationLength) { |
| 353 | final String abbr = str.substring(beginIndex, endIndex); | |
| 354 |
1
1. getAbbreviationIfIllegal : negated conditional → KILLED |
if (!allowedAbbreviations.contains(abbr)) { |
| 355 | result = abbr; | |
| 356 | } | |
| 357 | } | |
| 358 |
1
1. getAbbreviationIfIllegal : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getAbbreviationIfIllegal to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return result; |
| 359 | } | |
| 360 | ||
| 361 | /** | |
| 362 | * Gets all the children which are one level below on the current DetailAST | |
| 363 | * parent node. | |
| 364 | * @param node | |
| 365 | * Current parent node. | |
| 366 | * @return The list of children one level below on the current parent node. | |
| 367 | */ | |
| 368 | private static List<DetailAST> getChildren(final DetailAST node) { | |
| 369 | final List<DetailAST> result = new LinkedList<>(); | |
| 370 | DetailAST curNode = node.getFirstChild(); | |
| 371 |
1
1. getChildren : negated conditional → KILLED |
while (curNode != null) { |
| 372 | result.add(curNode); | |
| 373 | curNode = curNode.getNextSibling(); | |
| 374 | } | |
| 375 |
1
1. getChildren : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck::getChildren to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return result; |
| 376 | } | |
| 377 | ||
| 378 | } | |
Mutations | ||
| 175 |
1.1 |
|
| 183 |
1.1 |
|
| 197 |
1.1 |
|
| 212 |
1.1 |
|
| 217 |
1.1 |
|
| 222 |
1.1 |
|
| 223 |
1.1 2.2 |
|
| 239 |
1.1 |
|
| 240 |
1.1 2.2 |
|
| 241 |
1.1 |
|
| 246 |
1.1 |
|
| 247 |
1.1 2.2 |
|
| 249 |
1.1 |
|
| 252 |
1.1 |
|
| 253 |
1.1 2.2 |
|
| 258 |
1.1 |
|
| 272 |
1.1 |
|
| 273 |
1.1 |
|
| 276 |
1.1 |
|
| 289 |
1.1 |
|
| 292 |
1.1 2.2 |
|
| 298 |
1.1 |
|
| 313 |
1.1 2.2 3.3 |
|
| 316 |
1.1 |
|
| 317 |
1.1 |
|
| 322 |
1.1 |
|
| 325 |
1.1 |
|
| 328 |
1.1 |
|
| 335 |
1.1 2.2 3.3 |
|
| 339 |
1.1 |
|
| 351 |
1.1 |
|
| 352 |
1.1 2.2 |
|
| 354 |
1.1 |
|
| 358 |
1.1 |
|
| 371 |
1.1 |
|
| 375 |
1.1 |