Features

Support for Java 13 Preview Features in IntelliJ IDEA 2019.2

Java 13 is planned for release on September 17, 2019. And IntelliJ IDEA is already getting ready for it! Starting with version 2019.2, IntelliJ IDEA has support for Java 13 Preview features.

Support for Switch Expressions preview feature

As you may already know, the Java 12 preview significantly improved the handling of the traditional switch statement. Java 13 offers a second preview of switch expressions to drop the “break with value” statement in favor of “yield” (JEP 354).

Here is an example of yielding the values from a switch expression:

import java.time.DayOfWeek;

class Sample {

    private int workWeekDays(DayOfWeek s) {
        return switch (s) {
            case MONDAY:
                yield 1;
            case TUESDAY:
                yield 2;
            case WEDNESDAY:
                yield 3;
            case THURSDAY:
                yield 4;
            case FRIDAY:
            default:
                System.out.println("Seems that the selected day is weekend");
                yield 0;
        };
    }

}

A switch expression must either complete normally with a value or complete abruptly by throwing an exception. In the cases where you miss yielding a value, IntelliJ IDEA will underline the “switch” keyword and throw an error message indicating that the result is not produced in all execution paths.

MissingYield

Support for Text Blocks preview feature

One more preview feature targeting JDK 13 is text blocks (JEP 355). A text block is a new way to embed a multiline snippet into the Java source code. It enhances readability and gives the developer control over the format.

Let’s take a closer look at text blocks. Up to now a snippet of HTML, XML, SQL, etc. code embedded in a string literal was hard-to-read, not easily editable, and error-prone. The new “two-dimensional” text blocks syntax allows writing the same lines of code without any cumbersome or excessive complexity.
IntelliJ IDEA now understands the syntax of preview text blocks and provides correct highlighting for it.

Compare

IntelliJ IDEA 2019.2 supports most of the features applicable to normal string literals also for text blocks, though it’s a work in progress and more goodies can be expected. Now you can, for example, try the ‘Join concatenated String literal’ quick-fix or check the constant value of the concatenation.

TextBlocksIntentions

The IDE will correctly highlight the code with skipped line terminator between opening and closing delimiters ( “”” ), giving an error message that the new line is missing after an opening quote.

IllegalTextBlock

As you may know, working with string literals can be made simpler with IntelliJ IDEA’s language injection features. They provide comprehensive language assistance while you edit the fragments of injected code.

For more details please refer to https://www.jetbrains.com/help/idea/using-language-injections.html

How to enable support for Java 13 preview features
To enable this support, set the corresponding Language Level in the Project Structure settings and install the JDK to compile.
At the time this blog-post is written, Java 13 is still not released, so only early access builds are available and their stability, quality, and security are not guaranteed.
JDK 13 Early-Access Builds are available through this link: https://jdk.java.net/13.

IntelliJ IDEA policy of supporting Java preview versions
IntelliJ IDEA is committed to supporting the preview features from the latest Java release and, if possible, the preview features of whatever release is coming next. So v2019.2 will support Java 12 and 13 preview features. Please note that v2019.3 will drop the support for Java 12 preview features as IntelliJ IDEA 2019.3 will be released after the release of Java 13.

Your feedback on the new IntelliJ IDEA features is always welcome!

Happy Developing!

image description