Issue #1293: Refactoring of EqualsAvoidNullCheck. More UTs.
This commit is contained in:
parent
f747835b6b
commit
6ebcf2733b
1
pom.xml
1
pom.xml
|
|
@ -1148,7 +1148,6 @@
|
|||
<regex><pattern>.*.checks.coding.CovariantEqualsCheck</pattern><branchRate>95</branchRate><lineRate>96</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.DeclarationOrderCheck</pattern><branchRate>82</branchRate><lineRate>93</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.DefaultComesLastCheck</pattern><branchRate>87</branchRate><lineRate>100</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.EqualsAvoidNullCheck</pattern><branchRate>79</branchRate><lineRate>100</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.ExplicitInitializationCheck</pattern><branchRate>91</branchRate><lineRate>97</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.FallThroughCheck</pattern><branchRate>90</branchRate><lineRate>97</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.FinalLocalVariableCheck</pattern><branchRate>82</branchRate><lineRate>100</lineRate></regex>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import antlr.collections.AST;
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
|
@ -110,7 +109,12 @@ public class EqualsAvoidNullCheck extends Check {
|
|||
|
||||
@Override
|
||||
public int[] getAcceptableTokens() {
|
||||
return new int[] {TokenTypes.METHOD_CALL};
|
||||
return getDefaultTokens();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
return getDefaultTokens();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -130,19 +134,30 @@ public class EqualsAvoidNullCheck extends Check {
|
|||
final DetailAST expr = dot.getNextSibling().getFirstChild();
|
||||
|
||||
if ("equals".equals(method.getText())
|
||||
&& containsOneArg(expr) && containsAllSafeTokens(expr)) {
|
||||
&& containsOneArgument(methodCall) && containsAllSafeTokens(expr)) {
|
||||
log(methodCall.getLineNo(), methodCall.getColumnNo(),
|
||||
MSG_EQUALS_AVOID_NULL);
|
||||
}
|
||||
|
||||
if (!ignoreEqualsIgnoreCase
|
||||
&& "equalsIgnoreCase".equals(method.getText())
|
||||
&& containsOneArg(expr) && containsAllSafeTokens(expr)) {
|
||||
&& containsOneArgument(methodCall) && containsAllSafeTokens(expr)) {
|
||||
log(methodCall.getLineNo(), methodCall.getColumnNo(),
|
||||
MSG_EQUALS_IGNORE_CASE_AVOID_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that method call has one argument.
|
||||
*
|
||||
* @param methodCall METHOD_CALL DetailAST
|
||||
* @return true if method call has one argument.
|
||||
*/
|
||||
private static boolean containsOneArgument(DetailAST methodCall) {
|
||||
final DetailAST elist = methodCall.findFirstToken(TokenTypes.ELIST);
|
||||
return elist.getChildCount() == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks for calling equals on String literal and
|
||||
* anon object which cannot be null
|
||||
|
|
@ -158,40 +173,6 @@ public class EqualsAvoidNullCheck extends Check {
|
|||
|| objCalledOn.getType() == TokenTypes.DOT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a method contains no arguments
|
||||
* starting at with the argument expression.
|
||||
*
|
||||
* @param expr the argument expression
|
||||
* @return true if the method contains no args, false if not
|
||||
*/
|
||||
private boolean containsNoArgs(final AST expr) {
|
||||
return expr == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a method contains multiple arguments
|
||||
* starting at with the argument expression.
|
||||
*
|
||||
* @param expr the argument expression
|
||||
* @return true if the method contains multiple args, false if not
|
||||
*/
|
||||
private boolean containsMultiArgs(final AST expr) {
|
||||
final AST comma = expr.getNextSibling();
|
||||
return comma != null && comma.getType() == TokenTypes.COMMA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a method contains a single argument
|
||||
* starting at with the argument expression.
|
||||
*
|
||||
* @param expr the argument expression
|
||||
* @return true if the method contains a single arg, false if not
|
||||
*/
|
||||
private boolean containsOneArg(final AST expr) {
|
||||
return !containsNoArgs(expr) && !containsMultiArgs(expr);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Looks for all "safe" Token combinations in the argument
|
||||
|
|
|
|||
|
|
@ -62,6 +62,8 @@ public class EqualsAvoidNullCheckTest extends BaseCheckTestSupport {
|
|||
"75:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
|
||||
"77:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
|
||||
"79:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
|
||||
"225:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
|
||||
"227:34: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
|
||||
};
|
||||
verify(checkConfig, getPath("coding" + File.separator + "InputEqualsAvoidNull.java"), expected);
|
||||
}
|
||||
|
|
@ -85,6 +87,7 @@ public class EqualsAvoidNullCheckTest extends BaseCheckTestSupport {
|
|||
"63:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
|
||||
"65:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
|
||||
"67:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
|
||||
"225:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
|
||||
};
|
||||
verify(checkConfig, getPath("coding" + File.separator + "InputEqualsAvoidNull.java"), expected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,15 +188,43 @@ public class InputEqualsAvoidNull {
|
|||
|
||||
new String().equalsIgnoreCase("more cheese");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InputEqualsAvoidNullOutter {
|
||||
public class InputEqualsAvoidNullInner {
|
||||
public boolean equals(Object o) {
|
||||
return true;
|
||||
}
|
||||
public boolean equals(Object o) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyString {
|
||||
public boolean equals() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equals(String s1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equalsIgnoreCase() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equalsIgnoreCase(String s1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private String pizza;
|
||||
|
||||
public void main() {
|
||||
MyString myString = new MyString();
|
||||
myString.equals();
|
||||
myString.equals("what");
|
||||
myString.equalsIgnoreCase();
|
||||
myString.equalsIgnoreCase("what");
|
||||
myString.equals(this.pizza = "cold pizza");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue