Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/options/package-info.java
41155 views
1
/*
2
* Copyright (c) 2008, 2019, 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
/**
26
* This framework allows one to automate parsing of the test command line
27
* arguments (it automatically sets the corresponding fields of the
28
* test class).
29
<p>
30
<h3>A simplified example</h3>
31
Suppose we want to to define a test Test with an option "iterations",
32
which can be run via
33
</p>
34
<pre> > java Test -iterations 10 </pre>
35
or via
36
<pre> java Test </pre>
37
in the last case iterations defaults to 100.
38
<p>
39
We want to achieve this by annotating fields of the Test class by
40
a special @Option annotation.
41
<p>
42
For simplicity suppose @Option is defined as follows:
43
<pre>
44
&#064;interface Option
45
{ //here all the annotation fields are mandatory
46
String name();
47
String default();
48
String description();
49
}
50
</pre>
51
The test class uses an API like:
52
<pre>
53
public class OptionSupport {
54
public static void setup(Object test, String[] args);
55
}
56
</pre>
57
Now a simple example:
58
<pre>
59
public class Test {
60
61
&#064;Option( name="iterations",
62
default="100",
63
description="Number of iterations")
64
int iterations;
65
public void run() {
66
// ..do actual testing here..
67
}
68
69
public static void main(String args) {
70
Test test = new Test();
71
OptionsSupport.setup(test, args); // instead of manually
72
// parsing arguments
73
// now test.iterations is set to 10 or 100.
74
test.run();
75
}
76
}
77
</pre>
78
This test can be also run via
79
<pre>
80
- java Test -help
81
</pre>
82
Then OptionSupport.setup() shows
83
help and exits (by throwing exception?):
84
<pre>
85
Supported options:
86
-iterations <number>
87
Number of iterations (mandatory)
88
</pre>
89
We also want to be able to apply this to fields of non-simple types (via
90
factories) and to other classes recursively (see @Options annotation
91
below).
92
<p>
93
Please, see {@link vm.share.options.test.SimpleExample}
94
for a working version of this.
95
* <h3> General description </h3>
96
Options are defined using annotations like this:
97
<pre>
98
public class StressOptions {
99
// [2]
100
&#064;Option(name="stressTime",
101
default_value="30",
102
description="Stress time")
103
private long stressTime;
104
105
...
106
}
107
</pre>
108
<p> we want to use command line like
109
<pre> java Test -stressTime 50 </pre>
110
here 50 is passed to the StressOptions.stressTime field.
111
see {@link vm.share.options.Options} below. </p>
112
<pre>
113
public class Test {
114
// [1]
115
&#064;Options
116
StressOptions stressOptions = new StressOptions();
117
118
// [2]
119
&#064;Option(name="iterations",
120
default_value="100",
121
description="Number of iterations")
122
int iterations;
123
124
// [3]
125
&#064;Option(
126
name="garbageProducer",
127
default="byteArr",
128
description="Garbage producer",
129
factory="nsk.share.gc.gp.GarbageProducerFactory")
130
GarbageProducer garbageProducer;
131
...
132
133
// [4]
134
&#064;Option(name="logger",
135
description="Logger",
136
factory="nsk.share.log.LogFactory")
137
Log log;
138
139
public void run() {
140
log.info("Start test");
141
log.info("Finish test");
142
}
143
144
public static void main(String[] args) {
145
Test test = new Test();
146
OptionsSupport.setup(test, args);
147
test.run();
148
}
149
}
150
</pre>
151
<p> The API is invoked via a call to {@link vm.share.options.OptionSupport#setup(Object, String[])}).
152
Also there is {@link vm.share.options.OptionSupport#setup(Object, String[], OptionHandler)}) method.
153
It allows the caller to pass a handler which takes
154
care of the options not defined via @Option annotation.
155
</p>
156
157
<h3>Requirements</h3>
158
<p>
159
- The following field types are supported out-of-box: [2]
160
<ul>
161
<li/>All basic (primitive) types (int, long, ...) and corresponding wrapper types.
162
<li/> In the case of a boolean type option user is allowed to skip second argument,
163
i.e. write '-option_name'
164
instead of '-option_name true'.
165
<li/> Strings.
166
<li/> One dimentional arrays of the above (comma-separated).
167
<li/> Classes if a factory is specified, see below.
168
<emph>NOTE: currently there is no way to pass some options to the instantiated objects.</emph>
169
</ul>
170
</p>
171
<p> All non-static fields (including private and protected) of class and
172
it's superclasses are scanned for annotations.
173
</p>
174
<p>
175
(Possibly) Same annotations for setter methods ? (NOT IMPLEMENTED)
176
</p>
177
<p>
178
It is possible to inherit options of the field type
179
through @Options annotations, see {@link vm.share.options.Options}, and [1] above.
180
</p>
181
<p>
182
Option.name defaults to the name of the field.
183
</p>
184
<p> Object options are supported using {@link vm.share.options.OptionObjectFactory}, see [3] above.
185
Please see {@link vm.share.options.OptionObjectFactory} interface, it should be
186
implemented by the user and specified in the Option.factory attribute.
187
</p>
188
<p>
189
As a shortcut we provide BasicOptionObjectFactory class, which allows user to
190
create a factory via @{@link vm.share.options.Factory} annotation:
191
<pre>
192
&#064;Factory (
193
placeholder_text="garbage producer", //used for generating <..> in the help message
194
default_value="byteArr",
195
classlist={
196
&#064;FClass(key="byteArr",
197
type="nsk.share.gc.gp.array.ByteArrayProducer",
198
description="byte array producer")
199
&#064;FClass(key="charArr",
200
type="nsk.share.gc.gp.array.CharArrayProducer",
201
description="char array producer")
202
...
203
}
204
)
205
public class GarbageProducerFactory extends BasicOptionObjectFactory {
206
}
207
</pre>
208
<p> <emph> note: for subclasses of BasicOptionObjectFactory
209
factories can extend each other!!
210
so the check for @OptionObjectFactory is done recursively.
211
NOT SURE IF THIS IS IMPLEMENTED.
212
</emph></p>
213
<p> If there is no unknownOptionHandler then in case of unsupported
214
option, a Runtime exception is thrown.
215
</p>
216
<p>
217
If there is no 'default' annotation attribute and there is no default
218
for OptionObjectFactory, then option is mandatory and a Runtime exception is thrown if
219
it's missing.
220
</p>
221
<p> Both '-option value' and '-option=value' formats are supported.
222
</p>
223
<p> If main class is given '-help', OptionSupport.setup() shows
224
help and exits (by throwing a Runtime exception):
225
<pre>
226
Supported options:
227
-iterations <number>
228
Number of iterations (mandatory)
229
-stressTime <number>
230
Stress time (default 30)
231
-garbageProducer <garbage producer>
232
Garbage producer (default byteArr). Supported keys:
233
byteArr byte array producer
234
charArr char array producer
235
...
236
...
237
</pre>
238
239
<p> <emph> NOT IMPLEMENTED: </emph>
240
Integer type boundaries are validated with RuntimeException with detailed
241
meaningful error message. Other validations that are possible are also done.
242
</p>
243
<p> <emph> NOT IMPLEMENTED: </emph>
244
Empty default ("") value for Object annotations [3] means null value.
245
</p>
246
<p> The object created via factory is also scanned for @Option annotations
247
(like in the @Options case), i.e. it inherits options from the Test class.
248
<emph> This is not implemented as it causes several problems, in particular, the
249
object must be instanciated in order to be scanned for options, hence no meaningful
250
help message could be generated for corresponding options.
251
One of the possible solutions
252
is to allow -object_opt_name.suboption syntax (or even with a colon), and to pass the
253
corresponding suboptions Map to the OptionObjectFactory, it could use OptionFramework
254
to take care of it. It is unclear, what other problems can arise.</emph>
255
</p>
256
*
257
*/
258
package vm.share.options;
259
260