View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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       * Setter to specify the maximum number of components allowed in the header
68       * of a record definition.
69       *
70       * @param value the maximum allowed.
71       * @since 8.36
72       */
73      public void setMax(int value) {
74          max = value;
75      }
76  
77      /**
78       * Setter to access modifiers of record definitions where the number of record
79       * components should be checked.
80       *
81       * @param accessModifiers access modifiers of record definitions which should be checked.
82       * @since 8.36
83       */
84      public void setAccessModifiers(AccessModifierOption... accessModifiers) {
85          this.accessModifiers =
86                  Arrays.copyOf(accessModifiers, accessModifiers.length);
87      }
88  
89      @Override
90      public int[] getDefaultTokens() {
91          return getAcceptableTokens();
92      }
93  
94      @Override
95      public int[] getAcceptableTokens() {
96          return new int[] {
97              TokenTypes.RECORD_DEF,
98          };
99      }
100 
101     @Override
102     public int[] getRequiredTokens() {
103         return getAcceptableTokens();
104     }
105 
106     @Override
107     public void visitToken(DetailAST ast) {
108         final AccessModifierOption accessModifier =
109             CheckUtil.getAccessModifierFromModifiersToken(ast);
110 
111         if (matchAccessModifiers(accessModifier)) {
112             final DetailAST recordComponents =
113                 ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
114             final int componentCount = countComponents(recordComponents);
115 
116             if (componentCount > max) {
117                 log(ast, MSG_KEY, componentCount, max);
118             }
119         }
120     }
121 
122     /**
123      * Method to count the number of record components in this record definition.
124      *
125      * @param recordComponents the ast to check
126      * @return the number of record components in this record definition
127      */
128     private static int countComponents(DetailAST recordComponents) {
129         return recordComponents.getChildCount(TokenTypes.RECORD_COMPONENT_DEF);
130     }
131 
132     /**
133      * Checks whether a record definition has the correct access modifier to be checked.
134      *
135      * @param accessModifier the access modifier of the record definition.
136      * @return whether the record definition matches the expected access modifier.
137      */
138     private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
139         return Arrays.stream(accessModifiers)
140                 .anyMatch(modifier -> modifier == accessModifier);
141     }
142 }