|
2 | 2 |
|
3 | 3 | import static java.nio.charset.StandardCharsets.UTF_8; |
4 | 4 |
|
| 5 | +import datadog.cli.CLIHelper; |
5 | 6 | import de.thetaphi.forbiddenapis.SuppressForbidden; |
6 | 7 | import java.io.BufferedReader; |
7 | 8 | import java.io.File; |
8 | 9 | import java.io.IOException; |
9 | 10 | import java.io.InputStreamReader; |
10 | 11 | import java.io.PrintStream; |
11 | 12 | import java.lang.instrument.Instrumentation; |
12 | | -import java.lang.management.ManagementFactory; |
13 | | -import java.lang.reflect.Field; |
14 | 13 | import java.lang.reflect.Method; |
15 | 14 | import java.net.URI; |
16 | 15 | import java.net.URISyntaxException; |
17 | 16 | import java.net.URL; |
18 | 17 | import java.security.CodeSource; |
19 | 18 | import java.util.ArrayList; |
20 | | -import java.util.Arrays; |
21 | 19 | import java.util.Collections; |
22 | 20 | import java.util.IdentityHashMap; |
23 | 21 | import java.util.List; |
@@ -383,7 +381,7 @@ private static List<File> getAgentFilesFromVMArguments() { |
383 | 381 | // - On IBM-based JDKs since at least 1.7 |
384 | 382 | // This prevents custom log managers from working correctly |
385 | 383 | // Use reflection to bypass the loading of the class~ |
386 | | - for (final String argument : getVMArgumentsThroughReflection()) { |
| 384 | + for (final String argument : CLIHelper.getVmArgs()) { |
387 | 385 | if (argument.startsWith(JAVA_AGENT_ARGUMENT)) { |
388 | 386 | int index = argument.indexOf('=', JAVA_AGENT_ARGUMENT.length()); |
389 | 387 | String agentPathname = |
@@ -424,57 +422,6 @@ private static File getAgentFileUsingClassLoaderLookup() throws URISyntaxExcepti |
424 | 422 | return javaagentFile; |
425 | 423 | } |
426 | 424 |
|
427 | | - @SuppressForbidden |
428 | | - private static List<String> getVMArgumentsThroughReflection() { |
429 | | - // Try Oracle-based |
430 | | - // IBM Semeru Runtime 1.8.0_345-b01 will throw UnsatisfiedLinkError here. |
431 | | - try { |
432 | | - final Class<?> managementFactoryHelperClass = |
433 | | - Class.forName("sun.management.ManagementFactoryHelper"); |
434 | | - |
435 | | - final Class<?> vmManagementClass = Class.forName("sun.management.VMManagement"); |
436 | | - |
437 | | - Object vmManagement; |
438 | | - |
439 | | - try { |
440 | | - vmManagement = |
441 | | - managementFactoryHelperClass.getDeclaredMethod("getVMManagement").invoke(null); |
442 | | - } catch (final NoSuchMethodException e) { |
443 | | - // Older vm before getVMManagement() existed |
444 | | - final Field field = managementFactoryHelperClass.getDeclaredField("jvm"); |
445 | | - field.setAccessible(true); |
446 | | - vmManagement = field.get(null); |
447 | | - field.setAccessible(false); |
448 | | - } |
449 | | - |
450 | | - //noinspection unchecked |
451 | | - return (List<String>) vmManagementClass.getMethod("getVmArguments").invoke(vmManagement); |
452 | | - } catch (final ReflectiveOperationException | UnsatisfiedLinkError ignored) { |
453 | | - // Ignored exception |
454 | | - } |
455 | | - |
456 | | - // Try IBM-based. |
457 | | - try { |
458 | | - final Class<?> VMClass = Class.forName("com.ibm.oti.vm.VM"); |
459 | | - final String[] argArray = (String[]) VMClass.getMethod("getVMArgs").invoke(null); |
460 | | - return Arrays.asList(argArray); |
461 | | - } catch (final ReflectiveOperationException ignored) { |
462 | | - // Ignored exception |
463 | | - } |
464 | | - |
465 | | - // Fallback to default |
466 | | - try { |
467 | | - System.err.println( |
468 | | - "WARNING: Unable to get VM args through reflection. A custom java.util.logging.LogManager may not work correctly"); |
469 | | - return ManagementFactory.getRuntimeMXBean().getInputArguments(); |
470 | | - } catch (final Throwable t) { |
471 | | - // Throws InvocationTargetException on modularized applications |
472 | | - // with non-opened java.management module |
473 | | - System.err.println("WARNING: Unable to get VM args using managed beans"); |
474 | | - } |
475 | | - return Collections.emptyList(); |
476 | | - } |
477 | | - |
478 | 425 | private static void checkJarManifestMainClassIsThis(final URL jarUrl) throws IOException { |
479 | 426 | final URL manifestUrl = new URL("jar:" + jarUrl + "!/META-INF/MANIFEST.MF"); |
480 | 427 | final String mainClassLine = "Main-Class: " + thisClass.getCanonicalName(); |
|
0 commit comments