diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheck.java index b2f99b64df8..e8d782a23ac 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheck.java @@ -60,14 +60,6 @@ * Type is {@code java.lang.String[]}. * Default value is {@code ""}. * - *
  • - * Property {@code tokens} - tokens to check - * Type is {@code java.lang.String[]}. - * Validation type is {@code tokenSet}. - * Default value is: - * - * CLASS_DEF. - *
  • * *

    * To configure the check: @@ -95,12 +87,7 @@ * *

    * 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}: *

    *
      * <module name="IllegalInstantiation">
    @@ -131,41 +118,6 @@
      * }
      * 
    *

    - * 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}: - *

    - *
    - * <module name="IllegalInstantiation">
    - *   <property name="classes" value="java.lang.Boolean,
    - *     java.lang.Integer"/>
    - *   <property name="tokens" value="LITERAL_NEW, PACKAGE_DEF,
    - *     IMPORT"/>
    - * </module>
    - * 
    - *

    Example:

    - *
    - * 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
    - *   }
    - * }
    - * 
    - *

    * Finally, there is a limitation that it is currently not possible to specify array classes: *

    *
    @@ -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
    @@ -246,6 +193,7 @@ public int[] getRequiredTokens() {
                 TokenTypes.IMPORT,
                 TokenTypes.LITERAL_NEW,
                 TokenTypes.PACKAGE_DEF,
    +            TokenTypes.CLASS_DEF,
             };
         }
     
    diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/coding/IllegalInstantiationCheck.xml b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/coding/IllegalInstantiationCheck.xml
    index 5bd30de4886..f5719876025 100644
    --- a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/coding/IllegalInstantiationCheck.xml
    +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/coding/IllegalInstantiationCheck.xml
    @@ -30,12 +30,6 @@
                 
                    Specify fully qualified class names that should not be instantiated.
                 
    -            
    -               tokens to check
    -            
              
              
                 
    diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml
    index 642751feafc..1f41cf9891e 100644
    --- a/src/xdocs/config_coding.xml
    +++ b/src/xdocs/config_coding.xml
    @@ -2465,25 +2465,6 @@ try {
                   {}
                   3.0
                 
    -
    -            
    -              tokens
    -              tokens to check
    -
    -              
    -                subset of tokens
    -                
    -                CLASS_DEF
    -                  .
    -              
    -
    -              
    -                
    -                CLASS_DEF
    -                  .
    -              
    -              3.0
    -            
               
             
           
    @@ -2515,12 +2496,7 @@ public class MyTest {
             
             

    To configure the check to find instantiations of java.lang.Boolean - and java.lang.Integer. NOTE: Even if property tokens - is completely removed from the following configuration, Checkstyle will produce the - same results for violation. This is because if property tokens is not - defined in the configuration, Checkstyle will supply it with list of default tokens - CLASS_DEF, LITERAL_NEW, PACKAGE_DEF, IMPORT for this check. The property is - defined in this example only to provide clarity: + and java.lang.Integer:

    <module name="IllegalInstantiation"> @@ -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 - } -} - -

    - To configure the check to allow violations for local classes vs classes - defined in the check, for example java.lang.Boolean, property - tokens must be defined to not mention CLASS_DEF, so its - value should be LITERAL_NEW, PACKAGE_DEF, IMPORT: -

    - -<module name="IllegalInstantiation"> - <property name="classes" value="java.lang.Boolean, - java.lang.Integer"/> - <property name="tokens" value="LITERAL_NEW, PACKAGE_DEF, - IMPORT"/> -</module> - -

    Example:

    - -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