|
1 | 1 | package io.quarkus.runtime;
|
2 | 2 |
|
3 |
| -import java.lang.management.ManagementFactory; |
4 |
| -import java.lang.management.RuntimeMXBean; |
5 |
| -import java.util.List; |
6 |
| - |
7 |
| -import org.jboss.logging.Logger; |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.lang.reflect.Method; |
8 | 5 |
|
9 | 6 | import io.quarkus.runtime.annotations.Recorder;
|
10 | 7 |
|
11 | 8 | @Recorder
|
12 | 9 | public class JVMChecksRecorder {
|
13 | 10 |
|
14 |
| - public void check() { |
15 |
| - if (!isUnsafeMemoryAccessAllowed()) { |
16 |
| - Logger.getLogger("JVM").warn( |
17 |
| - "Unsafe memory access is not going to be allowed in future versions of the JVM. Since Java 24, the JVM will print a warning on boot (most likely shown above), but several Quarkus extensions still require it. " |
18 |
| - + |
19 |
| - "There is currently no need to worry: please add the `--sun-misc-unsafe-memory-access=allow` JVM argument to avoid these warnings. " |
20 |
| - + |
21 |
| - "We are working with the maintainers of those libraries to get this resolved in future versions; when this is done, we will remove the need for this argument."); |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - public static boolean isUnsafeMemoryAccessAllowed() { |
| 11 | + /** |
| 12 | + * This is a horrible hack to disable the Unsafe-related warnings that are printed on startup: |
| 13 | + * we know about the problem, we're working on it, and there's no need to print a warning scaring our |
| 14 | + * users with it. |
| 15 | + */ |
| 16 | + public void disableUnsafeRelatedWarnings() { |
26 | 17 | if (Runtime.version().feature() < 24) {
|
27 |
| - //Versions before Java 24 would not complain about the use of Unsafe. |
28 |
| - //Also, setting `--sun-misc-unsafe-memory-access=allow` isn't possible (not a valid argument) before Java 24. |
29 |
| - return true; |
| 18 | + return; |
| 19 | + } |
| 20 | + try { |
| 21 | + Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); |
| 22 | + Method trySetMemoryAccessWarnedMethod = unsafeClass.getDeclaredMethod("trySetMemoryAccessWarned"); |
| 23 | + trySetMemoryAccessWarnedMethod.setAccessible(true); |
| 24 | + trySetMemoryAccessWarnedMethod.invoke(null); |
| 25 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 26 | + //let's ignore it - if we failed with our horrible hack, worst that could happen is that the ugly warning is printed |
30 | 27 | }
|
31 |
| - RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); |
32 |
| - List<String> arguments = runtimeMxBean.getInputArguments(); |
33 |
| - return arguments.contains("--sun-misc-unsafe-memory-access=allow"); |
34 | 28 | }
|
35 |
| - |
36 | 29 | }
|
0 commit comments