apache / apache/netbeans

refactoring pull up fails on method member of inner record.

Open
#8,375 1 comment 0 reactions 0 assignees View on GitHub
kind:bug needs:triage
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 25

### What happened

In the following code, the method defined inside the inner record is not pulled up with its enclosing record.

```
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package recordast;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;

/**
*
* @author homberghp (Pieter van den Hombergh) {@code }
*/
class School extends Institute {

Student[] students;
Employee[] teachers;

School(Student... students) {
assert students != null;
this.students = students;
}

record Teacher(String name, String... topics) implements Serializable, Cloneable {

}

record Student(int id, String name, LocalDate dob, String... grades) implements Serializable {

Student {
// ensure valid id
assert id > 0;
assert name != null && !name.isBlank();
assert dob.isAfter(LocalDate.EPOCH);
}

String gradesAsString() {
return Arrays.toString(grades);
}
}

class Employee implements Serializable {

private final int id;
private final String name;
private final LocalDate dob;

Employee(int id, String name, LocalDate dob, R role) {
assert id > 0;
assert name != null;
assert dob.isAfter(LocalDate.EPOCH);
this.id = id;
this.name = name;
this.dob = dob;
}

@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + this.id;
hash = 71 * hash + Objects.hashCode(this.name);
hash = 71 * hash + Objects.hashCode(this.dob);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
if (this.id != other.id) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return Objects.equals(this.dob, other.dob);
}

@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + ", dob=" + dob + '}';
}

}

public static void main(String[] args) {
Student jan = new Student(123, "Jan", LocalDate.now(), "A", "D+");
School s = new School(jan);
System.out.println("s = " + s);
System.out.println("jan = " + jan + "grades" + jan.gradesAsString());
}
}
```
and
```
class Institute {

}
```
The problem may ly in the improper handling of the compact constructor, which may not contain a super
call.

### Language / Project Type / NetBeans Component

java refactoring (java/refactoring.java)

### How to reproduce

Try pull up e.g. Student member to super class Institute.

The compact constructor is miss formed.

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

linux ubuntu 24.04 LTS

### JDK

jdk 17 (the build platform for netbeans 25)

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

The problem occurs consistently.
I found the error while extending the tests on PR 8374
The test is on my branch issue7044a in my repo homberghp/netbeans.

If hope to work on a solution, because I may be the culprit.

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

Yes

Contributor guide

Open the contributing guide

Research direction

Start with the Java refactoring pull-up operation and reproduce the failure by pulling the Student member to Institute. Review the compact-constructor handling and the test on branch issue7044a from PR 8374. Done means the inner record and its gradesAsString method are pulled up correctly without treating the compact constructor as malformed.

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.