New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite JavaVersion #15372
Merged
Merged
Rewrite JavaVersion #15372
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 0 additions & 52 deletions
52
...c/main/java/com/hazelcast/internal/usercodedeployment/impl/ClassloadingMutexProvider.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
hazelcast/src/main/java/com/hazelcast/internal/util/JavaMajorVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hazelcast.internal.util; | ||
|
||
/** | ||
* Interface used by {@link JavaVersion} and | ||
* {@link JavaVersion.FutureJavaVersion}. This interface is needed only | ||
* to do version comparison safely on runtime environments with versions | ||
* not listed in {@link JavaVersion}. | ||
*/ | ||
interface JavaMajorVersion { | ||
/** | ||
* Returns the major version. | ||
* | ||
* @return the major version | ||
*/ | ||
int getMajorVersion(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,28 +16,52 @@ | |
|
||
package com.hazelcast.internal.util; | ||
|
||
import com.hazelcast.logging.ILogger; | ||
import com.hazelcast.logging.Logger; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Utility for checking runtime Java version. | ||
* | ||
* <p> | ||
* This class relies on the {@code java.lang.Runtime.Version.major()} method | ||
* available since Java 9, therefore it's accessed via reflection. If | ||
* {@code java.lang.Runtime.Version} is not present, we treat the runtime | ||
* environment as Java 8. Also, if there is any exception thrown during the | ||
* reflective access, we fall back to Java 8. | ||
* <p> | ||
* Since we rely on a public Java API returning the major Java version | ||
* version comparisons can be done safely even with versions that didn't | ||
* exist at the time the given Hazelcast version was released. | ||
* See {@link FutureJavaVersion). | ||
*/ | ||
public enum JavaVersion { | ||
UNKNOWN, | ||
JAVA_1_6, | ||
JAVA_1_7, | ||
JAVA_1_8, | ||
JAVA_9, | ||
JAVA_10, | ||
JAVA_11, | ||
JAVA_12; | ||
|
||
private static final JavaVersion CURRENT_VERSION = detectCurrentVersion(); | ||
public enum JavaVersion implements JavaMajorVersion { | ||
JAVA_8(8), | ||
JAVA_9(9), | ||
JAVA_10(10), | ||
JAVA_11(11), | ||
JAVA_12(12), | ||
JAVA_13(13), | ||
JAVA_14(14); | ||
|
||
private static final JavaMajorVersion CURRENT_VERSION = detectCurrentVersion(); | ||
|
||
private int majorVersion; | ||
|
||
JavaVersion(int majorVersion) { | ||
this.majorVersion = majorVersion; | ||
} | ||
|
||
@Override | ||
public int getMajorVersion() { | ||
return majorVersion; | ||
} | ||
|
||
/** | ||
* Check if the current runtime version is at least the given version. | ||
* | ||
* @param version version to be compared against the current runtime version | ||
* @return Return true if current runtime version of Java is the same or greater than given version. | ||
* When the passed version is {@link #UNKNOWN} then it always returns true. | ||
*/ | ||
public static boolean isAtLeast(JavaVersion version) { | ||
return isAtLeast(CURRENT_VERSION, version); | ||
|
@@ -53,44 +77,83 @@ public static boolean isAtMost(JavaVersion version) { | |
return isAtMost(CURRENT_VERSION, version); | ||
} | ||
|
||
private static JavaVersion detectCurrentVersion() { | ||
String version = System.getProperty("java.version"); | ||
return parseVersion(version); | ||
static boolean isAtLeast(JavaMajorVersion currentVersion, JavaMajorVersion minVersion) { | ||
return currentVersion.getMajorVersion() >= minVersion.getMajorVersion(); | ||
} | ||
|
||
static JavaVersion parseVersion(String version) { | ||
if (version == null) { | ||
// this should not happen but it's better to stay on the safe side | ||
return UNKNOWN; | ||
} | ||
JavaVersion result = UNKNOWN; | ||
if (version.startsWith("1.")) { | ||
String withoutMajor = version.substring(2, version.length()); | ||
if (withoutMajor.startsWith("6")) { | ||
result = JAVA_1_6; | ||
} else if (withoutMajor.startsWith("7")) { | ||
result = JAVA_1_7; | ||
} else if (withoutMajor.startsWith("8")) { | ||
result = JAVA_1_8; | ||
static boolean isAtMost(JavaMajorVersion currentVersion, JavaMajorVersion maxVersion) { | ||
return currentVersion.getMajorVersion() <= maxVersion.getMajorVersion(); | ||
} | ||
|
||
private static JavaVersion valueOf(int majorVersion) { | ||
for (JavaVersion version : values()) { | ||
if (version.majorVersion == majorVersion) { | ||
return version; | ||
} | ||
} else if (version.startsWith("9")) { | ||
// from version 9 the string does not start with "1." | ||
result = JAVA_9; | ||
} else if (version.startsWith("10")) { | ||
result = JAVA_10; | ||
} else if (version.startsWith("11")) { | ||
result = JAVA_11; | ||
} else if (version.startsWith("12")) { | ||
result = JAVA_12; | ||
} | ||
return result; | ||
|
||
return null; | ||
} | ||
|
||
static boolean isAtLeast(JavaVersion currentVersion, JavaVersion minVersion) { | ||
return currentVersion.ordinal() >= minVersion.ordinal(); | ||
private static JavaMajorVersion detectCurrentVersion() { | ||
final ILogger logger = Logger.getLogger(JavaVersion.class); | ||
final Class runtimeClass; | ||
final Class versionClass; | ||
|
||
try { | ||
runtimeClass = Class.forName("java.lang.Runtime"); | ||
} catch (ClassNotFoundException e) { | ||
logger.warning("Unable to detect Java version, falling back to Java 8", e); | ||
return JAVA_8; | ||
} | ||
|
||
try { | ||
versionClass = Class.forName("java.lang.Runtime$Version"); | ||
} catch (ClassNotFoundException e) { | ||
// if Runtime is present but Runtime.Version doesn't, it's Java8 for sure | ||
logger.info("Detected runtime version: Java 8"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reduce logging level to |
||
return JAVA_8; | ||
} | ||
|
||
try { | ||
Method versionMethod = runtimeClass.getDeclaredMethod("version"); | ||
Object versionObj = versionMethod.invoke(Runtime.getRuntime()); | ||
Method majorMethod = versionClass.getDeclaredMethod("major"); | ||
int majorVersion = (int) majorMethod.invoke(versionObj); | ||
|
||
logger.info("Detected runtime version: Java " + majorVersion); | ||
JavaVersion foundVersion = valueOf(majorVersion); | ||
if (foundVersion != null) { | ||
return foundVersion; | ||
} | ||
|
||
return new FutureJavaVersion(majorVersion); | ||
|
||
} catch (Exception e) { | ||
logger.warning("Unable to detect Java version, falling back to Java 8", e); | ||
return JAVA_8; | ||
} | ||
} | ||
|
||
static boolean isAtMost(JavaVersion currentVersion, JavaVersion minVersion) { | ||
return currentVersion.ordinal() <= minVersion.ordinal(); | ||
/** | ||
* Represents a future Java version that has not yet added to the | ||
* {@link JavaVersion} enum. This class allows comparison of known | ||
* versions against future, not yet listed (in the enum) Java versions, | ||
* making {@link JavaVersion#isAtLeast(JavaVersion)} and | ||
* {@link JavaVersion#isAtMost(JavaVersion)} methods usable in this | ||
* case too. | ||
*/ | ||
static class FutureJavaVersion implements JavaMajorVersion { | ||
private final int majorVersion; | ||
|
||
FutureJavaVersion(int majorVersion) { | ||
this.majorVersion = majorVersion; | ||
} | ||
|
||
@Override | ||
public int getMajorVersion() { | ||
return majorVersion; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need reflection here, I think. The
Runtime
is part of Java from its beginning.Runtime.class
should do the job.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Of course :) Fixed.