Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3955: corrected tokens so all are required #12108

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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,19 +87,12 @@
* </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}:
romani marked this conversation as resolved.
Show resolved Hide resolved
* </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;CLASS_DEF, LITERAL_NEW,
* PACKAGE_DEF, IMPORT&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
Expand All @@ -131,41 +116,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 +177,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 +191,7 @@ public int[] getRequiredTokens() {
TokenTypes.IMPORT,
TokenTypes.LITERAL_NEW,
TokenTypes.PACKAGE_DEF,
TokenTypes.CLASS_DEF,
};
}

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public void testWrongPackage() throws Exception {
expected);
}

@Test
public void testJavaLangPackage3() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputIllegalInstantiationLang3.java"),
expected);
}

@Test
public void testNameSimilarToStandardClass() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
IllegalInstantiation
classes = jaaa.lnng.Boolean,jaaa.lnng.String
tokens = (default)CLASS_DEF


*/

package com.puppycrawl.tools.checkstyle.checks.coding.illegalinstantiation;

class InputIllegalInstantiationLang3 {
Boolean obj = new Boolean(true); // ok
Integer obj2 = new Integer(0);
}
63 changes: 1 addition & 62 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
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,19 +2496,12 @@ 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;
&lt;property name=&quot;classes&quot; value=&quot;java.lang.Boolean,
java.lang.Integer&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;CLASS_DEF, LITERAL_NEW,
PACKAGE_DEF, IMPORT&quot;/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
Expand All @@ -2544,41 +2518,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