Skip to content
Permalink
Browse files
Remove support for JDK 1.7.
Also, no implied support for JDK 9 and above.
  • Loading branch information
leerho committed Jun 17, 2019
1 parent 1163725 commit b806cda8cf9bdcc26ee09a868f3e61903baaa9a1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 53 deletions.
@@ -51,7 +51,7 @@ public void force() {
capacityBytes);
} catch (final Exception e) {
throw new RuntimeException(String.format("Encountered %s exception in force. "
+ UnsafeUtil.tryIllegalAccessPermit, e.getClass()));
+ e.getClass()));
}
}
}
@@ -49,7 +49,6 @@ final class NioBits {

static {
try {
//TODO-JDK9 VM_CLASS = Class.forName("jdk.internal.misc.VM");
VM_CLASS = Class.forName("sun.misc.VM");
VM_MAX_DIRECT_MEMORY_METHOD =
VM_CLASS.getDeclaredMethod("maxDirectMemory");
@@ -86,8 +85,7 @@ final class NioBits {
nioBitsTotalCapacity = (AtomicLong) (totalCapacityField.get(null));

} catch (final Exception e) {
throw new RuntimeException("Could not acquire java.nio.Bits class: " + e.getClass()
+ UnsafeUtil.tryIllegalAccessPermit);
throw new RuntimeException("Could not acquire java.nio.Bits class: " + e.getClass());
}
}

@@ -255,12 +255,7 @@ public long getAndSetLong(final long offsetBytes, final long newValue) { //JDK 8
assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
final long addr = getCumulativeOffset(offsetBytes);
final long newValueReverseBytes = Long.reverseBytes(newValue);
if (UnsafeUtil.JDK8_OR_ABOVE) {
return Long.reverseBytes(unsafe.getAndSetLong(getUnsafeObject(), addr, newValueReverseBytes));
} else {
return Long.reverseBytes(
JDK7Compatible.getAndSetLong(getUnsafeObject(), addr, newValueReverseBytes));
}
return Long.reverseBytes(unsafe.getAndSetLong(getUnsafeObject(), addr, newValueReverseBytes));
}

@Override
@@ -33,21 +33,17 @@
* jdk8 and it must be done with both source and target versions of jdk7 specified in pom.xml.
* The resultant jar will work on jdk7 and jdk8.</p>
*
* <p>This may work with jdk9 but might require the JVM arg <i>-permit-illegal-access</i>,
* <i>–illegal-access=permit</i> or equivalent. Proper operation with jdk9 or above is not
* guaranteed and has not been tested.
*
* @author Lee Rhodes
*/
public final class UnsafeUtil {
public static final Unsafe unsafe;
public static final String JDK;
static final boolean JDK8_OR_ABOVE;
public static final String JDK; //must be at least "1.8"
public static final int JDK_MAJOR; //8, 9, 10, 11, 12, etc

//not an indicator of whether compressed references are used.
public static final int ADDRESS_SIZE;

//For 64-bit JVMs: varies depending on coop: 16 for JVM <= 32GB; 24 for JVM > 32GB
//For 64-bit JVMs: these offsets vary depending on coop: 16 for JVM <= 32GB; 24 for JVM > 32GB.
// Making this constant long-typed, rather than int, to exclude possibility of accidental overflow
// in expressions like arrayLength * ARRAY_BYTE_BASE_OFFSET, where arrayLength is int-typed.
// The same consideration for constants below: ARRAY_*_INDEX_SCALE, ARRAY_*_INDEX_SHIFT.
@@ -91,10 +87,6 @@ public final class UnsafeUtil {

//@formatter:on

static String tryIllegalAccessPermit =
" If using JDK 9+ try setting JVM arg -permit-illegal-access, "
+ "–illegal-access=permit or equivalent.";

static {
try {
final Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
@@ -109,7 +101,7 @@ public final class UnsafeUtil {
} catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("Unable to acquire Unsafe. " + tryIllegalAccessPermit, e);
throw new RuntimeException("Unable to acquire Unsafe. " + e);
}

//4 on 32-bit systems. 4 on 64-bit systems < 32GB, otherwise 8.
@@ -132,32 +124,35 @@ public final class UnsafeUtil {
final String jdkVer = System.getProperty("java.version");
final int[] p = parseJavaVersion(jdkVer);
JDK = p[0] + "." + p[1];
JDK8_OR_ABOVE = checkJavaVersion(JDK, p[0], p[1]);
JDK_MAJOR = (p[0] == 1) ? p[1] : p[0];
}

private UnsafeUtil() {}

/**
* Returns first two number groups of the java version string.
* @param jdkVer the java version string from System.getProperty("java.version").
* @return first two number groups of the java version string.
*/
static int[] parseJavaVersion(final String jdkVer) {
final int p0, p1;
try {
String[] parts = jdkVer.trim().split("[^0-9\\.]");
if (parts.length == 0) {
throw new ExceptionInInitializerError("Improper Java -version string: " + jdkVer);
}
parts = parts[0].split("\\.");
final int p0 = Integer.parseInt(parts[0]);
final int p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0;
return new int[] {p0, p1};
String[] parts = jdkVer.trim().split("[^0-9\\.]");//grab only number groups and "."
parts = parts[0].split("\\."); //split out the number groups
p0 = Integer.parseInt(parts[0]); //the first number group
p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0; //2nd number group, or 0
} catch (final Exception e) {
throw new ExceptionInInitializerError("Improper Java -version string: "
+ jdkVer + "\n" + e);
throw new ExceptionInInitializerError("Improper Java -version string: " + jdkVer + "\n" + e);
}
checkJavaVersion(jdkVer, p0, p1);
return new int[] {p0, p1};
}

static boolean checkJavaVersion(final String jdk, final int p0, final int p1) {
if ( (p0 < 1) || ((p0 == 1) && (p1 < 7)) ) {
throw new ExceptionInInitializerError("JDK Major Version must be >= 1.7: " + jdk);
static void checkJavaVersion(final String jdkVer, final int p0, final int p1) {
if ( (p0 < 1) || ((p0 == 1) && (p1 < 8)) || (p0 >= 9) ) {
throw new ExceptionInInitializerError(
"Unsupported JDK Major Version, must be 1.8: " + jdkVer);
}
return !( (p0 == 1) && (p1 == 7) );
}

static long getFieldOffset(final Class<?> c, final String fieldName) {
@@ -308,22 +308,14 @@ public void putShortArray(final long offsetBytes, final short[] srcArray,
public long getAndAddLong(final long offsetBytes, final long delta) { //JDK 8+
assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
final long addr = getCumulativeOffset(offsetBytes);
if (UnsafeUtil.JDK8_OR_ABOVE) {
return unsafe.getAndAddLong(getUnsafeObject(), addr, delta);
} else {
return JDK7Compatible.getAndAddLong(getUnsafeObject(), addr, delta);
}
return unsafe.getAndAddLong(getUnsafeObject(), addr, delta);
}

@Override
public long getAndSetLong(final long offsetBytes, final long newValue) { //JDK 8+
assertValidAndBoundsForWrite(offsetBytes, ARRAY_LONG_INDEX_SCALE);
final long addr = getCumulativeOffset(offsetBytes);
if (UnsafeUtil.JDK8_OR_ABOVE) {
return unsafe.getAndSetLong(getUnsafeObject(), addr, newValue);
} else {
return JDK7Compatible.getAndSetLong(getUnsafeObject(), addr, newValue);
}
return unsafe.getAndSetLong(getUnsafeObject(), addr, newValue);
}

@Override
@@ -20,7 +20,6 @@
package org.apache.datasketches.memory;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

@@ -54,16 +53,31 @@ public void checkJDK7methods() {
public void checkJdkString() {
String jdkVer;
int[] p = new int[2];
String[] good1_8Strings = {"1.8.0_121", "1.8.0_162", "9.0.4", "10.0.1", "11", "12b", "12_.2"};
String[] good1_8Strings = {"1.8.0_121", "1.8.0_162"};
int len = good1_8Strings.length;
for (int i = 0; i < len; i++) {
jdkVer = good1_8Strings[i];
p = UnsafeUtil.parseJavaVersion(jdkVer);
assertTrue(UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]));
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
int jdkMajor = (p[0] == 1) ? p[1] : p[0]; //model the actual JDK_MAJOR
if (p[0] == 1) { assertTrue(jdkMajor == p[1]); }
if (p[0] > 1 ) { assertTrue(jdkMajor == p[0]); }
}
try {
jdkVer = "9.0.4"; //ver 9 string
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
} catch (Error e) {
println("" + e);
}

try {
jdkVer = "1.7.0_80"; //1.7 string
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
} catch (Error e) {
println("" + e);
}
jdkVer = "1.7.0_80"; //1.7 string
p = UnsafeUtil.parseJavaVersion(jdkVer);
assertFalse(UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]));
try {
jdkVer = "1.6.0_65"; //valid string but < 1.7
p = UnsafeUtil.parseJavaVersion(jdkVer);

0 comments on commit b806cda

Please sign in to comment.