Skip to content

Refactoring class/record name duplicates switch "when" cases if the class/record is instantiated in those cases #8495

@mrj760

Description

@mrj760

Apache NetBeans version

Apache NetBeans 25

What happened

Pretty difficult to title this one or succinctly describe it. Easier to just look at the before/after...

...But basically if you have a switch case that uses "when" and there is an instantiation within it -- if you were to refactor the class name (of the thing being instantiated) the "when" clauses get duplicated.

Language / Project Type / NetBeans Component

Maven Application

How to reproduce

====

Example 1. Going to refactor A->B.

====

Before:
public class Test {
    record A() {
    }

    void method(Object o) {
        switch(o) {
            case Integer i when i == 0 -> {
                var r = new A();
            }
            default -> {
            }
        }
    }
}
After:
public class Test {
    record B() {
    }

    void method(Object o) {
        switch(o) {
            case Integer i when i == 0 when i == 0 -> {
                var r = new B();
            }
            default -> {
            }
        }
    }
}

====

Example 2. Same process, but with some more conditions in the when.

====

Before:
public class Test {
    record A() {
    }

    void method(Object o) {
        switch(o) {
            case Integer i when i == 0 || i == 0
                                || i == 0 -> {
                var r = new A();
            }
            default -> {
            }
        }
    }
}
After:
public class Test {
    record B() {
    }

    void method(Object o) {
        switch(o) {
            case Integer i when i == 0 || i == 0
                                || i == 0 when i == 0 || i == 0
                                || i == 0 -> {
                var r = new B();
            }
            default -> {
            }
        }
    }
}

Did this work correctly in an earlier version?

No / Don't know

Operating System

Win10

JDK

OpenJDK21

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

Just some lines from the IDE log that seemed relevant.
Saw these after doing Undo for the refactor.
I deleted lines that seemed irrelevant.

WARNING [null]: Last record repeated 2 more times.
INFO [org.netbeans.modules.parsing.impl.TaskProcessor]: Task: org.netbeans.modules.java.editor.semantic.SemanticHighlighter@65fd803a : class org.netbeans.modules.java.editor.semantic.SemanticHighlighter ignored cancel for 102 ms.
INFO [org.netbeans.modules.websvc.editor.hints.WebServicesRulesEngine]: Unknown element type: RECORD.
INFO [org.netbeans.modules.refactoring.spi.impl.ParametersPanel]: org.netbeans.modules.refactoring.api.AbstractRefactoring$ProgressL@625e1119 called start multiple times
INFO [null]: Last record repeated again.
WARNING [null]: Last record repeated again.
INFO [org.netbeans.modules.refactoring.spi.impl.ParametersPanel]: org.netbeans.modules.refactoring.api.RefactoringSession@424ae5ef called start multiple times
INFO [org.netbeans.modules.websvc.editor.hints.WebServicesRulesEngine]: Unknown element type: RECORD.
WARNING [null]: Last record repeated again.
INFO [org.netbeans.modules.websvc.editor.hints.WebServicesRulesEngine]: Unknown element type: RECORD.
WARNING [null]: Last record repeated again.
INFO [org.netbeans.modules.websvc.editor.hints.WebServicesRulesEngine]: Unknown element type: RECORD.
INFO [org.netbeans.modules.websvc.editor.hints.WebServicesRulesEngine]: Unknown element type: RECORD.

Are you willing to submit a pull request?

No

Activity

added
kind:bugBug report or fix
needs:triageRequires attention from one of the committers
on May 7, 2025
added
Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)
and removed
needs:triageRequires attention from one of the committers
on May 8, 2025
homberghp

homberghp commented on Jun 13, 2025

@homberghp
Contributor

I would like to pick this one up. It may be related to similar things as in #7044

homberghp

homberghp commented on Jun 13, 2025

@homberghp
Contributor

Most likely the issue is in CausalDiff.

added 2 commits that reference this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)kind:bugBug report or fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @mbien@homberghp@mrj760

      Issue actions

        Refactoring class/record name duplicates switch "when" cases if the class/record is instantiated in those cases · Issue #8495 · apache/netbeans