Skip to content

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

Open
@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

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

    Development

    No branches or pull requests

    Issue actions