Fine tune the patch.
This commit is contained in:
parent
bd757c37dd
commit
73ec35d35b
|
|
@ -22,9 +22,7 @@ import com.puppycrawl.tools.checkstyle.api.Check;
|
|||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
|
|
@ -84,63 +82,12 @@ public class MagicNumberCheck extends Check
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not the given AST is in a valid hash code method.
|
||||
* A valid hash code method is considered to be a method of the signature
|
||||
* {@code public int hashCode()}.
|
||||
*
|
||||
* @param aAST the AST from which to search for an enclosing hash code
|
||||
* method definition
|
||||
*
|
||||
* @return {@code true} if {@code aAST} is in the scope of a valid hash
|
||||
* code method
|
||||
*/
|
||||
private boolean isInHashCodeMethod(DetailAST aAST)
|
||||
{
|
||||
// if not in a code block, can't be in hashCode()
|
||||
if (!ScopeUtils.inCodeBlock(aAST)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// find the method definition AST
|
||||
DetailAST methodDefAST = aAST.getParent();
|
||||
while (methodDefAST != null
|
||||
&& methodDefAST.getType() != TokenTypes.METHOD_DEF)
|
||||
{
|
||||
methodDefAST = methodDefAST.getParent();
|
||||
}
|
||||
|
||||
if (methodDefAST == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// chech for 'hashCode' name
|
||||
final DetailAST identAST =
|
||||
methodDefAST.findFirstToken(TokenTypes.IDENT);
|
||||
System.out.println(identAST);
|
||||
if (!"hashCode".equals(identAST.getText())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check for no arguments
|
||||
final DetailAST paramAST =
|
||||
methodDefAST.findFirstToken(TokenTypes.PARAMETERS);
|
||||
if (paramAST.getChildCount() != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// we are in a 'public int hashCode()' method!
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
{
|
||||
if (inIgnoreList(aAST)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mIgnoreHashCodeMethod && isInHashCodeMethod(aAST)) {
|
||||
if (inIgnoreList(aAST)
|
||||
|| (mIgnoreHashCodeMethod && isInHashCodeMethod(aAST)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -224,6 +171,55 @@ public class MagicNumberCheck extends Check
|
|||
text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not the given AST is in a valid hash code method.
|
||||
* A valid hash code method is considered to be a method of the signature
|
||||
* {@code public int hashCode()}.
|
||||
*
|
||||
* @param aAST the AST from which to search for an enclosing hash code
|
||||
* method definition
|
||||
*
|
||||
* @return {@code true} if {@code aAST} is in the scope of a valid hash
|
||||
* code method
|
||||
*/
|
||||
private boolean isInHashCodeMethod(DetailAST aAST)
|
||||
{
|
||||
// if not in a code block, can't be in hashCode()
|
||||
if (!ScopeUtils.inCodeBlock(aAST)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// find the method definition AST
|
||||
DetailAST methodDefAST = aAST.getParent();
|
||||
while ((null != methodDefAST)
|
||||
&& (TokenTypes.METHOD_DEF != methodDefAST.getType()))
|
||||
{
|
||||
methodDefAST = methodDefAST.getParent();
|
||||
}
|
||||
|
||||
if (null == methodDefAST) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for 'hashCode' name.
|
||||
final DetailAST identAST =
|
||||
methodDefAST.findFirstToken(TokenTypes.IDENT);
|
||||
if (!"hashCode".equals(identAST.getText())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for no arguments.
|
||||
final DetailAST paramAST =
|
||||
methodDefAST.findFirstToken(TokenTypes.PARAMETERS);
|
||||
if (0 != paramAST.getChildCount()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// we are in a 'public int hashCode()' method! The compiler will ensure
|
||||
// the method returns an 'int' and is public.
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides whether the number of an AST is in the ignore list of this
|
||||
* check.
|
||||
|
|
|
|||
|
|
@ -44,6 +44,12 @@
|
|||
that checks there is only one statement per line. Thanks to
|
||||
Alexander Jesse for patch #1144994, which was the inspiration.
|
||||
</li>
|
||||
<li>
|
||||
Enhanced <a href="config_coding.html#MagicNumber">MagicNumber</a>
|
||||
to support the parameter <code>ignoreHashCodeMethod</code> to ignore
|
||||
magic numbers in <code>hashCode()</code> methods. Thanks to
|
||||
Daniel Solano Gómez for patch #3050788.
|
||||
</li>
|
||||
</ul>
|
||||
<p>Bug fixes:</p>
|
||||
<ul>
|
||||
|
|
|
|||
Loading…
Reference in New Issue