Added property to HiddenFieldCheck to ignore the parameter of

property setter methods (request 790456).
This commit is contained in:
Rick Giles 2003-08-18 12:27:55 +00:00
parent af1b996a4e
commit dc3fc5fab0
5 changed files with 168 additions and 4 deletions

View File

@ -322,6 +322,15 @@ public class MySingleton
<td><a href="property_types.html#regexp">regular expression</a></td>
<td>(not applied)</td>
</tr>
<tr>
<td>ignoreSetter</td>
<td>Controls whether to ignore the parameter of a property setter method, where
the property setter method for field &quot;xyz&quot; has name &quot;setXyz&quot;,
one parameter named &quot;xyz&quot;, and return type <span class="code">void</span>.</td>
<td><a href="property_types.html#boolean">Boolean</a></td>
<td><span class="default">false</span></td>
</tr>
</table>
<h4>Examples</h4>
<p class="body">
@ -344,6 +353,14 @@ public class MySingleton
<pre class="body">
&lt;module name=&quot;HiddenField&quot;&gt;
&lt;property name=&quot;ignoreFormat&quot; value=&quot;^rcsid$&quot;/&gt;
&lt;/module&gt;
</pre>
<p class="body">
To configure the check so that it ignores the parameter of setter methods:
</p>
<pre class="body">
&lt;module name=&quot;HiddenField&quot;&gt;
&lt;property name=&quot;ignoreSetter&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</pre>
<h4>Package</h4>

View File

@ -136,6 +136,10 @@
<li class="body">Added a check for illegal token text
(requests 740694 and 750633).</li>
<li class="body">Added property to HiddenFieldCheck to ignore the parameter of
property setter methods (request 790456).</li>
</ul>
<p class="body">

View File

@ -51,6 +51,16 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
* &lt;property name="tokens" value="VARIABLE_DEF"/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* An example of how to configure the check so that it ignores the parameter of
* a setter method is:
* </p>
* <pre>
* &lt;module name="HiddenField"&gt;
* &lt;property name="ignoreSetter" value="true"/&gt;
* &lt;/module&gt;
* </pre>
* @author Rick Giles
* @version 1.0
*/
@ -64,6 +74,9 @@ public class HiddenFieldCheck
/** the regexp to match against */
private RE mRegexp = null;
/** controls whether to check the parameter of a property setter method */
private boolean mIgnoreSetter = false;
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
@ -152,9 +165,11 @@ public class HiddenFieldCheck
final HashSet aFieldsSet = (HashSet) it.next();
if (aFieldsSet.contains(name)) {
if ((mRegexp == null) || (!getRegexp().match(name))) {
log(nameAST.getLineNo(), nameAST.getColumnNo(),
"hidden.field", name);
break;
if (!isIgnoredSetterParam(aAST, name)) {
log(nameAST.getLineNo(), nameAST.getColumnNo(),
"hidden.field", name);
break;
}
}
}
}
@ -162,6 +177,48 @@ public class HiddenFieldCheck
}
}
/**
* Decides whether to ignore an AST node that is the parameter of a
* setter method, where the property setter method for field 'xyz' has
* name 'setXyz', one parameter named 'xyz', and return type void.
* @param aAST the AST to check.
* @param aName the name of aAST.
* @return true if aAST should be ignored because check property
* ignoreSetter is true and aAST is the parameter of a setter method.
*/
private boolean isIgnoredSetterParam(DetailAST aAST, String aName)
{
if (!(aAST.getType() == TokenTypes.PARAMETER_DEF)
|| !mIgnoreSetter)
{
return false;
}
//single parameter?
final DetailAST parametersAST = aAST.getParent();
if (parametersAST.getChildCount() != 1)
{
return false;
}
//method parameter, not constructor parameter?
final DetailAST methodAST = parametersAST.getParent();
if (methodAST.getType() != TokenTypes.METHOD_DEF) {
return false;
}
//property setter name?
final String expectedName =
"set" + aName.substring(0, 1).toUpperCase() + aName.substring(1);
final DetailAST methodNameAST
= methodAST.findFirstToken(TokenTypes.IDENT);
final String methodName = methodNameAST.getText();
if (!methodName.equals(expectedName)) {
return false;
}
//void?
final DetailAST typeAST
= methodAST.findFirstToken(TokenTypes.TYPE);
return typeAST.branchContains(TokenTypes.LITERAL_VOID);
}
/**
* Set the ignore format to the specified regular expression.
* @param aFormat a <code>String</code> value
@ -178,6 +235,16 @@ public class HiddenFieldCheck
}
}
/**
* Set whether to ignore the parameter of a property setter method.
* @param aIgnoreSetter decide whether to ignore the parameter of
* a property setter method.
*/
public void setIgnoreSetter(boolean aIgnoreSetter)
{
mIgnoreSetter = aIgnoreSetter;
}
/** @return the regexp to match against */
public RE getRegexp()
{

View File

@ -90,3 +90,40 @@ interface NothingHidden
// not an error
public void noShadow(int notHidden);
}
/** tests ignoring the parameter of a property setter method */
class PropertySetter
{
private int prop;
/** setter */
public void setProp(int prop)
{
this.prop = prop;
}
/** error - incorrect method name */
public void setprop(int prop)
{
this.prop = prop;
}
/** error - more than one parameter */
public void setProp(int prop, int extra)
{
this.prop = prop;
}
}
/** tests a non-void method */
class PropertySetter2
{
private int prop;
/** error - not a void method */
public int setProp(int prop)
{
this.prop = prop;
return 0;
}
}

