Skip to content

New check request: Annotation conflicts between record component and explicit accessor method #5177

Closed

Description

@kevinb9n

(I know some things I'm filing might be low-value to actually implement; but it's not for me to know which ones those are :-))

If a record component declaration includes any @Target(METHOD) annotations, and an explicit accessor method is provided, and that explicit accessor method does not repeat the same set of method-annotations, this is fishy at best. The decision to write the accessor method explicitly has "subtracted" annotations that would otherwise have been there.

Probably either:

  • the explicit method declaration should repeat the same annotations, or
  • for annotations that don't also include Target(RECORD_COMPONENT), then being listed on the record component at all is suspect

Activity

  1. kevinb9n commented on Jul 24, 2025

    @kevinb9n
    ContributorAuthor

    Oh, and technically something similar can happen with parameter annotations if there is an explicit and non-compact constructor.

  2. kevinb9n commented on Jul 24, 2025

    @kevinb9n
    ContributorAuthor

    The exception to this is @Override; it is a method-annotation but it has special semantics in JLS; see #5174 instead for that case.

  3. kevinb9n commented on Jul 28, 2025

    @kevinb9n
    ContributorAuthor

    One might also claim that if any annotations on the explicit accessor method have @Target(RECORD_COMPONENT), arguably they should be also listed on the corresponding component declaration. However, I can't say I have a strong sense of what the use cases for that @Target (and for RecordComponent.getAnnotations()) really are.

  4. eamonnmcmanus commented on Jul 29, 2025

    @eamonnmcmanus
    Member

    If an annotation that has only @Target(METHOD) is applied to a record component, and the component has an explicit accessor method, then the annotation has zero effect. If you wrote it you presumably intended it to mean something, so I think a warning is appropriate.

    I'm not seeing a similar argument for the case in the last comment. An annotation on an explicit accessor method does have an effect, and it doesn't seem obviously wrong not to have also applied the annotation to the corresponding record component even though you could have. [I also don't have a strong sense of what record component annotations are for.]

  5. cushon commented on May 4, 2026

    @cushon
    Collaborator

    I was reminded me of this bug by a related discussion about @Target(METHOD) on record components.

    I did some investigation into this and didn't find any occurrences of annotations with @Target(METHOD) on record components, where the record had an explicit accessor without the annotation. That was for annotations with targets including METHOD, not exclusively METHOD. I'm still planning to add a check for it.

  6. cushon commented on Nov 18, 2026

    @cushon
    Collaborator

    Implemented in commit 6c7ce38 (released in v2.50.0) as RecordComponentAccessorAnnotationConflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions