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

Pattern Matching for switch in Java 21 / JEP 411 #306

Open
matsev opened this issue Sep 26, 2023 · 0 comments
Open

Pattern Matching for switch in Java 21 / JEP 411 #306

matsev opened this issue Sep 26, 2023 · 0 comments
Labels
enhancement New feature or request java 21+ parser-java recipe Recipe requested

Comments

@matsev
Copy link

matsev commented Sep 26, 2023

What problem are you trying to solve?

Add recipe for JEP 441: Pattern Matching for switch to improve Java 21 migration (and possibly also for the related JEP 440: Record Patterns).

Describe the solution you'd like

The two examples copied from the JEP 441: Pattern Matching for switch project (there are more):

instanceof

// Prior to Java 21
static String formatter(Object obj) {
    String formatted = "unknown";
    if (obj instanceof Integer i) {
        formatted = String.format("int %d", i);
    } else if (obj instanceof Long l) {
        formatted = String.format("long %d", l);
    } else if (obj instanceof Double d) {
        formatted = String.format("double %f", d);
    } else if (obj instanceof String s) {
        formatted = String.format("String %s", s);
    }
    return formatted;
}
// As of Java 21
static String formatterPatternSwitch(Object obj) {
    return switch (obj) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> obj.toString();
    };
}

Switches and null

// Prior to Java 21
static void testFooBarOld(String s) {
    if (s == null) {
        System.out.println("Oops!");
        return;
    }
    switch (s) {
        case "Foo", "Bar" -> System.out.println("Great");
        default           -> System.out.println("Ok");
    }
}
// As of Java 21
static void testFooBarNew(String s) {
    switch (s) {
        case null         -> System.out.println("Oops");
        case "Foo", "Bar" -> System.out.println("Great");
        default           -> System.out.println("Ok");
    }
}

Have you considered any alternatives or workarounds?

no

Additional context

Are you interested in contributing this feature to OpenRewrite?

Not at the moment

@matsev matsev added the enhancement New feature or request label Sep 26, 2023
@timtebeek timtebeek added recipe Recipe requested java 21+ labels Sep 26, 2023
@timtebeek timtebeek changed the title Feature request: Java 21 / JEP 411 Pattern Matching for switch in Java 21 / JEP 411 Sep 26, 2023
@timtebeek timtebeek moved this to Recipes Wanted in OpenRewrite Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request java 21+ parser-java recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants