Skip to content

Commit c721fa3

Browse files
karussellotbutz
andauthored
move to JDK17 (graphhopper#2827)
* move to JDK17 * Update pom.xml Co-authored-by: otbutz <[email protected]> * simplify cleanMappedByteBuffer --------- Co-authored-by: otbutz <[email protected]>
1 parent aa2ffd4 commit c721fa3

File tree

9 files changed

+20
-107
lines changed

9 files changed

+20
-107
lines changed

.github/workflows/publish-maven-central.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- uses: actions/setup-java@v3
1515
with:
16-
java-version: 8
16+
java-version: 17
1717
distribution: temurin
1818
server-id: ossrh
1919
server-username: MAVEN_USERNAME

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ To get started you can try [GraphHopper Maps](README.md#graphhopper-maps), read
8585

8686
## Installation
8787

88-
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:
88+
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:
8989

9090
```bash
9191
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

core/src/main/java/com/graphhopper/storage/MMapDataAccess.java

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package com.graphhopper.storage;
1919

20-
import com.graphhopper.util.Constants;
2120
import com.graphhopper.util.Helper;
2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
@@ -56,8 +55,6 @@
5655
*/
5756
public final class MMapDataAccess extends AbstractDataAccess {
5857

59-
private static final Logger LOGGER = LoggerFactory.getLogger(MMapDataAccess.class);
60-
6158
private final boolean allowWrites;
6259
private RandomAccessFile raFile;
6360
private final List<MappedByteBuffer> segments = new ArrayList<>();
@@ -67,96 +64,35 @@ public final class MMapDataAccess extends AbstractDataAccess {
6764
this.allowWrites = allowWrites;
6865
}
6966

70-
public static boolean jreIsMinimumJava9() {
71-
final StringTokenizer st = new StringTokenizer(System.getProperty("java.specification.version"), ".");
72-
int JVM_MAJOR_VERSION = Integer.parseInt(st.nextToken());
73-
int JVM_MINOR_VERSION;
74-
if (st.hasMoreTokens()) {
75-
JVM_MINOR_VERSION = Integer.parseInt(st.nextToken());
76-
} else {
77-
JVM_MINOR_VERSION = 0;
78-
}
79-
return JVM_MAJOR_VERSION > 1 || (JVM_MAJOR_VERSION == 1 && JVM_MINOR_VERSION >= 9);
80-
}
81-
8267
public static void cleanMappedByteBuffer(final ByteBuffer buffer) {
8368
// TODO avoid reflection on every call
8469
try {
8570
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
8671
@Override
8772
public Object run() throws Exception {
88-
if (jreIsMinimumJava9()) {
89-
// >=JDK9 class sun.misc.Unsafe { void invokeCleaner(ByteBuffer buf) }
90-
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
91-
// fetch the unsafe instance and bind it to the virtual MethodHandle
92-
final Field f = unsafeClass.getDeclaredField("theUnsafe");
93-
f.setAccessible(true);
94-
final Object theUnsafe = f.get(null);
95-
final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class);
96-
try {
97-
method.invoke(theUnsafe, buffer);
98-
return null;
99-
} catch (Throwable t) {
100-
throw new RuntimeException(t);
101-
}
102-
}
103-
104-
if (buffer.getClass().getSimpleName().equals("MappedByteBufferAdapter")) {
105-
if (!Constants.ANDROID)
106-
throw new RuntimeException("MappedByteBufferAdapter only supported for Android at the moment");
107-
108-
// For Android 4.1 call ((MappedByteBufferAdapter)buffer).free() see #914
109-
Class<?> directByteBufferClass = Class.forName("java.nio.MappedByteBufferAdapter");
110-
callBufferFree(buffer, directByteBufferClass);
111-
} else {
112-
// <=JDK8 class DirectByteBuffer { sun.misc.Cleaner cleaner(Buffer buf) }
113-
// then call sun.misc.Cleaner.clean
114-
final Class<?> directByteBufferClass = Class.forName("java.nio.DirectByteBuffer");
115-
try {
116-
final Method dbbCleanerMethod = directByteBufferClass.getMethod("cleaner");
117-
dbbCleanerMethod.setAccessible(true);
118-
// call: cleaner = ((DirectByteBuffer)buffer).cleaner()
119-
final Object cleaner = dbbCleanerMethod.invoke(buffer);
120-
if (cleaner != null) {
121-
final Class<?> cleanerMethodReturnType = dbbCleanerMethod.getReturnType();
122-
final Method cleanMethod = cleanerMethodReturnType.getDeclaredMethod("clean");
123-
cleanMethod.setAccessible(true);
124-
// call: ((sun.misc.Cleaner)cleaner).clean()
125-
cleanMethod.invoke(cleaner);
126-
}
127-
} catch (NoSuchMethodException ex2) {
128-
if (Constants.ANDROID)
129-
// For Android 5.1.1 call ((DirectByteBuffer)buffer).free() see #933
130-
callBufferFree(buffer, directByteBufferClass);
131-
else
132-
// ignore if method cleaner or clean is not available
133-
LOGGER.warn("NoSuchMethodException | " + System.getProperty("java.version"), ex2);
134-
}
73+
// >=JDK9 class sun.misc.Unsafe { void invokeCleaner(ByteBuffer buf) }
74+
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
75+
// fetch the unsafe instance and bind it to the virtual MethodHandle
76+
final Field f = unsafeClass.getDeclaredField("theUnsafe");
77+
f.setAccessible(true);
78+
final Object theUnsafe = f.get(null);
79+
final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class);
80+
try {
81+
method.invoke(theUnsafe, buffer);
82+
return null;
83+
} catch (Throwable t) {
84+
throw new RuntimeException(t);
13585
}
136-
137-
return null;
13886
}
13987
});
14088
} catch (PrivilegedActionException e) {
14189
throw new RuntimeException("Unable to unmap the mapped buffer", e);
14290
}
14391
}
14492

145-
private static void callBufferFree(ByteBuffer buffer, Class<?> directByteBufferClass)
146-
throws InvocationTargetException, IllegalAccessException {
147-
try {
148-
final Method dbbFreeMethod = directByteBufferClass.getMethod("free");
149-
dbbFreeMethod.setAccessible(true);
150-
dbbFreeMethod.invoke(buffer);
151-
} catch (NoSuchMethodException ex2) {
152-
LOGGER.warn("NoSuchMethodException | " + System.getProperty("java.version"), ex2);
153-
}
154-
}
155-
15693
private void initRandomAccessFile() {
157-
if (raFile != null) {
94+
if (raFile != null)
15895
return;
159-
}
16096

16197
try {
16298
// raFile necessary for loadExisting and create

core/src/main/java/com/graphhopper/util/Constants.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,10 @@ public class Constants {
4242
* True iff running on Linux.
4343
*/
4444
public static final boolean LINUX = OS_NAME.startsWith("Linux");
45-
/**
46-
* True iff running on Android.
47-
*/
48-
public static final boolean ANDROID = System.getProperty("java.vendor").contains("Android");
4945
/**
5046
* True iff running on Windows.
5147
*/
5248
public static final boolean WINDOWS = OS_NAME.startsWith("Windows");
53-
/**
54-
* True iff running on SunOS.
55-
*/
56-
public static final boolean SUN_OS = OS_NAME.startsWith("SunOS");
5749
/**
5850
* True iff running on Mac OS X
5951
*/

core/src/main/java/com/graphhopper/util/GitInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public boolean isDirty() {
5656
}
5757

5858
public String toString() {
59-
return Helper.join("|", Arrays.asList(commitHash, branch, "dirty=" + dirty, commitTime, commitMessage));
59+
return String.join("|", Arrays.asList(commitHash, branch, "dirty=" + dirty, commitTime, commitMessage));
6060
}
6161
}

core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void testRandomGraph(String algoStr) {
196196
}
197197
if (strictViolations.size() > 0.05 * numQueries) {
198198
fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" +
199-
Helper.join("\n", strictViolations));
199+
String.join("\n", strictViolations));
200200
}
201201
}
202202

core/src/test/java/com/graphhopper/routing/RandomCHRoutingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void runRandomTest(Fixture f, Random rnd) {
169169
}
170170
if (strictViolations.size() > 0.05 * numQueries) {
171171
fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" +
172-
Helper.join("\n", strictViolations));
172+
String.join("\n", strictViolations));
173173
}
174174
}
175175
}

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1818

19-
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.target>17</maven.compiler.target>
2020

2121
<!-- We always had this disabled as it is disabled by default in debian JDK builds, but when we
2222
switched to another JDK on travis it was enabled implicitly. Our javadocs are not ready for this
@@ -183,8 +183,7 @@
183183
<compilerArgument>-Xlint:deprecation</compilerArgument>
184184
-->
185185
<fork>true</fork>
186-
<source>1.8</source>
187-
<target>1.8</target>
186+
<release>17</release>
188187
</configuration>
189188
</plugin>
190189

web-api/src/main/java/com/graphhopper/util/Helper.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,6 @@ public static String underScoreToCamelCase(String key) {
413413
return sb.toString();
414414
}
415415

416-
/**
417-
* Equivalent to java 8 String#join
418-
*/
419-
public static String join(String delimiter, List<String> strings) {
420-
StringBuilder sb = new StringBuilder();
421-
for (int i = 0; i < strings.size(); i++) {
422-
if (i > 0) {
423-
sb.append(delimiter);
424-
}
425-
sb.append(strings.get(i));
426-
}
427-
return sb.toString();
428-
}
429-
430416
/**
431417
* parses a string like [a,b,c]
432418
*/

0 commit comments

Comments
 (0)