| 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.coding; | |
| 21 | ||
| 22 | import com.puppycrawl.tools.checkstyle.FileStatefulCheck; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 25 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 26 | import com.puppycrawl.tools.checkstyle.utils.CheckUtils; | |
| 27 | ||
| 28 | /** | |
| 29 | * Restricts nested if-else blocks to a specified depth (default = 1). | |
| 30 | * | |
| 31 | * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> | |
| 32 | */ | |
| 33 | @FileStatefulCheck | |
| 34 | public final class NestedIfDepthCheck extends AbstractCheck { | |
| 35 | ||
| 36 | /** | |
| 37 | * A key is pointing to the warning message text in "messages.properties" | |
| 38 | * file. | |
| 39 | */ | |
| 40 | public static final String MSG_KEY = "nested.if.depth"; | |
| 41 | ||
| 42 | /** Maximum allowed nesting depth. */ | |
| 43 | private int max = 1; | |
| 44 | /** Current nesting depth. */ | |
| 45 | private int depth; | |
| 46 | ||
| 47 | /** | |
| 48 | * Setter for maximum allowed nesting depth. | |
| 49 | * @param max maximum allowed nesting depth. | |
| 50 | */ | |
| 51 | public void setMax(int max) { | |
| 52 | this.max = max; | |
| 53 | } | |
| 54 | ||
| 55 | @Override | |
| 56 | public int[] getDefaultTokens() { | |
| 57 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedIfDepthCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public int[] getAcceptableTokens() { | |
| 62 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedIfDepthCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return getRequiredTokens(); |
| 63 | } | |
| 64 | ||
| 65 | @Override | |
| 66 | public int[] getRequiredTokens() { | |
| 67 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/NestedIfDepthCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] {TokenTypes.LITERAL_IF}; |
| 68 | } | |
| 69 | ||
| 70 | @Override | |
| 71 | public void beginTree(DetailAST rootAST) { | |
| 72 | depth = 0; | |
| 73 | } | |
| 74 | ||
| 75 | @Override | |
| 76 | public void visitToken(DetailAST literalIf) { | |
| 77 |
1
1. visitToken : negated conditional → KILLED |
if (!CheckUtils.isElseIf(literalIf)) { |
| 78 |
2
1. visitToken : changed conditional boundary → KILLED 2. visitToken : negated conditional → KILLED |
if (depth > max) { |
| 79 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/NestedIfDepthCheck::log → KILLED |
log(literalIf, MSG_KEY, depth, max); |
| 80 | } | |
| 81 |
1
1. visitToken : Replaced integer addition with subtraction → KILLED |
++depth; |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | @Override | |
| 86 | public void leaveToken(DetailAST literalIf) { | |
| 87 |
1
1. leaveToken : negated conditional → KILLED |
if (!CheckUtils.isElseIf(literalIf)) { |
| 88 |
1
1. leaveToken : Replaced integer subtraction with addition → KILLED |
--depth; |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | } | |
Mutations | ||
| 57 |
1.1 |
|
| 62 |
1.1 |
|
| 67 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 2.2 |
|
| 79 |
1.1 |
|
| 81 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |