1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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 import java.util.concurrent.atomic.AtomicInteger;
24
25 import com.puppycrawl.tools.checkstyle.StatelessCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
30 import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
31 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
32
33 /**
34 * <p>
35 * Checks the number of record components in the
36 * <a href="https://docs.oracle.com/javase/specs/jls/se14/preview/specs/records-jls.html#jls-8.10.1">
37 * header</a> of a record definition.
38 * </p>
39 * <ul>
40 * <li>
41 * Property {@code accessModifiers} - Access modifiers of record definitions where
42 * the number of record components should be checked.
43 * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]}.
44 * Default value is {@code public, protected, package, private}.
45 * </li>
46 * <li>
47 * Property {@code max} - Specify the maximum number of components allowed in the header of a
48 * record definition.
49 * Type is {@code int}.
50 * Default value is {@code 8}.
51 * </li>
52 * </ul>
53 * <p>
54 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
55 * </p>
56 * <p>
57 * Violation Message Keys:
58 * </p>
59 * <ul>
60 * <li>
61 * {@code too.many.components}
62 * </li>
63 * </ul>
64 *
65 * @since 8.36
66 */
67 @StatelessCheck
68 public class RecordComponentNumberCheck extends AbstractCheck {
69
70 /**
71 * A key is pointing to the warning message text in "messages.properties"
72 * file.
73 */
74 public static final String MSG_KEY = "too.many.components";
75
76 /** Default maximum number of allowed components. */
77 private static final int DEFAULT_MAX_COMPONENTS = 8;
78
79 /** Specify the maximum number of components allowed in the header of a record definition. */
80 private int max = DEFAULT_MAX_COMPONENTS;
81
82 /**
83 * Access modifiers of record definitions where the number
84 * of record components should be checked.
85 */
86 private AccessModifierOption[] accessModifiers = {
87 AccessModifierOption.PUBLIC,
88 AccessModifierOption.PROTECTED,
89 AccessModifierOption.PACKAGE,
90 AccessModifierOption.PRIVATE,
91 };
92
93 /**
94 * Setter to specify the maximum number of components allowed in the header
95 * of a record definition.
96 *
97 * @param value the maximum allowed.
98 * @since 8.36
99 */
100 public void setMax(int value) {
101 max = value;
102 }
103
104 /**
105 * Setter to access modifiers of record definitions where the number of record
106 * components should be checked.
107 *
108 * @param accessModifiers access modifiers of record definitions which should be checked.
109 * @since 8.36
110 */
111 public void setAccessModifiers(AccessModifierOption... accessModifiers) {
112 this.accessModifiers =
113 Arrays.copyOf(accessModifiers, accessModifiers.length);
114 }
115
116 @Override
117 public int[] getDefaultTokens() {
118 return getAcceptableTokens();
119 }
120
121 @Override
122 public int[] getAcceptableTokens() {
123 return new int[] {
124 TokenTypes.RECORD_DEF,
125 };
126 }
127
128 @Override
129 public int[] getRequiredTokens() {
130 return getAcceptableTokens();
131 }
132
133 @Override
134 public void visitToken(DetailAST ast) {
135 final AccessModifierOption accessModifier =
136 CheckUtil.getAccessModifierFromModifiersToken(ast);
137
138 if (matchAccessModifiers(accessModifier)) {
139 final DetailAST recordComponents =
140 ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
141 final int componentCount = countComponents(recordComponents);
142
143 if (componentCount > max) {
144 log(ast, MSG_KEY, componentCount, max);
145 }
146 }
147 }
148
149 /**
150 * Method to count the number of record components in this record definition.
151 *
152 * @param recordComponents the ast to check
153 * @return the number of record components in this record definition
154 */
155 private static int countComponents(DetailAST recordComponents) {
156 final AtomicInteger count = new AtomicInteger(0);
157 TokenUtil.forEachChild(recordComponents,
158 TokenTypes.RECORD_COMPONENT_DEF,
159 node -> count.getAndIncrement());
160 return count.get();
161 }
162
163 /**
164 * Checks whether a record definition has the correct access modifier to be checked.
165 *
166 * @param accessModifier the access modifier of the record definition.
167 * @return whether the record definition matches the expected access modifier.
168 */
169 private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
170 return Arrays.stream(accessModifiers)
171 .anyMatch(modifier -> modifier == accessModifier);
172 }
173 }