View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.CheckUtils;
27  import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
28  
29  /**
30   * <p>
31   * Checks if any class or object member 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   * </p>
36   * <p>
37   * Rationale: each instance variable gets
38   * initialized twice, to the same value.  Java
39   * initializes each instance variable to its default
40   * value (0 or null) before performing any
41   * initialization specified in the code.  So in this case,
42   * x gets initialized to 0 twice, and bar gets initialized
43   * to null twice.  So there is a minor inefficiency.  This style of
44   * coding is a hold-over from C/C++ style coding,
45   * and it shows that the developer isn't really confident that
46   * Java really initializes instance variables to default
47   * values.
48   * </p>
49   *
50   */
51  @StatelessCheck
52  public class ExplicitInitializationCheck extends AbstractCheck {
53  
54      /**
55       * A key is pointing to the warning message text in "messages.properties"
56       * file.
57       */
58      public static final String MSG_KEY = "explicit.init";
59  
60      /** Whether only explicit initialization made to null should be checked.**/
61      private boolean onlyObjectReferences;
62  
63      @Override
64      public final int[] getDefaultTokens() {
65          return getRequiredTokens();
66      }
67  
68      @Override
69      public final int[] getRequiredTokens() {
70          return new int[] {TokenTypes.VARIABLE_DEF};
71      }
72  
73      @Override
74      public final int[] getAcceptableTokens() {
75          return getRequiredTokens();
76      }
77  
78      /**
79       * Sets whether only explicit initialization made to null should be checked.
80       * @param onlyObjectReferences whether only explicit initialization made to null
81       *                             should be checked
82       */
83      public void setOnlyObjectReferences(boolean onlyObjectReferences) {
84          this.onlyObjectReferences = onlyObjectReferences;
85      }
86  
87      @Override
88      public void visitToken(DetailAST ast) {
89          if (!isSkipCase(ast)) {
90              final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
91              final DetailAST exprStart =
92                  assign.getFirstChild().getFirstChild();
93              final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
94              if (isObjectType(type)
95                  && exprStart.getType() == TokenTypes.LITERAL_NULL) {
96                  final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
97                  log(ident, MSG_KEY, ident.getText(), "null");
98              }
99              if (!onlyObjectReferences) {
100                 validateNonObjects(ast);
101             }
102         }
103     }
104 
105     /**
106      * Checks for explicit initializations made to 'false', '0' and '\0'.
107      * @param ast token being checked for explicit initializations
108      */
109     private void validateNonObjects(DetailAST ast) {
110         final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
111         final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
112         final DetailAST exprStart =
113                 assign.getFirstChild().getFirstChild();
114         final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
115         final int primitiveType = type.getFirstChild().getType();
116         if (primitiveType == TokenTypes.LITERAL_BOOLEAN
117                 && exprStart.getType() == TokenTypes.LITERAL_FALSE) {
118             log(ident, MSG_KEY, ident.getText(), "false");
119         }
120         if (isNumericType(primitiveType) && isZero(exprStart)) {
121             log(ident, MSG_KEY, ident.getText(), "0");
122         }
123         if (primitiveType == TokenTypes.LITERAL_CHAR
124                 && isZeroChar(exprStart)) {
125             log(ident, MSG_KEY, ident.getText(), "\\0");
126         }
127     }
128 
129     /**
130      * Examine char literal for initializing to default value.
131      * @param exprStart expression
132      * @return true is literal is initialized by zero symbol
133      */
134     private static boolean isZeroChar(DetailAST exprStart) {
135         return isZero(exprStart)
136             || exprStart.getType() == TokenTypes.CHAR_LITERAL
137             && "'\\0'".equals(exprStart.getText());
138     }
139 
140     /**
141      * Checks for cases that should be skipped: no assignment, local variable, final variables.
142      * @param ast Variable def AST
143      * @return true is that is a case that need to be skipped.
144      */
145     private static boolean isSkipCase(DetailAST ast) {
146         boolean skipCase = true;
147 
148         // do not check local variables and
149         // fields declared in interface/annotations
150         if (!ScopeUtils.isLocalVariableDef(ast)
151                 && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) {
152             final DetailAST assign = ast.findFirstToken(TokenTypes.ASSIGN);
153 
154             if (assign != null) {
155                 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
156                 skipCase = modifiers.findFirstToken(TokenTypes.FINAL) != null;
157             }
158         }
159         return skipCase;
160     }
161 
162     /**
163      * Determines if a given type is an object type.
164      * @param type type to check.
165      * @return true if it is an object type.
166      */
167     private static boolean isObjectType(DetailAST type) {
168         final int objectType = type.getFirstChild().getType();
169         return objectType == TokenTypes.IDENT || objectType == TokenTypes.DOT
170                 || objectType == TokenTypes.ARRAY_DECLARATOR;
171     }
172 
173     /**
174      * Determine if a given type is a numeric type.
175      * @param type code of the type for check.
176      * @return true if it's a numeric type.
177      * @see TokenTypes
178      */
179     private static boolean isNumericType(int type) {
180         return type == TokenTypes.LITERAL_BYTE
181                 || type == TokenTypes.LITERAL_SHORT
182                 || type == TokenTypes.LITERAL_INT
183                 || type == TokenTypes.LITERAL_FLOAT
184                 || type == TokenTypes.LITERAL_LONG
185                 || type == TokenTypes.LITERAL_DOUBLE;
186     }
187 
188     /**
189      * Checks if given node contains numeric constant for zero.
190      *
191      * @param expr node to check.
192      * @return true if given node contains numeric constant for zero.
193      */
194     private static boolean isZero(DetailAST expr) {
195         final int type = expr.getType();
196         final boolean isZero;
197         switch (type) {
198             case TokenTypes.NUM_FLOAT:
199             case TokenTypes.NUM_DOUBLE:
200             case TokenTypes.NUM_INT:
201             case TokenTypes.NUM_LONG:
202                 final String text = expr.getText();
203                 isZero = Double.compare(CheckUtils.parseDouble(text, type), 0.0) == 0;
204                 break;
205             default:
206                 isZero = false;
207         }
208         return isZero;
209     }
210 
211 }