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

[NETBEANS-2349] Convert switch to switch expression #1193

Merged
merged 5 commits into from May 2, 2019

Conversation

@vikasprabhakar
Copy link
Contributor

commented Apr 10, 2019

Convert old switch statement to switch expression in following case:

Old code: (Assign same variable in every case with one statement)

int x = 5;
String s = "foo";
switch (x)

{ case 1: s = "bar";break; case 2: s = "baz";break; default: s = "zuq";break; }

After fix:

int x = 5;
String s = switch (x)

{ case 1 -> "bar"; case 2 -> "baz"; default -> "zuq"; }
;

com.sun.tools.javac.util.Name leftTreeName = null;
for (CaseTree ct : st.getCases()) {
List<StatementTree> statements = new ArrayList<>(ct.getStatements());
if (statements.isEmpty()) {

This comment has been minimized.

Copy link
@rtaneja1

rtaneja1 Apr 17, 2019

Contributor

Can store statements.size() as it's required more than once.

Name leftTreeName = null;
for (CaseTree ct : st.getCases()) {
List<StatementTree> statements = new ArrayList<>(ct.getStatements());
switch (statements.size()) {

This comment has been minimized.

Copy link
@rtaneja1

rtaneja1 Apr 30, 2019

Contributor

In case of more statements, we can create block with break value. This can be considered in separate enhancement.

@arusinha arusinha merged commit ac510a0 into apache:master May 2, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@vikasprabhakar vikasprabhakar deleted the vikasprabhakar:convert_to_switchexpression branch May 2, 2019

@stefanwendelmann

This comment has been minimized.

Copy link

commented Jun 20, 2019

I cant see the lightbulb for this, the hint is active and i made up a test like

        int i = 1;
        String s = "foo";
        switch (i) {
            case 1:
                s = "foo1";
                break;
            case 2:
                s = "foo3";
                break;
            default:
                s = "foo2";
                break;
     }

image

Product Version: Apache NetBeans IDE Dev (Build 20190619-5f7082f1d06e)
Java: 12.0.1; OpenJDK 64-Bit Server VM 12.0.1+12
Runtime: OpenJDK Runtime Environment 12.0.1+12
System: Windows 10 version 10.0 running on amd64; UTF8; de_DE (nb)
User directory: C:\Users\swendelmann\AppData\Roaming\NetBeans\dev
Cache directory: C:\Users\swendelmann\AppData\Local\NetBeans\Cache\dev

Edit: May this not work bc its a JDK13 JEP ?

@arusinha

This comment has been minimized.

Copy link
Contributor

commented Jun 20, 2019

u need to add --enable-preview in compiler VM args
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.