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.sizes;
21
22 import java.util.Arrays;
23
24 import com.puppycrawl.tools.checkstyle.StatelessCheck;
25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28 import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
29 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
30
31 /**
32 * <div>
33 * Checks the number of record components in the
34 * <a href="https://docs.oracle.com/javase/specs/jls/se14/preview/specs/records-jls.html#jls-8.10.1">
35 * header</a> of a record definition.
36 * </div>
37 *
38 * @since 8.36
39 */
40 @StatelessCheck
41 public class RecordComponentNumberCheck extends AbstractCheck {
42
43 /**
44 * A key is pointing to the warning message text in "messages.properties"
45 * file.
46 */
47 public static final String MSG_KEY = "too.many.components";
48
49 /** Default maximum number of allowed components. */
50 private static final int DEFAULT_MAX_COMPONENTS = 8;
51
52 /** Specify the maximum number of components allowed in the header of a record definition. */
53 private int max = DEFAULT_MAX_COMPONENTS;
54
55 /**
56 * Access modifiers of record definitions where the number
57 * of record components should be checked.
58 */
59 private AccessModifierOption[] accessModifiers = {
60 AccessModifierOption.PUBLIC,
61 AccessModifierOption.PROTECTED,
62 AccessModifierOption.PACKAGE,
63 AccessModifierOption.PRIVATE,
64 };
65
66 /**
67 * Creates a new {@code RecordComponentNumberCheck} instance.
68 */
69 public RecordComponentNumberCheck() {
70 // no code by default
71 }
72
73 /**
74 * Setter to specify the maximum number of components allowed in the header
75 * of a record definition.
76 *
77 * @param value the maximum allowed.
78 * @since 8.36
79 */
80 public void setMax(int value) {
81 max = value;
82 }
83
84 /**
85 * Setter to access modifiers of record definitions where the number of record
86 * components should be checked.
87 *
88 * @param accessModifiers access modifiers of record definitions which should be checked.
89 * @since 8.36
90 */
91 public void setAccessModifiers(AccessModifierOption... accessModifiers) {
92 this.accessModifiers =
93 Arrays.copyOf(accessModifiers, accessModifiers.length);
94 }
95
96 @Override
97 public int[] getDefaultTokens() {
98 return getAcceptableTokens();
99 }
100
101 @Override
102 public int[] getAcceptableTokens() {
103 return new int[] {
104 TokenTypes.RECORD_DEF,
105 };
106 }
107
108 @Override
109 public int[] getRequiredTokens() {
110 return getAcceptableTokens();
111 }
112
113 @Override
114 public void visitToken(DetailAST ast) {
115 final AccessModifierOption accessModifier =
116 CheckUtil.getAccessModifierFromModifiersToken(ast);
117
118 if (matchAccessModifiers(accessModifier)) {
119 final DetailAST recordComponents =
120 ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
121 final int componentCount = countComponents(recordComponents);
122
123 if (componentCount > max) {
124 log(ast, MSG_KEY, componentCount, max);
125 }
126 }
127 }
128
129 /**
130 * Method to count the number of record components in this record definition.
131 *
132 * @param recordComponents the ast to check
133 * @return the number of record components in this record definition
134 */
135 private static int countComponents(DetailAST recordComponents) {
136 return recordComponents.getChildCount(TokenTypes.RECORD_COMPONENT_DEF);
137 }
138
139 /**
140 * Checks whether a record definition has the correct access modifier to be checked.
141 *
142 * @param accessModifier the access modifier of the record definition.
143 * @return whether the record definition matches the expected access modifier.
144 */
145 private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
146 return Arrays.stream(accessModifiers)
147 .anyMatch(modifier -> modifier == accessModifier);
148 }
149
150 }