Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/options/BasicObjectFactory.java
41155 views
1
/*
2
* Copyright (c) 2008, 2018, 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
* To change this template, choose Tools | Templates
25
* and open the template in the editor.
26
*/
27
28
package vm.share.options;
29
30
import java.util.HashMap;
31
import java.util.Map;
32
import nsk.share.TestBug;
33
34
/**
35
* This class allows user to create an {@link ObjectFactory} implementation
36
* via @{@link vm.share.options.Factory} annotation. See the source code of
37
* {@link vm.share.options.test.BasicObjectFactoryUsageExample} and
38
* {@link vm.share.options.test.ExampleWithNonprimitiveOptions}.
39
* @see Factory
40
* @see FClass
41
* @see ObjectFactory
42
*/
43
public class BasicObjectFactory<T> implements ObjectFactory<T>
44
{
45
public String getPlaceholder()
46
{
47
return getAnnotaion().placeholder_text();
48
}
49
50
public String[] getPossibleValues()
51
{
52
return getTypesMap().keySet().toArray(new String[0]);
53
}
54
55
public String getDescription()
56
{
57
return getAnnotaion().description().equals(Factory.defDescription)? null : getAnnotaion().description();
58
}
59
60
public String getDefaultValue()
61
{
62
return getAnnotaion().default_value().equals(Factory.defDefaultValue)? null :
63
getAnnotaion().default_value();
64
}
65
66
public String getParameterDescription(String key)
67
{
68
return getTypesMap().get(key).description();
69
}
70
71
72
// shouldn't value be named key?
73
74
public T getObject(String value)
75
{
76
try
77
{
78
@SuppressWarnings(value="unchecked")
79
T result = (T) getTypesMap().get(value).type().newInstance();
80
return result;
81
} catch (InstantiationException ex)
82
{
83
throw new TestBug("Error while trying to instantiate via " + this.getClass() + " for key " + value, ex);
84
} catch (IllegalAccessException ex)
85
{
86
throw new TestBug("Error while trying to instantiate via " + this.getClass() + " for key " + value, ex);
87
}
88
}
89
90
protected Factory getAnnotaion()
91
{
92
if(!this.getClass().isAnnotationPresent(Factory.class))
93
throw new TestBug(" Found an unnotated BasicObjectFactory subclass.");
94
Factory factoryAnn = this.getClass().getAnnotation(Factory.class);
95
return factoryAnn;
96
}
97
98
protected Map<String, FClass> getTypesMap()
99
{ // probably there could be some lazy initialization, but I decided not to deal with that.
100
FClass[] types = getAnnotaion().classlist();
101
Map<String, FClass> typesMap = new HashMap<String, FClass>(types.length);
102
for (FClass type : types)
103
{
104
typesMap.put(type.key(), type);
105
}
106
return typesMap;
107
108
}
109
110
// see ExampleWithNonprimitiveOptions instead.
111
// public void test()
112
// {
113
// if(!this.getClass().isAnnotationPresent(Factory.class))
114
// throw new RuntimeException(" Found an unnotated BasicObjectFactory subclass.");
115
// Factory factoryAnn = this.getClass().getAnnotation(Factory.class);
116
// System.out.println(" placeholder_text " + factoryAnn.placeholder_text());
117
// System.out.println(" default_value " +
118
// (factoryAnn.default_value().equals(Factory.defDefault_value) ? null : factoryAnn.default_value()) );
119
//
120
// }
121
//
122
// @Factory(placeholder_text="number", default_value="test",
123
// classlist = {
124
// @FClass( key="int", description="integer", type=int.class),
125
// @FClass( key="boolean", description="boolean", type=boolean.class)
126
// } )
127
// public static class testOF extends BasicObjectFactory<BasicObjectFactory> {}
128
//
129
//// @Factory(placeholder_text="placehldr1")
130
//// public static class testOF1 extends BasicObjectFactory<BasicObjectFactory> {}
131
//
132
//
133
// public static void main(String[] args)
134
// {
135
// BasicObjectFactory bof = new testOF();
136
// bof.test();
137
//// new testOF1().test();
138
//
139
// }
140
141
}
142
143