001/* 002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package jdk.internal.jvmci.options; 024 025import java.util.*; 026 027public class OptionUtils { 028 029 public interface OptionConsumer { 030 void set(OptionDescriptor desc, Object value); 031 } 032 033 /** 034 * Parses a given option value specification. 035 * 036 * @param option the specification of an option and its value 037 * @param setter the object to notify of the parsed option and value. 038 */ 039 public static void parseOption(String option, OptionConsumer setter) { 040 SortedMap<String, OptionDescriptor> options = OptionsLoader.options; 041 Objects.requireNonNull(setter); 042 if (option.length() == 0) { 043 return; 044 } 045 046 Object value = null; 047 String optionName = null; 048 String valueString = null; 049 050 char first = option.charAt(0); 051 if (first == '+' || first == '-') { 052 optionName = option.substring(1); 053 value = (first == '+'); 054 } else { 055 int index = option.indexOf('='); 056 if (index == -1) { 057 optionName = option; 058 valueString = null; 059 } else { 060 optionName = option.substring(0, index); 061 valueString = option.substring(index + 1); 062 } 063 } 064 065 OptionDescriptor desc = options.get(optionName); 066 if (desc == null) { 067 throw new IllegalArgumentException("Option '" + optionName + "' not found"); 068 } 069 070 Class<?> optionType = desc.getType(); 071 072 if (value == null) { 073 if (optionType == Boolean.TYPE || optionType == Boolean.class) { 074 throw new IllegalArgumentException("Boolean option '" + optionName + "' must use +/- prefix"); 075 } 076 077 if (valueString == null) { 078 throw new IllegalArgumentException("Missing value for non-boolean option '" + optionName + "' must use " + optionName + "=<value> format"); 079 } 080 081 if (optionType == Float.class) { 082 value = Float.parseFloat(valueString); 083 } else if (optionType == Double.class) { 084 value = Double.parseDouble(valueString); 085 } else if (optionType == Integer.class) { 086 value = Integer.valueOf((int) parseLong(valueString)); 087 } else if (optionType == Long.class) { 088 value = Long.valueOf(parseLong(valueString)); 089 } else if (optionType == String.class) { 090 value = valueString; 091 } else { 092 throw new IllegalArgumentException("Wrong value for option '" + optionName + "'"); 093 } 094 } else { 095 if (optionType != Boolean.class) { 096 throw new IllegalArgumentException("Non-boolean option '" + optionName + "' can not use +/- prefix. Use " + optionName + "=<value> format"); 097 } 098 } 099 100 setter.set(desc, value); 101 } 102 103 private static long parseLong(String v) { 104 String valueString = v.toLowerCase(); 105 long scale = 1; 106 if (valueString.endsWith("k")) { 107 scale = 1024L; 108 } else if (valueString.endsWith("m")) { 109 scale = 1024L * 1024L; 110 } else if (valueString.endsWith("g")) { 111 scale = 1024L * 1024L * 1024L; 112 } else if (valueString.endsWith("t")) { 113 scale = 1024L * 1024L * 1024L * 1024L; 114 } 115 116 if (scale != 1) { 117 /* Remove trailing scale character. */ 118 valueString = valueString.substring(0, valueString.length() - 1); 119 } 120 121 return Long.parseLong(valueString) * scale; 122 } 123 124 /** 125 * Wraps some given text to one or more lines of a given maximum width. 126 * 127 * @param text text to wrap 128 * @param width maximum width of an output line, exception for words in {@code text} longer than 129 * this value 130 * @return {@code text} broken into lines 131 */ 132 private static List<String> wrap(String text, int width) { 133 List<String> lines = Collections.singletonList(text); 134 if (text.length() > width) { 135 String[] chunks = text.split("\\s+"); 136 lines = new ArrayList<>(); 137 StringBuilder line = new StringBuilder(); 138 for (String chunk : chunks) { 139 if (line.length() + chunk.length() > width) { 140 lines.add(line.toString()); 141 line.setLength(0); 142 } 143 if (line.length() != 0) { 144 line.append(' '); 145 } 146 String[] embeddedLines = chunk.split("%n", -2); 147 if (embeddedLines.length == 1) { 148 line.append(chunk); 149 } else { 150 for (int i = 0; i < embeddedLines.length; i++) { 151 line.append(embeddedLines[i]); 152 if (i < embeddedLines.length - 1) { 153 lines.add(line.toString()); 154 line.setLength(0); 155 } 156 } 157 } 158 } 159 if (line.length() != 0) { 160 lines.add(line.toString()); 161 } 162 } 163 return lines; 164 } 165 166 public static void printFlags(SortedMap<String, OptionDescriptor> options, String prefix) { 167 System.out.println("[List of " + prefix + " options]"); 168 SortedMap<String, OptionDescriptor> sortedOptions = options; 169 for (Map.Entry<String, OptionDescriptor> e : sortedOptions.entrySet()) { 170 e.getKey(); 171 OptionDescriptor desc = e.getValue(); 172 Object value = desc.getOptionValue().getValue(); 173 List<String> helpLines = wrap(desc.getHelp(), 70); 174 System.out.println(String.format("%9s %-40s = %-14s %s", desc.getType().getSimpleName(), e.getKey(), value, helpLines.get(0))); 175 for (int i = 1; i < helpLines.size(); i++) { 176 System.out.println(String.format("%67s %s", " ", helpLines.get(i))); 177 } 178 } 179 180 System.exit(0); 181 } 182}