001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.sizes;
021
022import java.util.Arrays;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
029import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
030
031/**
032 * <div>
033 * Checks the number of record components in the
034 * <a href="https://docs.oracle.com/javase/specs/jls/se14/preview/specs/records-jls.html#jls-8.10.1">
035 * header</a> of a record definition.
036 * </div>
037 *
038 * <ul>
039 * <li>
040 * Property {@code accessModifiers} - Access modifiers of record definitions where
041 * the number of record components should be checked.
042 * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]}.
043 * Default value is {@code public, protected, package, private}.
044 * </li>
045 * <li>
046 * Property {@code max} - Specify the maximum number of components allowed in the header of a
047 * record definition.
048 * Type is {@code int}.
049 * Default value is {@code 8}.
050 * </li>
051 * </ul>
052 *
053 * <p>
054 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
055 * </p>
056 *
057 * <p>
058 * Violation Message Keys:
059 * </p>
060 * <ul>
061 * <li>
062 * {@code too.many.components}
063 * </li>
064 * </ul>
065 *
066 * @since 8.36
067 */
068@StatelessCheck
069public class RecordComponentNumberCheck extends AbstractCheck {
070
071    /**
072     * A key is pointing to the warning message text in "messages.properties"
073     * file.
074     */
075    public static final String MSG_KEY = "too.many.components";
076
077    /** Default maximum number of allowed components. */
078    private static final int DEFAULT_MAX_COMPONENTS = 8;
079
080    /** Specify the maximum number of components allowed in the header of a record definition. */
081    private int max = DEFAULT_MAX_COMPONENTS;
082
083    /**
084     * Access modifiers of record definitions where the number
085     * of record components should be checked.
086     */
087    private AccessModifierOption[] accessModifiers = {
088        AccessModifierOption.PUBLIC,
089        AccessModifierOption.PROTECTED,
090        AccessModifierOption.PACKAGE,
091        AccessModifierOption.PRIVATE,
092    };
093
094    /**
095     * Setter to specify the maximum number of components allowed in the header
096     * of a record definition.
097     *
098     * @param value the maximum allowed.
099     * @since 8.36
100     */
101    public void setMax(int value) {
102        max = value;
103    }
104
105    /**
106     * Setter to access modifiers of record definitions where the number of record
107     * components should be checked.
108     *
109     * @param accessModifiers access modifiers of record definitions which should be checked.
110     * @since 8.36
111     */
112    public void setAccessModifiers(AccessModifierOption... accessModifiers) {
113        this.accessModifiers =
114                Arrays.copyOf(accessModifiers, accessModifiers.length);
115    }
116
117    @Override
118    public int[] getDefaultTokens() {
119        return getAcceptableTokens();
120    }
121
122    @Override
123    public int[] getAcceptableTokens() {
124        return new int[] {
125            TokenTypes.RECORD_DEF,
126        };
127    }
128
129    @Override
130    public int[] getRequiredTokens() {
131        return getAcceptableTokens();
132    }
133
134    @Override
135    public void visitToken(DetailAST ast) {
136        final AccessModifierOption accessModifier =
137            CheckUtil.getAccessModifierFromModifiersToken(ast);
138
139        if (matchAccessModifiers(accessModifier)) {
140            final DetailAST recordComponents =
141                ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
142            final int componentCount = countComponents(recordComponents);
143
144            if (componentCount > max) {
145                log(ast, MSG_KEY, componentCount, max);
146            }
147        }
148    }
149
150    /**
151     * Method to count the number of record components in this record definition.
152     *
153     * @param recordComponents the ast to check
154     * @return the number of record components in this record definition
155     */
156    private static int countComponents(DetailAST recordComponents) {
157        return recordComponents.getChildCount(TokenTypes.RECORD_COMPONENT_DEF);
158    }
159
160    /**
161     * Checks whether a record definition has the correct access modifier to be checked.
162     *
163     * @param accessModifier the access modifier of the record definition.
164     * @return whether the record definition matches the expected access modifier.
165     */
166    private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
167        return Arrays.stream(accessModifiers)
168                .anyMatch(modifier -> modifier == accessModifier);
169    }
170}