From 314acd3407771098b6e144814ce322184b1ecbeb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 9 Mar 2022 14:26:19 +0000 Subject: [PATCH] Add EIGHTEEN to JavaVersion Closes gh-29524 --- .../boot/system/JavaVersion.java | 10 +++++-- .../boot/system/JavaVersionTests.java | 27 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java index 17a635d17e..eedfe30b49 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.boot.system; import java.io.Console; import java.lang.invoke.MethodHandles; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -83,7 +84,12 @@ public enum JavaVersion { /** * Java 17. */ - SEVENTEEN("17", Console.class, "charset"); + SEVENTEEN("17", Console.class, "charset"), + + /** + * Java 18. + */ + EIGHTEEN("18", Duration.class, "isPositive"); private final String name; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java index 8bcbfd0414..83e61c447d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/system/JavaVersionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,13 @@ package org.springframework.boot.system; +import java.lang.reflect.Method; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledOnJre; import org.junit.jupiter.api.condition.JRE; +import org.junit.platform.commons.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -139,4 +143,25 @@ class JavaVersionTests { assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.SEVENTEEN); } + @Test + @EnabledIf(value = "isJava18") + void currentJavaVersionEighteen() { + assertThat(JavaVersion.getJavaVersion()).isEqualTo(JavaVersion.EIGHTEEN); + } + + boolean isJava18() throws Exception { + // Same detection mechanism as org.junit.jupiter.api.condition.JRE + // which does not have a JAVA_18 constant in JUnit 5.7.x + try { + Method versionMethod = Runtime.class.getMethod("version"); + Object version = ReflectionUtils.invokeMethod(versionMethod, null); + Method majorMethod = version.getClass().getMethod("major"); + int major = (int) ReflectionUtils.invokeMethod(majorMethod, version); + return major == 18; + } + catch (Exception ex) { + return false; + } + } + }