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