Skip to content

Commit

Permalink
Issue checkstyle#8930: Example to show how to target Check to fields …
Browse files Browse the repository at this point in the history
…only
  • Loading branch information
aryaniiit002 authored and romani committed Feb 12, 2021
1 parent 7c7958a commit 5bf4e04
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,39 @@
* }
* </pre>
* <p>
* To configure the check to target fields types only:
* </p>
* <pre>
* &lt;module name="IllegalType"&gt;
* &lt;property name=&quot;illegalClassNames&quot; value=&quot;java.util.Optional&quot;/&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
* &lt;property name=&quot;id&quot; value=&quot;IllegalTypeOptionalAsField&quot;/&gt;
* &lt;/module&gt;
* &lt;module name="SuppressionXpathSingleFilter"&gt;
* &lt;property name=&quot;query&quot; value=&quot;//METHOD_DEF//*&quot;/&gt;
* &lt;property name=&quot;id&quot; value=&quot;IllegalTypeOptionalAsField&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* import java.util.Optional;
*
* public class Main {
*
* static int field1 = 4; // OK
* public Optional&lt;String&gt; field2; // violation, usage of type 'Optional' is not allowed
* protected String field3; // OK
* Optional&lt;String&gt; field4; // violation, usage of type 'Optional' is not allowed
* private Optional&lt;String&gt; field5; // violation, usage of type 'Optional' is not allowed
*
* void foo() {
* Optional&lt;String&gt; i; // OK
* }
* public &lt;T extends java.util.Optional&gt; void method(T t) { // OK
* Optional&lt;T&gt; i; // OK
* }
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
Expand Down
33 changes: 33 additions & 0 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,39 @@ public class Test {
return new TreeSet&lt;Foo&lt;Boolean&gt;&gt;();
}

}
</source>
<p>
To configure the check to target fields types only:
</p>
<source>
&lt;module name="IllegalType"&gt;
&lt;property name=&quot;illegalClassNames&quot; value=&quot;java.util.Optional&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
&lt;property name=&quot;id&quot; value=&quot;IllegalTypeOptionalAsField&quot;/&gt;
&lt;/module&gt;
&lt;module name="SuppressionXpathSingleFilter"&gt;
&lt;property name=&quot;query&quot; value=&quot;//METHOD_DEF//*&quot;/&gt;
&lt;property name=&quot;id&quot; value=&quot;IllegalTypeOptionalAsField&quot;/&gt;
&lt;/module&gt;
</source>
<source>
import java.util.Optional;

public class Main {

static int field1 = 4; // OK
public Optional&lt;String&gt; field2; // violation, usage of type 'Optional' is not allowed
protected String field3; // OK
Optional&lt;String&gt; field4; // violation, usage of type 'Optional' is not allowed
private Optional&lt;String&gt; field5; // violation, usage of type 'Optional' is not allowed

void foo() {
Optional&lt;String&gt; i; // OK
}
public &lt;T extends java.util.Optional&gt; void method(T t) { // OK
Optional&lt;T&gt; i; // OK
}
}
</source>
</subsection>
Expand Down

0 comments on commit 5bf4e04

Please sign in to comment.