Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull #565: added new check MoveVariableInsideIfCheck
- Loading branch information
Showing
11 changed files
with
575 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 275 additions & 0 deletions
275
...s/src/main/java/com/github/sevntu/checkstyle/checks/coding/MoveVariableInsideIfCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 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.github.sevntu.checkstyle.checks.coding; | ||
|
||
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.ScopeUtils; | ||
|
||
/** | ||
* <p> | ||
* Checks if a variable is only used inside if statements and asks for it's | ||
* declaration to be moved there too. | ||
* </p> | ||
* | ||
* @author Richard Veach | ||
*/ | ||
public class MoveVariableInsideIfCheck extends AbstractCheck { | ||
/** | ||
* A key is pointing to the warning message text in "messages.properties" | ||
* file. | ||
*/ | ||
public static final String MSG_KEY = "move.variable.inside"; | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return new int[] {TokenTypes.VARIABLE_DEF}; | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return getDefaultTokens(); | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
if (ScopeUtils.isLocalVariableDef(ast)) { | ||
validateLocalVariable(ast); | ||
} | ||
} | ||
|
||
/** | ||
* Examines the local variable for violations to be moved inside an nest if | ||
* statement. | ||
* | ||
* @param ast The local variable to examine. | ||
*/ | ||
private void validateLocalVariable(DetailAST ast) { | ||
final Holder holder = new Holder(ast); | ||
|
||
for (DetailAST child = ast.getNextSibling(); (!holder.exit) && (child != null); | ||
child = child.getNextSibling()) { | ||
switch (child.getType()) { | ||
case TokenTypes.LITERAL_IF: | ||
validateIf(holder, child); | ||
break; | ||
default: | ||
validateOther(holder, child); | ||
break; | ||
} | ||
} | ||
|
||
if (holder.blockNode != null) { | ||
log(ast, MSG_KEY, holder.variableName, holder.blockNode.getLineNo()); | ||
} | ||
} | ||
|
||
/** | ||
* Examines an if statement to see how many times the specified variable | ||
* identifier was used inside it. | ||
* | ||
* @param holder The object holder with the specified variable to check and | ||
* it's current state. | ||
* @param ifNodeGiven The current if node to examine. | ||
*/ | ||
private static void validateIf(Holder holder, DetailAST ifNodeGiven) { | ||
DetailAST ifNode = ifNodeGiven; | ||
|
||
// -@cs[SingleBreakOrContinue] Too complex to break apart | ||
while (true) { | ||
// check condition | ||
final DetailAST rparen = ifNode.findFirstToken(TokenTypes.RPAREN); | ||
final boolean usedInCondition = holder.hasIdent( | ||
ifNode.findFirstToken(TokenTypes.LPAREN), rparen); | ||
|
||
if (usedInCondition) { | ||
holder.setExit(); | ||
break; | ||
} | ||
|
||
final DetailAST elseNode = ifNode.getLastChild(); | ||
|
||
// check body of if | ||
final DetailAST body = rparen.getNextSibling(); | ||
final DetailAST bodyEnd; | ||
|
||
if (body.getType() == TokenTypes.SLIST) { | ||
bodyEnd = body.getLastChild(); | ||
} | ||
else { | ||
bodyEnd = elseNode; | ||
} | ||
|
||
final boolean used = holder.hasIdent(body, bodyEnd); | ||
|
||
if (used) { | ||
holder.setBlockNode(ifNode); | ||
|
||
if (holder.exit) { | ||
break; | ||
} | ||
} | ||
|
||
if (elseNode.getType() != TokenTypes.LITERAL_ELSE) { | ||
break; | ||
} | ||
|
||
ifNode = elseNode.getFirstChild(); | ||
|
||
if (ifNode.getType() != TokenTypes.LITERAL_IF) { | ||
// check body of else | ||
|
||
validateElseOfIf(holder, ifNode, elseNode); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Examines the else of an if statement to see how many times the specified | ||
* variable identifier was used inside it. | ||
* | ||
* @param holder The object holder with the specified variable to check and | ||
* it's current state. | ||
* @param ifNode The if node of the specified else. | ||
* @param elseNode The current else node to examine. | ||
*/ | ||
private static void validateElseOfIf(Holder holder, DetailAST ifNode, DetailAST elseNode) { | ||
final boolean used; | ||
|
||
if (ifNode.getType() == TokenTypes.SLIST) { | ||
used = holder.hasIdent(ifNode.getFirstChild(), ifNode.getLastChild()); | ||
} | ||
else { | ||
used = holder.hasIdent(ifNode, elseNode.getLastChild()); | ||
} | ||
|
||
if (used) { | ||
holder.setBlockNode(elseNode); | ||
} | ||
} | ||
|
||
/** | ||
* Examines other nodes to see how many times a variable was used inside it. | ||
* If the variable is used, no violations are reported for it. | ||
* | ||
* @param holder The object holder with the specified variable to check and | ||
* it's current state. | ||
* @param child The current node to examine. | ||
*/ | ||
private static void validateOther(Holder holder, DetailAST child) { | ||
final boolean used = holder.hasIdent(child, child.getNextSibling()); | ||
|
||
if (used) { | ||
holder.setExit(); | ||
} | ||
} | ||
|
||
/** | ||
* The holder of information for the specified variable. | ||
* | ||
* @author Richard Veach | ||
*/ | ||
private static class Holder { | ||
/** The name of the variable being examined. */ | ||
private String variableName; | ||
/** Switch to trigger ending examining more nodes. */ | ||
private boolean exit; | ||
/** The node to report violations on. */ | ||
private DetailAST blockNode; | ||
|
||
/** | ||
* Default constructor for the class. | ||
* | ||
* @param ast The variable the holder is for. | ||
*/ | ||
Holder(DetailAST ast) { | ||
variableName = ast.findFirstToken(TokenTypes.IDENT).getText(); | ||
} | ||
|
||
/** | ||
* Sets the specified node that is to be reported for the violation for | ||
* the block. If there is already a node being reported, then no nodes | ||
* are reported. | ||
* | ||
* @param blockNode The given block node to report for. | ||
*/ | ||
public void setBlockNode(DetailAST blockNode) { | ||
if (this.blockNode != null) { | ||
setExit(); | ||
} | ||
else { | ||
this.blockNode = blockNode; | ||
} | ||
} | ||
|
||
/** Sets the state to exit examining further nodes. */ | ||
public void setExit() { | ||
blockNode = null; | ||
exit = true; | ||
} | ||
|
||
/** | ||
* Checks if any of the nodes between the given start and end are an | ||
* identifier with the name of the variable. | ||
* | ||
* @param start The node to start examining from. | ||
* @param end The last node to stop examining once reached. If null, | ||
* then the last node is when we leave the start node. | ||
* @return true if the identifier has been found, otherwise false. | ||
*/ | ||
public boolean hasIdent(DetailAST start, DetailAST end) { | ||
boolean found = false; | ||
DetailAST curNode = start; | ||
|
||
// -@cs[SingleBreakOrContinue] Too complex to break apart | ||
while (curNode != null) { | ||
if ((curNode.getType() == TokenTypes.IDENT) | ||
&& (variableName.equals(curNode.getText()))) { | ||
found = true; | ||
break; | ||
} | ||
|
||
if (curNode == end) { | ||
break; | ||
} | ||
|
||
DetailAST toVisit = curNode.getFirstChild(); | ||
|
||
while (curNode != null && toVisit == null) { | ||
toVisit = curNode.getNextSibling(); | ||
|
||
if (toVisit == null) { | ||
if ((end == null) && (curNode == start)) { | ||
break; | ||
} | ||
|
||
curNode = curNode.getParent(); | ||
} | ||
} | ||
|
||
curNode = toVisit; | ||
} | ||
|
||
return found; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...c/test/java/com/github/sevntu/checkstyle/checks/coding/MoveVariableInsideIfCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 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.github.sevntu.checkstyle.checks.coding; | ||
|
||
import static com.github.sevntu.checkstyle.checks.coding.MoveVariableInsideIfCheck.MSG_KEY; | ||
|
||
import org.junit.Test; | ||
|
||
import com.github.sevntu.checkstyle.BaseCheckTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | ||
|
||
public class MoveVariableInsideIfCheckTest extends BaseCheckTestSupport { | ||
@Test | ||
public final void testNoViolations() throws Exception { | ||
final DefaultConfiguration checkConfig = createCheckConfig(MoveVariableInsideIfCheck.class); | ||
verify(checkConfig, getPath("InputMoveVariableInsideIfCheckNoViolations.java"), | ||
CommonUtils.EMPTY_STRING_ARRAY); | ||
} | ||
|
||
@Test | ||
public final void testViolations() throws Exception { | ||
final DefaultConfiguration checkConfig = createCheckConfig(MoveVariableInsideIfCheck.class); | ||
final String[] expected = { | ||
"5:9: " + getCheckMessage(MSG_KEY, "variable", "7"), | ||
"13:9: " + getCheckMessage(MSG_KEY, "variable", "15"), | ||
"24:9: " + getCheckMessage(MSG_KEY, "variable", "26"), | ||
"33:9: " + getCheckMessage(MSG_KEY, "variable", "38"), | ||
"44:9: " + getCheckMessage(MSG_KEY, "variable", "48"), | ||
}; | ||
verify(checkConfig, getPath("InputMoveVariableInsideIfCheckViolations.java"), expected); | ||
} | ||
|
||
@Test | ||
public final void testFalsePositives() throws Exception { | ||
final DefaultConfiguration checkConfig = createCheckConfig(MoveVariableInsideIfCheck.class); | ||
final String[] expected = { | ||
"9:9: " + getCheckMessage(MSG_KEY, "variable", "12"), | ||
"18:9: " + getCheckMessage(MSG_KEY, "variable", "24"), | ||
"30:9: " + getCheckMessage(MSG_KEY, "variable", "34"), | ||
}; | ||
verify(checkConfig, getPath("InputMoveVariableInsideIfCheckFalsePositives.java"), | ||
expected); | ||
} | ||
} |
Oops, something went wrong.