You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New check request: Annotation conflicts between record component and explicit accessor method #5177
(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
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.
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.]
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.
Activity
kevinb9n commentedon Jul 24, 2025
Oh, and technically something similar can happen with parameter annotations if there is an explicit and non-compact constructor.
kevinb9n commentedon Jul 24, 2025
The exception to this is
@Override; it is a method-annotation but it has special semantics in JLS; see #5174 instead for that case.kevinb9n commentedon Jul 28, 2025
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 forRecordComponent.getAnnotations()) really are.eamonnmcmanus commentedon Jul 29, 2025
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.]
cushon commentedon May 4, 2026
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 includingMETHOD, not exclusivelyMETHOD. I'm still planning to add a check for it.Detect conflicts between record components and explicit accessor methods
Detect conflicts between record components and explicit accessor methods
Detect conflicts between record components and explicit accessor methods
cushon commentedon Nov 18, 2026
Implemented in commit 6c7ce38 (released in v2.50.0) as
RecordComponentAccessorAnnotationConflict.