apache / apache/netbeans

Refactoring record parameters (change order etc.)

Open
#5,811 0 comments 0 reactions 0 assignees View on GitHub
Java kind:feature
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Description

Refactoring should be supported for records, too.

```java
public class RecordTest {
public static final record TestRecord(int id, String name) {
}

public static void main(final String[] args) {
new TestRecord(1, "foo");
}
}
```
When selecting _Refactor → Change method parameters_ in the context menu in the line containing `record`, the message “Cannot refactor this object.” appears.

Using the same menu entry in the line containing `new`, the _Change Method Parameters_ window appears, but when using it do change the order of the parameters, NetBeans (17) generates a new constructor inside the `record` which will not compile. The result is
```java
public class RecordTest {
public static final record TestRecord(int id, String name) {
public TestRecord(String name, int id) {
}
}

public static void main(final String[] args) {
new TestRecord("foo", 1);
}
}
```
and the compiler message is “error: constructor is not canonical, so its first statement must invoke another constructor of class TestRecord”. Removing parameters via the _Change Method Parameters_ window also generates a new invalid constructor.

### Use case/motivation

_No response_

### Related issues

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue in NetBeans 17 using the RecordTest example and the Change Method Parameters context-menu action on both the record declaration and constructor call. The work is done when changing or removing record parameters produces valid Java code and no longer reports that the constructor is non-canonical.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.