From c721fa322f448976355951504525353d36bd6c36 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 18 Oct 2023 15:00:10 +0200 Subject: [PATCH] move to JDK17 (#2827) * move to JDK17 * Update pom.xml Co-authored-by: otbutz * simplify cleanMappedByteBuffer --------- Co-authored-by: otbutz --- .github/workflows/publish-maven-central.yml | 2 +- README.md | 2 +- .../graphhopper/storage/MMapDataAccess.java | 90 +++---------------- .../java/com/graphhopper/util/Constants.java | 8 -- .../java/com/graphhopper/util/GitInfo.java | 2 +- .../EdgeBasedRoutingAlgorithmTest.java | 2 +- .../routing/RandomCHRoutingTest.java | 2 +- pom.xml | 5 +- .../java/com/graphhopper/util/Helper.java | 14 --- 9 files changed, 20 insertions(+), 107 deletions(-) diff --git a/.github/workflows/publish-maven-central.yml b/.github/workflows/publish-maven-central.yml index 74779486322..a8c9bd8fe5b 100644 --- a/.github/workflows/publish-maven-central.yml +++ b/.github/workflows/publish-maven-central.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-java@v3 with: - java-version: 8 + java-version: 17 distribution: temurin server-id: ossrh server-username: MAVEN_USERNAME diff --git a/README.md b/README.md index a6d6b80adbb..748563ee65c 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ To get started you can try [GraphHopper Maps](README.md#graphhopper-maps), read ## Installation -To install the [GraphHopper Maps](https://graphhopper.com/maps/) UI and the web service locally you [need a JVM](https://adoptium.net) (>= Java 8) and do: +To install the [GraphHopper Maps](https://graphhopper.com/maps/) UI and the web service locally you [need a JVM](https://adoptium.net) (>= Java 17) and do: ```bash wget https://repo1.maven.org/maven2/com/graphhopper/graphhopper-web/8.0/graphhopper-web-8.0.jar https://raw.githubusercontent.com/graphhopper/graphhopper/8.x/config-example.yml http://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf diff --git a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java index 6424f88eef1..ca2b15747eb 100644 --- a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java @@ -17,7 +17,6 @@ */ package com.graphhopper.storage; -import com.graphhopper.util.Constants; import com.graphhopper.util.Helper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,8 +55,6 @@ */ public final class MMapDataAccess extends AbstractDataAccess { - private static final Logger LOGGER = LoggerFactory.getLogger(MMapDataAccess.class); - private final boolean allowWrites; private RandomAccessFile raFile; private final List segments = new ArrayList<>(); @@ -67,74 +64,25 @@ public final class MMapDataAccess extends AbstractDataAccess { this.allowWrites = allowWrites; } - public static boolean jreIsMinimumJava9() { - final StringTokenizer st = new StringTokenizer(System.getProperty("java.specification.version"), "."); - int JVM_MAJOR_VERSION = Integer.parseInt(st.nextToken()); - int JVM_MINOR_VERSION; - if (st.hasMoreTokens()) { - JVM_MINOR_VERSION = Integer.parseInt(st.nextToken()); - } else { - JVM_MINOR_VERSION = 0; - } - return JVM_MAJOR_VERSION > 1 || (JVM_MAJOR_VERSION == 1 && JVM_MINOR_VERSION >= 9); - } - public static void cleanMappedByteBuffer(final ByteBuffer buffer) { // TODO avoid reflection on every call try { AccessController.doPrivileged(new PrivilegedExceptionAction() { @Override public Object run() throws Exception { - if (jreIsMinimumJava9()) { - // >=JDK9 class sun.misc.Unsafe { void invokeCleaner(ByteBuffer buf) } - final Class unsafeClass = Class.forName("sun.misc.Unsafe"); - // fetch the unsafe instance and bind it to the virtual MethodHandle - final Field f = unsafeClass.getDeclaredField("theUnsafe"); - f.setAccessible(true); - final Object theUnsafe = f.get(null); - final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class); - try { - method.invoke(theUnsafe, buffer); - return null; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - if (buffer.getClass().getSimpleName().equals("MappedByteBufferAdapter")) { - if (!Constants.ANDROID) - throw new RuntimeException("MappedByteBufferAdapter only supported for Android at the moment"); - - // For Android 4.1 call ((MappedByteBufferAdapter)buffer).free() see #914 - Class directByteBufferClass = Class.forName("java.nio.MappedByteBufferAdapter"); - callBufferFree(buffer, directByteBufferClass); - } else { - // <=JDK8 class DirectByteBuffer { sun.misc.Cleaner cleaner(Buffer buf) } - // then call sun.misc.Cleaner.clean - final Class directByteBufferClass = Class.forName("java.nio.DirectByteBuffer"); - try { - final Method dbbCleanerMethod = directByteBufferClass.getMethod("cleaner"); - dbbCleanerMethod.setAccessible(true); - // call: cleaner = ((DirectByteBuffer)buffer).cleaner() - final Object cleaner = dbbCleanerMethod.invoke(buffer); - if (cleaner != null) { - final Class cleanerMethodReturnType = dbbCleanerMethod.getReturnType(); - final Method cleanMethod = cleanerMethodReturnType.getDeclaredMethod("clean"); - cleanMethod.setAccessible(true); - // call: ((sun.misc.Cleaner)cleaner).clean() - cleanMethod.invoke(cleaner); - } - } catch (NoSuchMethodException ex2) { - if (Constants.ANDROID) - // For Android 5.1.1 call ((DirectByteBuffer)buffer).free() see #933 - callBufferFree(buffer, directByteBufferClass); - else - // ignore if method cleaner or clean is not available - LOGGER.warn("NoSuchMethodException | " + System.getProperty("java.version"), ex2); - } + // >=JDK9 class sun.misc.Unsafe { void invokeCleaner(ByteBuffer buf) } + final Class unsafeClass = Class.forName("sun.misc.Unsafe"); + // fetch the unsafe instance and bind it to the virtual MethodHandle + final Field f = unsafeClass.getDeclaredField("theUnsafe"); + f.setAccessible(true); + final Object theUnsafe = f.get(null); + final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class); + try { + method.invoke(theUnsafe, buffer); + return null; + } catch (Throwable t) { + throw new RuntimeException(t); } - - return null; } }); } catch (PrivilegedActionException e) { @@ -142,21 +90,9 @@ public Object run() throws Exception { } } - private static void callBufferFree(ByteBuffer buffer, Class directByteBufferClass) - throws InvocationTargetException, IllegalAccessException { - try { - final Method dbbFreeMethod = directByteBufferClass.getMethod("free"); - dbbFreeMethod.setAccessible(true); - dbbFreeMethod.invoke(buffer); - } catch (NoSuchMethodException ex2) { - LOGGER.warn("NoSuchMethodException | " + System.getProperty("java.version"), ex2); - } - } - private void initRandomAccessFile() { - if (raFile != null) { + if (raFile != null) return; - } try { // raFile necessary for loadExisting and create diff --git a/core/src/main/java/com/graphhopper/util/Constants.java b/core/src/main/java/com/graphhopper/util/Constants.java index f5cfcfba0f8..498479f8d1a 100644 --- a/core/src/main/java/com/graphhopper/util/Constants.java +++ b/core/src/main/java/com/graphhopper/util/Constants.java @@ -42,18 +42,10 @@ public class Constants { * True iff running on Linux. */ public static final boolean LINUX = OS_NAME.startsWith("Linux"); - /** - * True iff running on Android. - */ - public static final boolean ANDROID = System.getProperty("java.vendor").contains("Android"); /** * True iff running on Windows. */ public static final boolean WINDOWS = OS_NAME.startsWith("Windows"); - /** - * True iff running on SunOS. - */ - public static final boolean SUN_OS = OS_NAME.startsWith("SunOS"); /** * True iff running on Mac OS X */ diff --git a/core/src/main/java/com/graphhopper/util/GitInfo.java b/core/src/main/java/com/graphhopper/util/GitInfo.java index b7cbef6f4d2..36708c249d4 100644 --- a/core/src/main/java/com/graphhopper/util/GitInfo.java +++ b/core/src/main/java/com/graphhopper/util/GitInfo.java @@ -56,6 +56,6 @@ public boolean isDirty() { } public String toString() { - return Helper.join("|", Arrays.asList(commitHash, branch, "dirty=" + dirty, commitTime, commitMessage)); + return String.join("|", Arrays.asList(commitHash, branch, "dirty=" + dirty, commitTime, commitMessage)); } } diff --git a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java index 06fa172e476..9ec43c85db3 100644 --- a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java @@ -196,7 +196,7 @@ public void testRandomGraph(String algoStr) { } if (strictViolations.size() > 0.05 * numQueries) { fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" + - Helper.join("\n", strictViolations)); + String.join("\n", strictViolations)); } } diff --git a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java index b05918482b4..97b7ea92228 100644 --- a/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java +++ b/core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java @@ -169,7 +169,7 @@ private void runRandomTest(Fixture f, Random rnd) { } if (strictViolations.size() > 0.05 * numQueries) { fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" + - Helper.join("\n", strictViolations)); + String.join("\n", strictViolations)); } } } diff --git a/pom.xml b/pom.xml index 268a49015d1..a196a99ff4e 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ UTF-8 UTF-8 - 1.8 + 17 true - 1.8 - 1.8 + 17 diff --git a/web-api/src/main/java/com/graphhopper/util/Helper.java b/web-api/src/main/java/com/graphhopper/util/Helper.java index 761abffc9bb..ed91970b7a8 100644 --- a/web-api/src/main/java/com/graphhopper/util/Helper.java +++ b/web-api/src/main/java/com/graphhopper/util/Helper.java @@ -413,20 +413,6 @@ public static String underScoreToCamelCase(String key) { return sb.toString(); } - /** - * Equivalent to java 8 String#join - */ - public static String join(String delimiter, List strings) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < strings.size(); i++) { - if (i > 0) { - sb.append(delimiter); - } - sb.append(strings.get(i)); - } - return sb.toString(); - } - /** * parses a string like [a,b,c] */