View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
27  /**
28   * <div>
29   * Checks that classes (except abstract ones) define a constructor and don't rely
30   * on the default one.
31   * </div>
32   *
33   * <p>
34   * Compatibility note: <b>when creating</b> an explicit constructor already
35   * in existing class that used by other in codebases that you do not own, it must match
36   * precisely the declaration of the automatically generated constructor;
37   * even if the constructor should logically be protected, it must be made
38   * public to match the declaration of the automatically generated
39   * constructor, for <b>compatibility</b>.
40   * </p>
41   *
42   * <p>
43   * See
44   * <a href="https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html#defaultconstructors">
45   * Documentation Comments Style Guide</a>.
46   * </p>
47   *
48   * @since 3.4
49   */
50  @StatelessCheck
51  public class MissingCtorCheck extends AbstractCheck {
52  
53      /**
54       * A key is pointing to the warning message text in "messages.properties"
55       * file.
56       */
57      public static final String MSG_KEY = "missing.ctor";
58  
59      @Override
60      public int[] getDefaultTokens() {
61          return getRequiredTokens();
62      }
63  
64      @Override
65      public int[] getAcceptableTokens() {
66          return getRequiredTokens();
67      }
68  
69      @Override
70      public int[] getRequiredTokens() {
71          return new int[] {TokenTypes.CLASS_DEF};
72      }
73  
74      @Override
75      public void visitToken(DetailAST ast) {
76          final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
77          if (modifiers.findFirstToken(TokenTypes.ABSTRACT) == null
78                  && ast.findFirstToken(TokenTypes.OBJBLOCK)
79                      .findFirstToken(TokenTypes.CTOR_DEF) == null) {
80              log(ast, MSG_KEY);
81          }
82      }
83  
84  }