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 java.util.ArrayList;
23  import java.util.List;
24  import java.util.Optional;
25  
26  import com.puppycrawl.tools.checkstyle.StatelessCheck;
27  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28  import com.puppycrawl.tools.checkstyle.api.DetailAST;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  /**
32   * <div>
33   * Checks that all constructors are grouped together.
34   * If there is any non-constructor code separating constructors,
35   * this check identifies and logs a violation for those ungrouped constructors.
36   * The violation message will specify the line number of the last grouped constructor.
37   * Comments between constructors are allowed.
38   * </div>
39   *
40   * <p>
41   * Rationale: Grouping constructors together in a class improves code readability
42   * and maintainability. It allows developers to easily understand
43   * the different ways an object can be instantiated
44   * and the tasks performed by each constructor.
45   * </p>
46   *
47   * @since 10.17.0
48   */
49  
50  @StatelessCheck
51  public class ConstructorsDeclarationGroupingCheck 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 = "constructors.declaration.grouping";
58  
59      /**
60       * A key is pointing to the warning message text in "messages.properties"
61       * file.
62       */
63      public static final String MSG_ORDER = "constructors.declaration.order";
64  
65      /**
66       * Control whether to order constructors by increasing parameter count or not.
67       */
68      private boolean orderByIncreasingParameterCount;
69  
70      /**
71       * Creates a new {@code ConstructorsDeclarationGroupingCheck} instance.
72       */
73      public ConstructorsDeclarationGroupingCheck() {
74          // no code by default
75      }
76  
77      /**
78       * Setter to control whether to enforce order by increasing parameter count (arity) or not.
79       *
80       * @param orderByIncreasingParameterCount true if order by increasing parameter
81       *        count is required.
82       * @since 13.6.0
83       */
84      public void setOrderByIncreasingParameterCount(boolean orderByIncreasingParameterCount) {
85          this.orderByIncreasingParameterCount = orderByIncreasingParameterCount;
86      }
87  
88      @Override
89      public int[] getDefaultTokens() {
90          return getRequiredTokens();
91      }
92  
93      @Override
94      public int[] getAcceptableTokens() {
95          return getRequiredTokens();
96      }
97  
98      @Override
99      public int[] getRequiredTokens() {
100         return new int[] {
101             TokenTypes.CLASS_DEF,
102             TokenTypes.ENUM_DEF,
103             TokenTypes.RECORD_DEF,
104         };
105     }
106 
107     @Override
108     public void visitToken(DetailAST ast) {
109         // list of all child ASTs
110         final List<DetailAST> children = getChildList(ast);
111 
112         // find first constructor
113         final DetailAST firstConstructor = children.stream()
114                 .filter(ConstructorsDeclarationGroupingCheck::isConstructor)
115                 .findFirst()
116                 .orElse(null);
117 
118         if (firstConstructor != null) {
119 
120             // get all children AST after the first constructor
121             final List<DetailAST> childrenAfterFirstConstructor =
122                     children.subList(children.indexOf(firstConstructor), children.size());
123 
124             // find the first index of non-constructor AST after the first constructor, if present
125             final Optional<Integer> indexOfFirstNonConstructor = childrenAfterFirstConstructor
126                     .stream()
127                     .filter(currAst -> !isConstructor(currAst))
128                     .findFirst()
129                     .map(children::indexOf);
130 
131             // list of all children after first non-constructor AST
132             final List<DetailAST> childrenAfterFirstNonConstructor = indexOfFirstNonConstructor
133                     .map(index -> children.subList(index, children.size()))
134                     .orElseGet(ArrayList::new);
135 
136             // create a list of all constructors that are not grouped to log
137             final List<DetailAST> constructorsToLog = childrenAfterFirstNonConstructor.stream()
138                     .filter(ConstructorsDeclarationGroupingCheck::isConstructor)
139                     .toList();
140 
141             // find the last grouped constructor
142             final DetailAST lastGroupedConstructor = childrenAfterFirstConstructor.stream()
143                     .takeWhile(ConstructorsDeclarationGroupingCheck::isConstructor)
144                     .reduce((first, second) -> second)
145                     .orElse(firstConstructor);
146 
147             // log all constructors that are not grouped
148             constructorsToLog
149                     .forEach(ctor -> log(ctor, MSG_KEY, lastGroupedConstructor.getLineNo()));
150 
151             if (orderByIncreasingParameterCount) {
152 
153                 // list of all constructor ASTs
154                 final List<DetailAST> allConstructors = children.stream()
155                         .filter(ConstructorsDeclarationGroupingCheck::isConstructor)
156                         .toList();
157 
158                 int previousParamCount = 0;
159                 boolean isOrdered = true;
160                 for (DetailAST constructor : allConstructors) {
161                     final int currentParamCount = getParameterCount(constructor);
162                     isOrdered = isOrdered && currentParamCount >= previousParamCount;
163                     previousParamCount = currentParamCount;
164                     if (!isOrdered) {
165                         log(constructor, MSG_ORDER);
166                     }
167                 }
168             }
169         }
170     }
171 
172     /**
173      * Get a list of all children of the given AST.
174      *
175      * @param ast the AST to get children of
176      * @return a list of all children of the given AST
177      */
178     private static List<DetailAST> getChildList(DetailAST ast) {
179         final List<DetailAST> children = new ArrayList<>();
180         DetailAST child = ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild();
181         while (child != null) {
182             children.add(child);
183             child = child.getNextSibling();
184         }
185         return children;
186     }
187 
188     /**
189      * Check if the given AST is a constructor.
190      *
191      * @param ast the AST to check
192      * @return true if the given AST is a constructor, false otherwise
193      */
194     private static boolean isConstructor(DetailAST ast) {
195         return ast.getType() == TokenTypes.CTOR_DEF
196                 || ast.getType() == TokenTypes.COMPACT_CTOR_DEF;
197     }
198 
199     /**
200      * Get the parameter count of a constructor.
201      *
202      * @param constructor the constructor AST
203      * @return the parameter count of the constructor
204      */
205     private static int getParameterCount(DetailAST constructor) {
206         final DetailAST params = constructor.findFirstToken(TokenTypes.PARAMETERS);
207         int parameterCount = 0;
208         if (params != null) {
209             parameterCount = params.getChildCount(TokenTypes.PARAMETER_DEF);
210         }
211         return parameterCount;
212     }
213 
214 }