Java Preview Features
by?Maksim Shelkovich,?Java Engineer?at?InterLogic
Even before first public release in March 1995, Java community continuously collecting feedbacks to deliver that software developers have told them they need. One of the processes to achieve that is a Preview Feature, specified in JEP 12.
A preview feature is a new feature whose design, specification, and implementation are complete, but which is not permanent, which means that the feature may exist in a different form or not at all in future JDK releases.
The Demo Application
With Java 17 release become available?"JEP 406: Pattern Matching for switch (Preview)", so we can write something like this:
public class EnablePreviewFeatureDemo {
public static void main(String[] args) {
System.out.println(formatterPatternSwitch(11));
}
static String formatterPatternSwitch(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
default -> o.toString();
};
}
}
In demo purpose, the class above listed will be placed in a project with the following structure:?/demo-enable-preview-features/src/main/java/com/tutrit/demo/EnablePreviewFeatureDemo.java
The Demo Application could be used as a validator of enabling/disabling preview feature. It could be found at:?https://github.com/tutrit/demo-enable-preview-features?
Enable Preview Feature in Java Compiler
Preview features enables by passing 2 arguments to compiler:?enable-preview?and?source?or?release.
In order to compile and run java code with preview features, the source/release argument must be the same for JDK release version of the compiler and JVM version.
Since the Demo Application is just one java class, it could be run from?demo-enable-preview-features/src/main/java?directory with the following command:
java --enable-preview --source 17 com/tutrit/demo/EnablePreviewFeatureDemo.java
Another way is to compile it first with?source?or?release?flag:
javac --enable-preview --source 17 com/tutrit/demo/EnablePreviewFeatureDemo.java
javac --enable-preview --relese 17 com/tutrit/demo/EnablePreviewFeatureDemo.java
And then run with JVM without second argument:
java --enable-preview com.tutrit.demo.EnablePreviewFeatureDemo
Enable Preview Feature in IntelliJ IDEA
The Demo Application won't compile in IntelliJ by default with a message that?Patterns in switch are not supported at language level '17'?and recommendation to?Set language level to 17 (Preview) - Pattern matching for switch. It could be done from?File -> Project Structure -> Project -> Project language level
Enable Preview Feature with Maven in pom.xml
In order to enable Preview Feature with Maven,?Maven Compiler Plugin?should be included in build script file?pom.xml. It is used to provide language version and compiler arguments. Here is the configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
Because the Demo Application doesn't specify main class due to make it as small as possible, to run it after regular packaging with?mvn package, alongside with?enable-preview?argument a class path should be specified in the run command:
java --enable-preview -cp demo-0.0.1-SNAPSHOT.jar com.tutrit.demo.EnablePreviewFeatureDemo
Enable Preview Feature with Gradle in gradle.build
In order to enable Preview Feature with Gradle, 3 tasks should be included in build script file?gradle.build?to provide arguments to compiler and JVM:
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec) {
jvmArgs += "--enable-preview"
}
Because the Demo Application doesn't specify main class due to make it as small as possible, to run it after regular packaging with?gradle jar, alongside with?enable-preview?argument a class path should be specified in the run command:
java --enable-preview -cp demo-0.0.1-SNAPSHOT.jar com.tutrit.demo.EnablePreviewFeatureDemo