1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.testlibrary.management; 25 26 import java.lang.management.RuntimeMXBean; 27 import java.lang.management.ManagementFactory; 28 import java.util.List; 29 30 /** 31 * This class provides access to the input arguments to the VM. 32 */ 33 public class RuntimeMXBeanTool { 34 private static final RuntimeMXBean runtimeMxBean; 35 private static final List<String> args; 36 37 static { 38 runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 39 args = runtimeMxBean.getInputArguments(); 40 } 41 42 /** 43 * Returns true if {@code arg} is an input argument to the VM. 44 * 45 * This is useful for checking boolean flags such as -XX:+UseSerialGC or 46 * -XX:-UsePerfData. 47 * 48 * @param arg The name of the argument. 49 * @return {@code true} if the given argument is an input argument, 50 * otherwise {@code false}. 51 */ 52 public static boolean inputArgumentsContain(String arg) { 53 return args.contains(arg); 54 } 55 56 /** 57 * Returns true if {@code prefix} is the start of an input argument to the 58 * VM. 59 * 60 * This is useful for checking if flags describing a quantity, such as 61 * -XX:+MaxMetaspaceSize=100m, is set without having to know the quantity. 62 * To check if the flag -XX:MaxMetaspaceSize is set, use 63 * {@code InputArguments.containsPrefix("-XX:MaxMetaspaceSize")}. 64 * 65 * @param prefix The start of the argument. 66 * @return {@code true} if the given argument is the start of an input 67 * argument, otherwise {@code false}. 68 */ 69 public static boolean anInputArgumentStartsWith(String prefix) { 70 for (String arg : args) { 71 if (arg.startsWith(prefix)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 78 /** 79 * Get the string containing input arguments passed to the VM 80 */ 81 public static String getInputArguments() { 82 StringBuilder result = new StringBuilder(); 83 for (String arg : args) 84 result.append(arg).append(' '); 85 86 return result.toString(); 87 } 88 89 }