Skip to content

Commit

Permalink
Issue checkstyle#3955: corrected tokens so all are required
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Sep 20, 2022
1 parent 1194536 commit d6d443b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 122 deletions.
Expand Up @@ -60,14 +60,6 @@
* Type is {@code java.lang.String[]}.
* Default value is {@code ""}.
* </li>
* <li>
* Property {@code tokens} - tokens to check
* Type is {@code java.lang.String[]}.
* Validation type is {@code tokenSet}.
* Default value is:
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
* CLASS_DEF</a>.
* </li>
* </ul>
* <p>
* To configure the check:
Expand Down Expand Up @@ -95,12 +87,7 @@
* </pre>
* <p>
* To configure the check to find instantiations of {@code java.lang.Boolean}
* and {@code java.lang.Integer}. NOTE: Even if property {@code tokens}
* is completely removed from the following configuration, Checkstyle will produce
* the same results for violation. This is because if property {@code tokens} is not
* defined in the configuration, Checkstyle will supply it with list of default tokens
* {@code CLASS_DEF, LITERAL_NEW, PACKAGE_DEF, IMPORT} for this check. The property is
* defined in this example only to provide clarity:
* and {@code java.lang.Integer}:
* </p>
* <pre>
* &lt;module name=&quot;IllegalInstantiation&quot;&gt;
Expand Down Expand Up @@ -131,41 +118,6 @@
* }
* </pre>
* <p>
* To configure the check to allow violations for local classes vs classes
* defined in the check, for example {@code java.lang.Boolean}, property
* {@code tokens} must be defined to not mention {@code CLASS_DEF}, so its
* value should be {@code LITERAL_NEW, PACKAGE_DEF, IMPORT}:
* </p>
* <pre>
* &lt;module name=&quot;IllegalInstantiation&quot;&gt;
* &lt;property name=&quot;classes&quot; value=&quot;java.lang.Boolean,
* java.lang.Integer&quot;/&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_NEW, PACKAGE_DEF,
* IMPORT&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
* <pre>
* public class MyTest {
* public class Boolean {
* boolean a;
*
* public Boolean (boolean a) { this.a = a; }
* }
*
* public void myTest (boolean a, int b) {
* Boolean c = new Boolean(a); // violation, instantiation of
* // java.lang.Boolean should be avoided
* java.lang.Boolean d = new java.lang.Boolean(a); // violation, instantiation of
* // java.lang.Boolean should be avoided
*
* Integer e = new Integer(b); // violation, instantiation of
* // java.lang.Integer should be avoided
* Integer f = Integer.valueOf(b); // OK
* }
* }
* </pre>
* <p>
* Finally, there is a limitation that it is currently not possible to specify array classes:
* </p>
* <pre>
Expand Down Expand Up @@ -227,17 +179,12 @@ public class IllegalInstantiationCheck

@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
return getRequiredTokens();
}

@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.IMPORT,
TokenTypes.LITERAL_NEW,
TokenTypes.PACKAGE_DEF,
TokenTypes.CLASS_DEF,
};
return getRequiredTokens();
}

@Override
Expand All @@ -246,6 +193,7 @@ public int[] getRequiredTokens() {
TokenTypes.IMPORT,
TokenTypes.LITERAL_NEW,
TokenTypes.PACKAGE_DEF,
TokenTypes.CLASS_DEF,
};
}

Expand Down
Expand Up @@ -30,12 +30,6 @@
<property default-value="" name="classes" type="java.lang.String[]">
<description>Specify fully qualified class names that should not be instantiated.</description>
</property>
<property default-value="CLASS_DEF"
name="tokens"
type="java.lang.String[]"
validation-type="tokenSet">
<description>tokens to check</description>
</property>
</properties>
<message-keys>
<message-key key="instantiation.avoid"/>
Expand Down
61 changes: 1 addition & 60 deletions src/xdocs/config_coding.xml
Expand Up @@ -2465,25 +2465,6 @@ try {
<td><code>{}</code></td>
<td>3.0</td>
</tr>

<tr>
<td>tokens</td>
<td>tokens to check</td>

<td>
subset of tokens
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
CLASS_DEF</a>
.
</td>

<td>
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
CLASS_DEF</a>
.
</td>
<td>3.0</td>
</tr>
</table>
</div>
</subsection>
Expand Down Expand Up @@ -2515,12 +2496,7 @@ public class MyTest {
</source>
<p>
To configure the check to find instantiations of <code>java.lang.Boolean</code>
and <code>java.lang.Integer</code>. NOTE: Even if property <code>tokens</code>
is completely removed from the following configuration, Checkstyle will produce the
same results for violation. This is because if property <code>tokens</code> is not
defined in the configuration, Checkstyle will supply it with list of default tokens
<code>CLASS_DEF, LITERAL_NEW, PACKAGE_DEF, IMPORT</code> for this check. The property is
defined in this example only to provide clarity:
and <code>java.lang.Integer</code>:
</p>
<source>
&lt;module name=&quot;IllegalInstantiation&quot;&gt;
Expand All @@ -2544,41 +2520,6 @@ public class MyTest {
java.lang.Boolean d = new java.lang.Boolean(a); // violation, instantiation of
// java.lang.Boolean should be avoided

Integer e = new Integer(b); // violation, instantiation of
// java.lang.Integer should be avoided
Integer f = Integer.valueOf(b); // OK
}
}
</source>
<p>
To configure the check to allow violations for local classes vs classes
defined in the check, for example <code>java.lang.Boolean</code>, property
<code>tokens</code> must be defined to not mention <code>CLASS_DEF</code>, so its
value should be <code>LITERAL_NEW, PACKAGE_DEF, IMPORT</code>:
</p>
<source>
&lt;module name=&quot;IllegalInstantiation&quot;&gt;
&lt;property name=&quot;classes&quot; value=&quot;java.lang.Boolean,
java.lang.Integer&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;LITERAL_NEW, PACKAGE_DEF,
IMPORT&quot;/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
public class MyTest {
public class Boolean {
boolean a;

public Boolean (boolean a) { this.a = a; }
}

public void myTest (boolean a, int b) {
Boolean c = new Boolean(a); // violation, instantiation of
// java.lang.Boolean should be avoided
java.lang.Boolean d = new java.lang.Boolean(a); // violation, instantiation of
// java.lang.Boolean should be avoided

Integer e = new Integer(b); // violation, instantiation of
// java.lang.Integer should be avoided
Integer f = Integer.valueOf(b); // OK
Expand Down

0 comments on commit d6d443b

Please sign in to comment.