View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.annotation;
21  
22  import java.util.HashSet;
23  import java.util.Set;
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.utils.AnnotationUtil;
30  
31  /**
32   * <div>
33   * Verifies that explicitly declared record accessor methods include
34   * the {@code @Override} annotation.
35   * </div>
36   *
37   * <p>
38   * Per <a href="https://openjdk.org/jeps/395">JEP 395</a>, the meaning of the
39   * {@code @Override} annotation was extended to include explicitly declared
40   * accessor methods for record components.
41   * </p>
42   *
43   * <p>
44   * This check focuses only on record component accessor methods. It does not
45   * attempt to detect general method overrides from interfaces or superclasses,
46   * due to Checkstyle's single-file analysis limitations.
47   * </p>
48   *
49   * @since 13.1.0
50   */
51  @StatelessCheck
52  public class MissingOverrideOnRecordAccessorCheck extends AbstractCheck {
53  
54      /**
55       * A key is pointing to the warning message text in "messages.properties" file.
56       */
57      public static final String MSG_KEY = "annotation.missing.override.record.accessor";
58  
59      @Override
60      public int[] getDefaultTokens() {
61          return getRequiredTokens();
62      }
63  
64      @Override
65      public int[] getAcceptableTokens() {
66          return getRequiredTokens();
67      }
68  
69      @Override
70      public int[] getRequiredTokens() {
71          return new int[] {TokenTypes.METHOD_DEF};
72      }
73  
74      @Override
75      public void visitToken(DetailAST ast) {
76          if (isRecordAccessorMethod(ast) && !AnnotationUtil.hasOverrideAnnotation(ast)) {
77              log(ast, MSG_KEY);
78          }
79      }
80  
81      /**
82       * Checks if the given method is an explicitly declared accessor for a record component.
83       *
84       * @param ast the METHOD_DEF AST node
85       * @return true if this method is a record accessor
86       */
87      private static boolean isRecordAccessorMethod(DetailAST ast) {
88          boolean result = false;
89          final DetailAST grandParent = ast.getParent().getParent();
90          if (grandParent.getType() == TokenTypes.RECORD_DEF) {
91              final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
92              if (parameters.getChildCount() == 0) {
93                  final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText();
94                  result = getRecordComponentNames(grandParent).contains(methodName);
95              }
96          }
97          return result;
98      }
99  
100     /**
101      * Gets the set of record component names from a record definition.
102      *
103      * @param recordDef the RECORD_DEF AST node
104      * @return set of component names
105      */
106     private static Set<String> getRecordComponentNames(DetailAST recordDef) {
107         final Set<String> names = new HashSet<>();
108         final DetailAST recordComponents = recordDef.findFirstToken(TokenTypes.RECORD_COMPONENTS);
109         DetailAST child = recordComponents.getFirstChild();
110         while (child != null) {
111             if (child.getType() == TokenTypes.RECORD_COMPONENT_DEF) {
112                 names.add(child.findFirstToken(TokenTypes.IDENT).getText());
113             }
114             child = child.getNextSibling();
115         }
116         return names;
117     }
118 }