Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring pull up fails on method member of inner record. #8375

Open
homberghp opened this issue Mar 31, 2025 · 0 comments
Open

refactoring pull up fails on method member of inner record. #8375

homberghp opened this issue Mar 31, 2025 · 0 comments
Labels
kind:bug Bug report or fix needs:triage Requires attention from one of the committers

Comments

@homberghp
Copy link
Contributor

homberghp commented Mar 31, 2025

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 <your.name at your.org>}
 */
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<R> 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

@homberghp homberghp added kind:bug Bug report or fix needs:triage Requires attention from one of the committers labels Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Bug report or fix needs:triage Requires attention from one of the committers
Projects
None yet
Development

No branches or pull requests

1 participant