Skip to content

Commit

Permalink
Issue #7641: Adding code examples for HiddenField check
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniiit002 authored and romani committed Feb 13, 2021
1 parent c312f75 commit cbe3d7b
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@
* <pre>
* &lt;module name=&quot;HiddenField&quot;/&gt;
* </pre>
* <pre>
* public class SomeClass {
*
* private String field;
* private String testField;
*
* public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // violation, 'testField' param
* // hides 'testField' field
* this.field = field;
* }
* public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
* this.field = field;
* }
* }
* </pre>
*
* <p>
* To configure the check so that it checks local variables but not parameters:
Expand All @@ -134,29 +154,50 @@
* &lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* public class SomeClass {
*
* private String field;
* private String testField;
*
* public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
* this.field = field;
* }
* public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field
* this.field = field;
* }
* }
* </pre>
*
* <p>
* To configure the check so that it ignores the variables and parameters named "test":
* </p>
* <pre>
* &lt;module name=&quot;HiddenField&quot;&gt;
* &lt;property name=&quot;ignoreFormat&quot; value=&quot;^test$&quot;/&gt;
* &lt;property name=&quot;ignoreFormat&quot; value=&quot;^testField&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* class SomeClass
* {
* private List&lt;String&gt; test;
* public class SomeClass {
*
* private void addTest(List&lt;String&gt; test) // no violation
* {
* this.test.addAll(test);
* }
* private String field;
* private String testField;
*
* private void foo()
* {
* final List&lt;String&gt; test = new ArrayList&lt;&gt;(); // no violation
* ...
* public SomeClass(String testField) { // OK, because it match ignoreFormat
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // OK, because it match ignoreFormat
* this.field = field;
* }
* public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
* this.field = field;
* }
* }
* </pre>
Expand All @@ -168,6 +209,26 @@
* &lt;property name=&quot;ignoreConstructorParameter&quot; value=&quot;true&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* public class SomeClass {
*
* private String field;
* private String testField;
*
* public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // violation, 'testField' variable
* // hides 'testField' field
* this.field = field;
* }
* public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
* this.field = field;
* }
* }
* </pre>
* <p>
* To configure the check so that it ignores the parameter of setter methods:
* </p>
Expand All @@ -176,6 +237,25 @@
* &lt;property name=&quot;ignoreSetter&quot; value=&quot;true&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* public class SomeClass {
*
* private String field;
* private String testField;
*
* public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
* this.field = field;
* }
* public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
* this.field = field;
* }
* }
* </pre>
* <p>
* To configure the check so that it ignores the parameter of setter methods
* recognizing setter as returning either {@code void} or a class in which it is declared:
Expand All @@ -186,6 +266,51 @@
* &lt;property name=&quot;setterCanReturnItsClass&quot; value=&quot;true&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* public class SomeClass {
*
* private String field;
* private String testField;
*
* public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
* }
* public void method(String param) { // OK
* String field = param; // violation, 'field' variable hides 'field' field
* }
* public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
* this.field = field;
* }
* public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field
* this.field = field;
* }
* }
* </pre>
* <p>
* To configure the check so that it ignores parameters of abstract methods:
* </p>
* <pre>
* &lt;module name=&quot;HiddenField&quot;&gt;
* &lt;property name=&quot;ignoreAbstractMethods&quot; value=&quot;true&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* abstract class SomeClass {
*
* private String field;
*
* public SomeClass(int field) { // violation, 'field' param hides a 'field' field
* float field; // violation, 'field' variable hides a 'field' field
* }
* public abstract int method(String field); // OK
* }
*
* public class Demo extends SomeClass {
*
* public int method(String param){
* return param;
* }
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
Expand Down
151 changes: 138 additions & 13 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,26 @@ class PageBuilder {
<source>
&lt;module name=&quot;HiddenField&quot;/&gt;
</source>
<source>
public class SomeClass {

private String field;
private String testField;

public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // violation, 'testField' param
// hides 'testField' field
this.field = field;
}
public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
this.field = field;
}
}
</source>

<p>
To configure the check so that it checks local variables but not
Expand All @@ -2013,31 +2033,52 @@ class PageBuilder {
&lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
&lt;/module&gt;
</source>
<source>
public class SomeClass {

private String field;
private String testField;

public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
this.field = field;
}
public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field
this.field = field;
}
}
</source>

<p>
To configure the check so that it ignores the variables and parameters named
&quot;test&quot;:
</p>
<source>
&lt;module name=&quot;HiddenField&quot;&gt;
&lt;property name=&quot;ignoreFormat&quot; value=&quot;^test$&quot;/&gt;
&lt;property name=&quot;ignoreFormat&quot; value=&quot;^testField&quot;/&gt;
&lt;/module&gt;
</source>

<source>
class SomeClass
{
private List&lt;String&gt; test;
public class SomeClass {

private void addTest(List&lt;String&gt; test) // no violation
{
this.test.addAll(test);
}
private String field;
private String testField;

private void foo()
{
final List&lt;String&gt; test = new ArrayList&lt;&gt;(); // no violation
...
public SomeClass(String testField) { // OK, because it match ignoreFormat
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // OK, because it match ignoreFormat
this.field = field;
}
public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
this.field = field;
}
}
</source>
Expand All @@ -2050,6 +2091,26 @@ class SomeClass
&lt;property name=&quot;ignoreConstructorParameter&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
<source>
public class SomeClass {

private String field;
private String testField;

public SomeClass(String testField) { // OK, 'testField' param doesn't hide any field
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // violation, 'testField' variable
// hides 'testField' field
this.field = field;
}
public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
this.field = field;
}
}
</source>

<p>
To configure the check so that it ignores the parameter of setter
Expand All @@ -2060,18 +2121,82 @@ class SomeClass
&lt;property name=&quot;ignoreSetter&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
<source>
public class SomeClass {

private String field;
private String testField;

public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
this.field = field;
}
public SomeClass setField(String field) { // violation, 'field' param hides 'field' field
this.field = field;
}
}
</source>

<p>
To configure the check so that it ignores the parameter of setter
methods recognizing setter as returning either <code>void</code> or
a class in which it is declared:
a class in which it is declared:
</p>
<source>
&lt;module name=&quot;HiddenField&quot;&gt;
&lt;property name=&quot;ignoreSetter&quot; value=&quot;true&quot;/&gt;
&lt;property name=&quot;setterCanReturnItsClass&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
<source>
public class SomeClass {

private String field;
private String testField;

public SomeClass(String testField) { // violation, 'testField' param hides 'testField' field
}
public void method(String param) { // OK
String field = param; // violation, 'field' variable hides 'field' field
}
public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field
this.field = field;
}
public SomeClass setField(String field) { // OK, 'field' param doesn't hide any field
this.field = field;
}
}
</source>
<p>
To configure the check so that it ignores parameters of abstract methods:
</p>
<source>
&lt;module name=&quot;HiddenField&quot;&gt;
&lt;property name=&quot;ignoreAbstractMethods&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
<source>
abstract class SomeClass {

private String field;

public SomeClass(int field) { // violation, 'field' param hides a 'field' field
float field; // violation, 'field' variable hides a 'field' field
}
public abstract int method(String field); // OK
}

public class Demo extends SomeClass {

public int method(String param){
return param;
}
}
</source>
</subsection>

<subsection name="Example of Usage" id="HiddenField_Example_of_Usage">
Expand Down

0 comments on commit cbe3d7b

Please sign in to comment.