Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Java 21 guarded patterns in switch(Unexpected resukts using switch)
Java 21 guarded patterns in switch [message #1861800] Sat, 04 November 2023 11:25 Go to next message
Tom Sundquist is currently offline Tom SundquistFriend
Messages: 3
Registered: November 2023
Junior Member
I am trying out the switch statement with guarded pattern matching. For example
public static void foo( Object obj ) {
    switch ( obj ) {
        case null -> System.out.println( "NULL" );
        case Integer x when x > 0 -> System.out.println( "Positive Integer: " + x );
        case Integer x when x < 0 -> System.out.println( "Negative Integer: " + x );
        case Integer x -> System.out.println( "Integer: " + x );
        default -> System.out.println( "OTHER: " + obj );
    }
}

which I tested with
public static void main( String[] args ) {
    Stream.of( 0, -42, 42 ).forEach( n -> foo( n ) );
}

should produce
Integer: 0
Negative Integer: -42
Positive Integer: 42

but I get
Integer: 0
Integer: -42
Positive Integer: 42

NOTE: The results are correct when I remove the null case.

Am I doing something wrong here? I do get the expected results when I use Oracle's compiler.

I am running
Version: 2023-09 (4.29.0)
Build id: 20230907-1323
and I am using the Marketplace plugin for Java 21 support.

Tom
Re: Java 21 guarded patterns in switch [message #1861804 is a reply to message #1861800] Sat, 04 November 2023 16:08 Go to previous messageGo to next message
David M. Karr is currently offline David M. KarrFriend
Messages: 810
Registered: July 2009
Senior Member
And what happens when you run this from the command line, outside of Eclipse?
Re: Java 21 guarded patterns in switch [message #1861805 is a reply to message #1861804] Sat, 04 November 2023 18:18 Go to previous messageGo to next message
Tom Sundquist is currently offline Tom SundquistFriend
Messages: 3
Registered: November 2023
Junior Member
The byte code produced by eclipse produces the incorrect results when executed in eclipse and when run in the Oracle JVM. I get the correct results when I compile using Oracle's javac.

Further testing shows it's something with the null case. If I move the null case below the guarded case it works as expected. The same incorrect behavior happens with other types, for example
public static void foo( Object obj ) {
    switch ( obj ) {
        case null -> System.out.println( "NULL" );
        case Integer x when x > 0 -> System.out.println( "Positive Integer: " + x );
        case Integer x when x < 0 -> System.out.println( "Negative Integer: " + x );
        case Integer x -> System.out.println( "Integer: " + x );
        case String x when x.length() > 4 -> System.out.println( "Long String: " + x );
        case String x -> System.out.println( "String: " + x );
        default -> App.get().msg( "OTHER: " + obj );
    }
}

incorrectly matches integers and strings. Moving the null case below the integers gives correct matching for ints, but the strings still fail. Moving the null below the strings fixes them
Re: Java 21 guarded patterns in switch [message #1861806 is a reply to message #1861805] Sat, 04 November 2023 19:02 Go to previous messageGo to next message
David M. Karr is currently offline David M. KarrFriend
Messages: 810
Registered: July 2009
Senior Member
I'm having trouble making sense of that statement. What do you mean by "when run in the Oracle JVM"? Do you mean from the command line, or in Eclipse? Are you using the Oracle JVM within Eclipse? If you get the correct results when you use Oracle's javac, what compiler are you using when it doesn't work?

I can't tell for sure, but you might need to report this as an Oracle Java bug: https://bugreport.java.com/bugreport/ .
Re: Java 21 guarded patterns in switch [message #1861807 is a reply to message #1861806] Sat, 04 November 2023 19:39 Go to previous messageGo to next message
Tom Sundquist is currently offline Tom SundquistFriend
Messages: 3
Registered: November 2023
Junior Member
Thank you for your replies David. I appreciate your help, and I apologize for the hasty and confusing response. Here's a more complete description:

On my Linux Mint system I have a JRE from Oracle:

$ javac --version
javac 21.0.1

$ java --version
java 21.0.1 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)

It is my understanding that eclipse does not use this compiler, although I don't know about the JVM that it uses. My version of eclipse, Version: 2023-09 (4.29.0), does not actually have support for the Java 21 execution environment (I think it is coming in December?) so I have installed a Marketplace plugin for the Java 21 support.

When I use the command line compiler I get correct behavior from the switch pattern matching, but code compiled in eclipse does not properly handle guarded patterns (assuming I have the correct interpretation of correct behavior.)

I'm new to the Eclipse Community Forums and this is my first time using the Marketplace, so I wasn't sure where to raise this issue.

Best,
Tom
Re: Java 21 guarded patterns in switch [message #1861809 is a reply to message #1861807] Sun, 05 November 2023 09:41 Go to previous message
Erik BrangsFriend
Messages: 55
Registered: February 2010
Member
Eclipse uses the Eclipse Compiler for Java (ECJ), which is provided by JDT.

The JVM that is used to execute code depends on what is configured for your project. Eclipses uses "Execution Environments" (e.g. something like "Java-SE21") which are mapped to installed JDKs and/or JREs. You can find the global setttings in the preferences dialog (e.g. Window -> Preferences) under Java -> Installed JREs and Java -> Installed JREs -> Execution Environments.

If the code isn't working correctly when compiled by ECJ, it could be a bug in ECJ. If you're considering reporting it, you could take a look at JDT's Github organization at https://github.com/eclipse-jdt .

There are also development builds of Eclipse that you could try to see if the bug is already fixed.
Previous Topic:Restricted Identifier Error in Eclipse when using Mockitos when
Next Topic:import existing Maven projects: remove root directory suggestion
Goto Forum:
  


Current Time: Sat Jan 18 18:50:27 GMT 2025

Powered by FUDForum. Page generated in 0.04339 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top