1 /* 2 * Copyright (c) 2014, 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 25 package com.oracle.java.testlibrary; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 public class PrintFlagsFinalGetter { 31 32 /** 33 * Get values of the properies by their names via -XX:+PrintFlagsFinal 34 * option. Also System.getProperty("test.vm.opts") will be passed to VM in 35 * coupe with -XX:+PrintFlagsFinal 36 * @param names of options to read values from 37 * @return map of names of the properties and their values 38 */ 39 public static Map<String,Object> getWithVMOpts(String ... names) 40 throws Exception { 41 if (System.getProperty("test.vm.opts") == null) { 42 throw new RuntimeException("Property -Dtest.vm.opts is not set. " + 43 "You better set it the same as the options that are passed " + 44 "to the VM"); 45 } 46 return getFlagsFinal(names, System.getProperty("test.vm.opts")); 47 } 48 49 /** 50 * Get values of the properies by their names via -XX:+PrintFlagsFinal 51 * option 52 * @param names of options to read values from 53 * @param vmOpts additional options to pass to the VM while starting it with 54 * -XX:+PrintFlagsFinal 55 * @return map of names of the properties and their values 56 */ 57 public static Map<String,Object> getFlagsFinal( String [] names, 58 String vmOpts) throws Exception { 59 HashMap<String,Object> nameVal = new HashMap<>(); 60 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOpts, 61 "-XX:+PrintFlagsFinal", 62 "-version" 63 ); 64 OutputAnalyzer out = new OutputAnalyzer(pb.start()); 65 for (String name : names) { 66 String npat = ".* " + name + " .* :?= "; 67 String val = null; 68 if ((val = out.firstMatch(npat + "(true|false).*", 1)) != null) { 69 nameVal.put(name, Boolean.parseBoolean(val)); 70 } else if ((val = out.firstMatch(npat + "((\\d+)\\.(\\d+)).*", 1)) 71 != null) { 72 nameVal.put(name, Double.parseDouble(val)); 73 } else if ((val = out.firstMatch(npat + "(\\d+).*", 1)) != null) { 74 nameVal.put(name, Integer.parseInt(val)); 75 } else if ((val = out.firstMatch(npat + "(\\S*) \\{.*", 1)) 76 != null) { 77 nameVal.put(name, val); 78 } else { 79 System.err.println("Can't match value of '" + name + 80 "' in FinalFlags. It may be empty or whitespaces"); 81 nameVal.put(name, ""); 82 } 83 } 84 return nameVal; 85 } 86 } 87