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;
  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 InputArguments {

  34     private static final List<String> args;
  35 
  36     static {
  37         RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
  38         args = runtimeMxBean.getInputArguments();
  39     }
  40 
  41     /**
  42      * Returns true if {@code arg} is an input argument to the VM.
  43      *
  44      * This is useful for checking boolean flags such as -XX:+UseSerialGC or
  45      * -XX:-UsePerfData.
  46      *
  47      * @param arg The name of the argument.
  48      * @return {@code true} if the given argument is an input argument,
  49      *         otherwise {@code false}.
  50      */
  51     public static boolean contains(String arg) {
  52         return args.contains(arg);
  53     }
  54 
  55     /**
  56      * Returns true if {@code prefix} is the start of an input argument to the
  57      * VM.
  58      *
  59      * This is useful for checking if flags describing a quantity, such as
  60      * -XX:+MaxMetaspaceSize=100m, is set without having to know the quantity.
  61      * To check if the flag -XX:MaxMetaspaceSize is set, use
  62      * {@code InputArguments.containsPrefix("-XX:MaxMetaspaceSize")}.
  63      *
  64      * @param prefix The start of the argument.
  65      * @return {@code true} if the given argument is the start of an input
  66      *         argument, otherwise {@code false}.
  67      */
  68     public static boolean containsPrefix(String prefix) {
  69         for (String arg : args) {
  70             if (arg.startsWith(prefix)) {
  71                 return true;
  72             }
  73         }
  74         return false;
  75     }
  76 
  77     /**
  78      * Get the string containing input arguments passed to the VM
  79      */
  80     public static String getInputArguments() {
  81         StringBuilder result = new StringBuilder();
  82         for (String arg : args)
  83             result.append(arg).append(' ');
  84 
  85         return result.toString();
  86     }
  87 
  88 }
--- EOF ---