Skip to content

Commit

Permalink
[java-preview-features] Upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroomes committed Aug 3, 2024
1 parent ac55bc7 commit 1cd74d5
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The subprojects include:

### `java-preview-features/`

This subproject shows how to configure Gradle to build and run a project that uses Java [_Preview Features_](https://openjdk.java.net/jeps/12).
Configure Gradle to build and run a project that uses Java [_Preview Features_](https://openjdk.java.net/jeps/12).

See the README in [java-preview-features/](java-preview-features/).

Expand Down
29 changes: 12 additions & 17 deletions java-preview-features/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# java-preview-features

This project shows how to configure Gradle to build and run a project that uses Java [_Preview Features_](https://openjdk.java.net/jeps/12).
Configure Gradle to build and run a project that uses Java [_Preview Features_](https://openjdk.java.net/jeps/12).

---

This repo in particular showcases the [Pattern Matching for `switch`](https://openjdk.org/jeps/433) preview language
feature in Java 20. Preview features are hidden behind the `--enable-preview` Java flag.
## Overview

This repo in particular showcases the [Unnamed Patterns and Variables](https://openjdk.org/jeps/443) preview language
feature in Java 21. Preview features are hidden behind the `--enable-preview` Java flag.

Please note: Intellij won't always support preview features!

Expand All @@ -14,31 +15,27 @@ Please note: Intellij won't always support preview features!

Follow these instructions to build and run the demo program.

1. Use Java 17
2. Make sure that Java 20 is installed in a location known to Gradle
3. Build and run the program:
1. Pre-requisite: Java 21
2. Build and run the program:
* ```shell
./gradlew run
```
* It should output the following.
```text
> Task :compileJava
Note: /Users/dave/repos/personal/gradle-playground/java-preview-features/src/main/java/dgroomes/App.java uses preview features of Java SE 20.
Note: /Users/dave/repos/personal/gradle-playground/java-preview-features/src/main/java/dgroomes/java_preview_features/App.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
> Task :run
It's Earth, an instance of Planet. Its atmosphere is: mostly nitrogen.
It's Earth, an instance of Planet. Its atmosphere is: mostly nitrogen.
It's Mars, an instance of Planet. Its atmosphere is: carbon dioxide
It's an instance of Star. It says: I shine bright!
It's an instance of Star. It says: I shine bright!
Found 631 time zones
```
Notice the warning:
> Note: /...omitted.../App.java uses preview features of Java SE 20.
> Note: /...omitted.../App.java uses preview features of Java SE 21.

There is no way to suppress this because Java's preview features are designed to allow breaking changes in future
releases, so the compile-time reminder is a welcome one.
4. Run the tests:
3. Run the tests:
* ```shell
./gradlew test
```
Expand All @@ -48,7 +45,5 @@ Follow these instructions to build and run the demo program.
General clean-ups, TODOs and things I wish to implement for this project:
* [ ] When Gradle officially supports running on Java 20, upgrade to Java 20 for "use this Java version" because this
subproject is not designed to showcase the "toolchains for Java" feature of Gradle.
* [x] DONE (I could still use the planet types very effectively thanks to "pattern matching for switch") Upgrade to Java 20 and use a Java 20 preview feature. This will require throwing away the planet design for something
else.
10 changes: 2 additions & 8 deletions java-preview-features/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ repositories {
mavenCentral()
}

val junitVersion = "5.8.1" // releases: https://junit.org/junit5/docs/current/release-notes/index.html
val junitVersion = "5.10.3" // releases: https://junit.org/junit5/docs/current/release-notes/index.html

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(20))
}
}

/**
* Configure the compiler task, test task, start script creation task, and the run task to enable Java language "Preview
* Features"
Expand All @@ -44,5 +38,5 @@ tasks {
}

application {
mainClass.set("dgroomes.App")
mainClass.set("dgroomes.java_preview_features.App")
}
Binary file modified java-preview-features/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
24 changes: 16 additions & 8 deletions java-preview-features/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,13 +80,10 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,22 +130,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -193,6 +197,10 @@ if "$cygwin" || "$msys" ; then
done
fi


# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
Expand Down
1 change: 1 addition & 0 deletions java-preview-features/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand Down
31 changes: 0 additions & 31 deletions java-preview-features/src/main/java/dgroomes/App.java

This file was deleted.

9 changes: 0 additions & 9 deletions java-preview-features/src/main/java/dgroomes/Celestial.java

This file was deleted.

9 changes: 0 additions & 9 deletions java-preview-features/src/main/java/dgroomes/Earth.java

This file was deleted.

9 changes: 0 additions & 9 deletions java-preview-features/src/main/java/dgroomes/Mars.java

This file was deleted.

6 changes: 0 additions & 6 deletions java-preview-features/src/main/java/dgroomes/Planet.java

This file was deleted.

8 changes: 0 additions & 8 deletions java-preview-features/src/main/java/dgroomes/Star.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dgroomes.java_preview_features;

import java.util.TimeZone;

public class App {

public static void main(String[] args) {
var count = new App().countTimezones();
System.out.printf("Found %d time zones%n", count);
}

public int countTimezones() {
int count = 0;

// This contrived example shows the use of the "Unnamed patterns and variables" preview language feature. We use
// "_" in place of a variable name. This is a convenient way to not have to come up with a name, and to tell
// tooling (linters, IDEs) that we indeed did not intend to use the variable.
for (var _ : TimeZone.getAvailableIDs()) {
count++;
}

return count;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dgroomes;
package dgroomes.java_preview_features;

import org.junit.jupiter.api.Test;

Expand All @@ -10,6 +10,6 @@ class AppTest {
void message() {
var app = new App();

assertNotNull(app.randomCelestialObject());
assertNotNull(app.countTimezones());
}
}

0 comments on commit 1cd74d5

Please sign in to comment.