Skip to content

Google style: Override is required on explicitly declared accessor method for a record #17561

@mohitsatr

Description

@mohitsatr

I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

From: https://google.github.io/styleguide/javaguide.html#s6.1-override-annotation

A method is marked with the @Override annotation whenever it is legal. This includes ... and an explicitly declared accessor method for a record component

Code:

import java.util.Locale;

/** some javadoc. */
public record InputOverrideAlwaysUsedForRecord(String name, int age) {

  /** some javadoc. */
  public String name() { // Expected violation
    return name.toUpperCase(Locale.ROOT);
  }
}

record Container(String fileName, int port) {

  public int port() { // Expected violation
    return port;
  }
}

Cli:

$ java -jar checkstyle-10.26.1-all.jar -c google_checks.xml InputOverrideAlwaysUsedForRecord.java 
Starting audit...
Audit done.

Expected: two violations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions