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" file.
045     */
046    public static final String MSG_KEY = "too.many.components";
047
048    /** Default maximum number of allowed components. */
049    private static final int DEFAULT_MAX_COMPONENTS = 8;
050
051    /** Specify the maximum number of components allowed in the header of a record definition. */
052    private int max = DEFAULT_MAX_COMPONENTS;
053
054    /**
055     * Access modifiers of record definitions where the number
056     * of record components should be checked.
057     */
058    private AccessModifierOption[] accessModifiers = {
059        AccessModifierOption.PUBLIC,
060        AccessModifierOption.PROTECTED,
061        AccessModifierOption.PACKAGE,
062        AccessModifierOption.PRIVATE,
063    };
064
065    /**
066     * Creates a new {@code RecordComponentNumberCheck} instance.
067     */
068    public RecordComponentNumberCheck() {
069        // no code by default
070    }
071
072    /**
073     * Setter to specify the maximum number of components allowed in the header
074     * of a record definition.
075     *
076     * @param value the maximum allowed.
077     * @since 8.36
078     */
079    public void setMax(int value) {
080        max = value;
081    }
082
083    /**
084     * Setter to access modifiers of record definitions where the number of record
085     * components should be checked.
086     *
087     * @param accessModifiers access modifiers of record definitions which should be checked.
088     * @since 8.36
089     */
090    public void setAccessModifiers(AccessModifierOption... accessModifiers) {
091        this.accessModifiers =
092                Arrays.copyOf(accessModifiers, accessModifiers.length);
093    }
094
095    @Override
096    public int[] getDefaultTokens() {
097        return getAcceptableTokens();
098    }
099
100    @Override
101    public int[] getAcceptableTokens() {
102        return new int[] {
103            TokenTypes.RECORD_DEF,
104        };
105    }
106
107    @Override
108    public int[] getRequiredTokens() {
109        return getAcceptableTokens();
110    }
111
112    @Override
113    public void visitToken(DetailAST ast) {
114        final AccessModifierOption accessModifier =
115            CheckUtil.getAccessModifierFromModifiersToken(ast);
116
117        if (matchAccessModifiers(accessModifier)) {
118            final DetailAST recordComponents =
119                ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
120            final int componentCount = countComponents(recordComponents);
121
122            if (componentCount > max) {
123                log(ast, MSG_KEY, componentCount, max);
124            }
125        }
126    }
127
128    /**
129     * Method to count the number of record components in this record definition.
130     *
131     * @param recordComponents the ast to check
132     * @return the number of record components in this record definition
133     */
134    private static int countComponents(DetailAST recordComponents) {
135        return recordComponents.getChildCount(TokenTypes.RECORD_COMPONENT_DEF);
136    }
137
138    /**
139     * Checks whether a record definition has the correct access modifier to be checked.
140     *
141     * @param accessModifier the access modifier of the record definition.
142     * @return whether the record definition matches the expected access modifier.
143     */
144    private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
145        return Arrays.stream(accessModifiers)
146                .anyMatch(modifier -> modifier == accessModifier);
147    }
148
149}