Skip to content

Conversation

@lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Mar 11, 2025

Consider code like:

package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {
        String str = switch (args.length) {
            case 0 -> {
                System.err.println("3");
                yield "";
            }
            default -> "";
        };
    }
    
}

Select these lines:

                System.err.println("3");
                yield "";

In the Alt-Enter popup, choose Introduce Method.... This leads to:

    public static void main(String[] args) {
        String s = "GTK-test";
        String str = switch (args.length) {
            case 0 -> {
                method();
            }
            default -> "";
        };
    }

    private static void method() {
        System.err.println("3");
        yield "";
    }

which is obviously wrong - it should be something like:

    public static void main(String[] args) {
        String s = "GTK-test";
        String str = switch (args.length) {
            case 0 -> {
                yield method();
            }
            default -> "";
        };
    }

    private static String method() {
        System.err.println("3");
        return "";
    }

This patch attempts to change the introduce hint to do the correct transformation.

closes #8278


PR approval and merge checklist:

  1. Was this PR correctly labeled, did the right tests run? When did they run?
  2. Is this PR squashed?
  3. Are author name / email address correct? Are co-authors correctly listed? Do the commit messages need updates?
  4. Does the PR title and description still fit after the Nth iteration? Is the description sufficient to appear in the release notes?

@lahodaj lahodaj added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Mar 11, 2025
@lahodaj lahodaj added this to the NB26 milestone Mar 11, 2025
@neilcsmith-net neilcsmith-net modified the milestones: NB26, NB27 Apr 16, 2025
@ebarboni ebarboni modified the milestones: NB27, NB28 Jul 23, 2025
@mbien
Copy link
Member

mbien commented Oct 8, 2025

would be good to rebase this one. Might be something for early NB 29? (freeze for NB28 is about a week away)

@mbien
Copy link
Member

mbien commented Oct 9, 2025

testPatternSwitch1: org.netbeans.api.java.source.gen.SwitchTest might have found something

@ebarboni ebarboni modified the milestones: NB28, NB29 Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce New Method refactoring with yield statement leads to broken code

4 participants