View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
27  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
28  
29  /**
30   * <div>
31   * Checks if any class or object member is explicitly initialized
32   * to default for its type value ({@code null} for object
33   * references, zero for numeric types and {@code char}
34   * and {@code false} for {@code boolean}.
35   * </div>
36   *
37   * <p>
38   * Rationale: Each instance variable gets
39   * initialized twice, to the same value. Java
40   * initializes each instance variable to its default
41   * value ({@code 0} or {@code null}) before performing any
42   * initialization specified in the code.
43   * So there is a minor inefficiency.
44   * </p>
45   * <ul>
46   * <li>
47   * Property {@code onlyObjectReferences} - Control whether only explicit
48   * initializations made to null for objects should be checked.
49   * Type is {@code boolean}.
50   * Default value is {@code false}.
51   * </li>
52   * </ul>
53   *
54   * <p>
55   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
56   * </p>
57   *
58   * <p>
59   * Violation Message Keys:
60   * </p>
61   * <ul>
62   * <li>
63   * {@code explicit.init}
64   * </li>
65   * </ul>
66   *
67   * @since 3.2
68   */
69  @StatelessCheck
70  public class ExplicitInitializationCheck extends AbstractCheck {
71  
72      /**
73       * A key is pointing to the warning message text in "messages.properties"
74       * file.
75       */
76      public static final String MSG_KEY = "explicit.init";
77  
78      /**
79       * Control whether only explicit initializations made to null for objects should be checked.
80       **/
81      private boolean onlyObjectReferences;
82  
83      @Override
84      public final int[] getDefaultTokens() {
85          return getRequiredTokens();
86      }
87  
88      @Override
89      public final int[] getRequiredTokens() {
90          return new int[] {TokenTypes.VARIABLE_DEF};
91      }
92  
93      @Override
94      public final int[] getAcceptableTokens() {
95          return getRequiredTokens();
96      }
97  
98      /**
99       * Setter to control whether only explicit initializations made to null
100      * for objects should be checked.
101      *
102      * @param onlyObjectReferences whether only explicit initialization made to null
103      *                             should be checked
104      * @since 7.8
105      */
106     public void setOnlyObjectReferences(boolean onlyObjectReferences) {
107         this.onlyObjectReferences = onlyObjectReferences;
108     }
109 
110     @Override
111     public void visitToken(DetailAST ast) {
112         if (!isSkipCase(ast)) {
113             final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
114             final DetailAST exprStart =
115                 assign.getFirstChild().getFirstChild();
116             if (exprStart.getType() == TokenTypes.LITERAL_NULL) {
117                 final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
118                 log(ident, MSG_KEY, ident.getText(), "null");
119             }
120             if (!onlyObjectReferences) {
121                 validateNonObjects(ast);
122             }
123         }
124     }
125 
126     /**
127      * Checks for explicit initializations made to 'false', '0' and '\0'.
128      *
129      * @param ast token being checked for explicit initializations
130      */
131     private void validateNonObjects(DetailAST ast) {
132         final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
133         final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
134         final DetailAST exprStart =
135                 assign.getFirstChild().getFirstChild();
136         final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
137         final int primitiveType = type.getFirstChild().getType();
138         if (primitiveType == TokenTypes.LITERAL_BOOLEAN
139                 && exprStart.getType() == TokenTypes.LITERAL_FALSE) {
140             log(ident, MSG_KEY, ident.getText(), "false");
141         }
142         if (isNumericType(primitiveType) && isZero(exprStart)) {
143             log(ident, MSG_KEY, ident.getText(), "0");
144         }
145         if (primitiveType == TokenTypes.LITERAL_CHAR
146                 && isZeroChar(exprStart)) {
147             log(ident, MSG_KEY, ident.getText(), "\\0");
148         }
149     }
150 
151     /**
152      * Examine char literal for initializing to default value.
153      *
154      * @param exprStart expression
155      * @return true is literal is initialized by zero symbol
156      */
157     private static boolean isZeroChar(DetailAST exprStart) {
158         return isZero(exprStart)
159             || "'\\0'".equals(exprStart.getText());
160     }
161 
162     /**
163      * Checks for cases that should be skipped: no assignment, local variable, final variables.
164      *
165      * @param ast Variable def AST
166      * @return true is that is a case that need to be skipped.
167      */
168     private static boolean isSkipCase(DetailAST ast) {
169         boolean skipCase = true;
170 
171         // do not check local variables and
172         // fields declared in interface/annotations
173         if (!ScopeUtil.isLocalVariableDef(ast)
174                 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) {
175             final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
176 
177             if (assign != null) {
178                 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
179                 skipCase = modifiers.findFirstToken(TokenTypes.FINAL) != null;
180             }
181         }
182         return skipCase;
183     }
184 
185     /**
186      * Determine if a given type is a numeric type.
187      *
188      * @param type code of the type for check.
189      * @return true if it's a numeric type.
190      * @see TokenTypes
191      */
192     private static boolean isNumericType(int type) {
193         return type == TokenTypes.LITERAL_BYTE
194                 || type == TokenTypes.LITERAL_SHORT
195                 || type == TokenTypes.LITERAL_INT
196                 || type == TokenTypes.LITERAL_FLOAT
197                 || type == TokenTypes.LITERAL_LONG
198                 || type == TokenTypes.LITERAL_DOUBLE;
199     }
200 
201     /**
202      * Checks if given node contains numeric constant for zero.
203      *
204      * @param expr node to check.
205      * @return true if given node contains numeric constant for zero.
206      */
207     private static boolean isZero(DetailAST expr) {
208         final int type = expr.getType();
209         final boolean isZero;
210         switch (type) {
211             case TokenTypes.NUM_FLOAT:
212             case TokenTypes.NUM_DOUBLE:
213             case TokenTypes.NUM_INT:
214             case TokenTypes.NUM_LONG:
215                 final String text = expr.getText();
216                 isZero = Double.compare(CheckUtil.parseDouble(text, type), 0.0) == 0;
217                 break;
218             default:
219                 isZero = false;
220         }
221         return isZero;
222     }
223 
224 }