View File

@ -50,6 +50,10 @@ public class HiddenFieldCheckTest
"76:17: 'innerHidden' hides a field.",
"77:17: 'hidden' hides a field.",
"82:13: 'hidden' hides a field.",
"100:29: 'prop' hides a field.",
"106:29: 'prop' hides a field.",
"112:29: 'prop' hides a field.",
"124:28: 'prop' hides a field.",
};
verify(checkConfig, getPath("InputHiddenField.java"), expected);
}
@ -72,10 +76,45 @@ public class HiddenFieldCheckTest
"70:17: 'hidden' hides a field.",
"77:17: 'hidden' hides a field.",
"82:13: 'hidden' hides a field.",
"100:29: 'prop' hides a field.",
"106:29: 'prop' hides a field.",
"112:29: 'prop' hides a field.",
"124:28: 'prop' hides a field.",
};
verify(checkConfig, getPath("InputHiddenField.java"), expected);
}
/** tests ignoreSetter property */
public void testIgnoreSetter()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(HiddenFieldCheck.class);
checkConfig.addAttribute("ignoreSetter", "true");
final String[] expected = {
"18:13: 'hidden' hides a field.",
"21:33: 'hidden' hides a field.",
"27:13: 'hidden' hides a field.",
"32:18: 'hidden' hides a field.",
"36:33: 'hidden' hides a field.",
"46:17: 'innerHidden' hides a field.",
"49:26: 'innerHidden' hides a field.",
"55:17: 'innerHidden' hides a field.",
"56:17: 'hidden' hides a field.",
"61:22: 'innerHidden' hides a field.",
"64:22: 'hidden' hides a field.",
"69:17: 'innerHidden' hides a field.",
"70:17: 'hidden' hides a field.",
"76:17: 'innerHidden' hides a field.",
"77:17: 'hidden' hides a field.",
"82:13: 'hidden' hides a field.",
"106:29: 'prop' hides a field.",
"112:29: 'prop' hides a field.",
"124:28: 'prop' hides a field.",
};
verify(checkConfig, getPath("InputHiddenField.java"), expected);
}
/** Test against a class with field declarations in different order */
public void testReordered()
throws Exception