001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 * @since 8.36
039 */
040@StatelessCheck
041public class RecordComponentNumberCheck extends AbstractCheck {
042
043    /**
044     * A key is pointing to the warning message text in "messages.properties"
045     * file.
046     */
047    public static final String MSG_KEY = "too.many.components";
048
049    /** Default maximum number of allowed components. */
050    private static final int DEFAULT_MAX_COMPONENTS = 8;
051
052    /** Specify the maximum number of components allowed in the header of a record definition. */
053    private int max = DEFAULT_MAX_COMPONENTS;
054
055    /**
056     * Access modifiers of record definitions where the number
057     * of record components should be checked.
058     */
059    private AccessModifierOption[] accessModifiers = {
060        AccessModifierOption.PUBLIC,
061        AccessModifierOption.PROTECTED,
062        AccessModifierOption.PACKAGE,
063        AccessModifierOption.PRIVATE,
064    };
065
066    /**
067     * Creates a new {@code RecordComponentNumberCheck} instance.
068     */
069    public RecordComponentNumberCheck() {
070        // no code by default
071    }
072
073    /**
074     * Setter to specify the maximum number of components allowed in the header
075     * of a record definition.
076     *
077     * @param value the maximum allowed.
078     * @since 8.36
079     */
080    public void setMax(int value) {
081        max = value;
082    }
083
084    /**
085     * Setter to access modifiers of record definitions where the number of record
086     * components should be checked.
087     *
088     * @param accessModifiers access modifiers of record definitions which should be checked.
089     * @since 8.36
090     */
091    public void setAccessModifiers(AccessModifierOption... accessModifiers) {
092        this.accessModifiers =
093                Arrays.copyOf(accessModifiers, accessModifiers.length);
094    }
095
096    @Override
097    public int[] getDefaultTokens() {
098        return getAcceptableTokens();
099    }
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}