spoon icon indicating copy to clipboard operation
spoon copied to clipboard

[Bug]: CtRecord.setTypeMembers adds method due to wrong polarity check

Open kevemueller opened this issue 1 month ago • 1 comments

Describe the bug

When using CtRecord.setTypeMembers, with an existing accessor method for a component, the implicit accessor is still created. This is the code snippet from CtRecordImpl in trunk:

@Override
	public <C extends CtType<Object>> C setTypeMembers(List<CtTypeMember> members) {
		super.setTypeMembers(members);
		for (CtRecordComponent component : components) {
			**if (hasMethodWithSameNameAndNoParameter(component)) {**
				addMethod(component.toMethod());
			}
			if (getField(component.getSimpleName()) == null) {
				addField(component.toField());
			}
		}
		return (C) this;
	}

The marked line should read:

if (!hasMethodWithSameNameAndNoParameter(component)) {

Other places in the file using this logic look fine.

Happy to open a PR if needed.

Source code you are trying to analyze/transform


Source code for your Spoon processing


Actual output


Expected output


Spoon Version

11.5.0

JVM Version

openjdk version "21.0.13" 2026-10-20 LTS

What operating system are you using?

MacOS X

kevemueller avatar Nov 03 '26 21:11 kevemueller

Hi @kevemueller, I have submitted PR #6987 to address this issue.

The fix corrects the inverted condition in CtRecordImpl#setTypeMembers() so that an implicit accessor is only generated when an explicit accessor does not already exist.

I've also added a regression test to verify that explicit record accessors are preserved.

The targeted test and the complete CtRecordTest suite pass successfully.

yashdotdev13 avatar Nov 17 '26 12:11 yashdotdev13