Mercurial > hg > truffle
annotate graal/com.oracle.jvmci.options/src/com/oracle/jvmci/options/OptionUtils.java @ 21771:ed3d7af510bd
BytecodeParser#lookupField: use eagerResolving instead of unresolvedIsError to guard explicit initialization.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Wed, 03 Jun 2015 09:28:41 +0200 |
parents | 47bebae7454f |
children |
rev | line source |
---|---|
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
1 /* |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
4 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
8 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
14 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
18 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
21 * questions. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
22 */ |
21554
b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
20822
diff
changeset
|
23 package com.oracle.jvmci.options; |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
24 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 import java.util.*; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
27 public class OptionUtils { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
28 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
29 public interface OptionConsumer { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
30 void set(OptionDescriptor desc, Object value); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
31 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
32 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
33 /** |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
34 * Parses a given option value specification. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
35 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
36 * @param option the specification of an option and its value |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
37 * @param setter the object to notify of the parsed option and value. |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
38 */ |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
39 public static void parseOption(String option, OptionConsumer setter) { |
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
40 SortedMap<String, OptionDescriptor> options = OptionsLoader.options; |
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
41 Objects.requireNonNull(setter); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
42 if (option.length() == 0) { |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
43 return; |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
45 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
46 Object value = null; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
47 String optionName = null; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
48 String valueString = null; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
49 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
50 char first = option.charAt(0); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
51 if (first == '+' || first == '-') { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
52 optionName = option.substring(1); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
53 value = (first == '+'); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
54 } else { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
55 int index = option.indexOf('='); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
56 if (index == -1) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
57 optionName = option; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
58 valueString = null; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
59 } else { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
60 optionName = option.substring(0, index); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
61 valueString = option.substring(index + 1); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
62 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
63 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
64 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
65 OptionDescriptor desc = options.get(optionName); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
66 if (desc == null) { |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
67 throw new IllegalArgumentException("Option '" + optionName + "' not found"); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
68 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
69 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
70 Class<?> optionType = desc.getType(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
71 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
72 if (value == null) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
73 if (optionType == Boolean.TYPE || optionType == Boolean.class) { |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
74 throw new IllegalArgumentException("Boolean option '" + optionName + "' must use +/- prefix"); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
75 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
76 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
77 if (valueString == null) { |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
78 throw new IllegalArgumentException("Missing value for non-boolean option '" + optionName + "' must use " + optionName + "=<value> format"); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
79 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
80 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
81 if (optionType == Float.class) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
82 value = Float.parseFloat(valueString); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
83 } else if (optionType == Double.class) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
84 value = Double.parseDouble(valueString); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
85 } else if (optionType == Integer.class) { |
20822
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
86 value = Integer.valueOf((int) parseLong(valueString)); |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
87 } else if (optionType == Long.class) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
88 value = Long.valueOf(parseLong(valueString)); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
89 } else if (optionType == String.class) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
90 value = valueString; |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
91 } else { |
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
92 throw new IllegalArgumentException("Wrong value for option '" + optionName + "'"); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
93 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
94 } else { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
95 if (optionType != Boolean.class) { |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
96 throw new IllegalArgumentException("Non-boolean option '" + optionName + "' can not use +/- prefix. Use " + optionName + "=<value> format"); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
97 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
98 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
99 |
21519
cecb4e39521c
Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents:
20822
diff
changeset
|
100 setter.set(desc, value); |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
101 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
102 |
20822
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
103 private static long parseLong(String v) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
104 String valueString = v.toLowerCase(); |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
105 long scale = 1; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
106 if (valueString.endsWith("k")) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
107 scale = 1024L; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
108 } else if (valueString.endsWith("m")) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
109 scale = 1024L * 1024L; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
110 } else if (valueString.endsWith("g")) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
111 scale = 1024L * 1024L * 1024L; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
112 } else if (valueString.endsWith("t")) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
113 scale = 1024L * 1024L * 1024L * 1024L; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
114 } |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
115 |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
116 if (scale != 1) { |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
117 /* Remove trailing scale character. */ |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
118 valueString = valueString.substring(0, valueString.length() - 1); |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
119 } |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
120 |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
121 return Long.parseLong(valueString) * scale; |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
122 } |
8ad82587f08d
Allow scaling factors in integer options; add support for Long values in addition to Integer values
Christian Wimmer <christian.wimmer@oracle.com>
parents:
18684
diff
changeset
|
123 |
18684
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
124 /** |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
125 * Wraps some given text to one or more lines of a given maximum width. |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
126 * |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
127 * @param text text to wrap |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
128 * @param width maximum width of an output line, exception for words in {@code text} longer than |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
129 * this value |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
130 * @return {@code text} broken into lines |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
131 */ |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
132 private static List<String> wrap(String text, int width) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
133 List<String> lines = Collections.singletonList(text); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
134 if (text.length() > width) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
135 String[] chunks = text.split("\\s+"); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
136 lines = new ArrayList<>(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
137 StringBuilder line = new StringBuilder(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
138 for (String chunk : chunks) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
139 if (line.length() + chunk.length() > width) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
140 lines.add(line.toString()); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
141 line.setLength(0); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
142 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
143 if (line.length() != 0) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
144 line.append(' '); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
145 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
146 String[] embeddedLines = chunk.split("%n", -2); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
147 if (embeddedLines.length == 1) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
148 line.append(chunk); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
149 } else { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
150 for (int i = 0; i < embeddedLines.length; i++) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
151 line.append(embeddedLines[i]); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
152 if (i < embeddedLines.length - 1) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
153 lines.add(line.toString()); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
154 line.setLength(0); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
155 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
156 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
157 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
158 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
159 if (line.length() != 0) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
160 lines.add(line.toString()); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
161 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
162 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
163 return lines; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
164 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
165 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
166 public static void printFlags(SortedMap<String, OptionDescriptor> options, String prefix) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
167 System.out.println("[List of " + prefix + " options]"); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
168 SortedMap<String, OptionDescriptor> sortedOptions = options; |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
169 for (Map.Entry<String, OptionDescriptor> e : sortedOptions.entrySet()) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
170 e.getKey(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
171 OptionDescriptor desc = e.getValue(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
172 Object value = desc.getOptionValue().getValue(); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
173 List<String> helpLines = wrap(desc.getHelp(), 70); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
174 System.out.println(String.format("%9s %-40s = %-14s %s", desc.getType().getSimpleName(), e.getKey(), value, helpLines.get(0))); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
175 for (int i = 1; i < helpLines.size(); i++) { |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
176 System.out.println(String.format("%67s %s", " ", helpLines.get(i))); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
177 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
178 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
179 |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
180 System.exit(0); |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
181 } |
137773e5250c
Factor out VM-independent parts of option parsing
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
182 } |