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