Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/ObjectInstancesManager.java
40948 views
1
/*
2
* Copyright (c) 2006, 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
package nsk.share;
24
25
import java.util.*;
26
27
/*
28
* Class create/delete instances with given reference type and given referrers number
29
*/
30
public class ObjectInstancesManager
31
{
32
public static String STRONG_REFERENCE = "STRONG";
33
public static String WEAK_REFERENCE = "WEAK";
34
public static String SOFT_REFERENCE = "SOFT";
35
public static String PHANTOM_REFERENCE = "PHANTOM";
36
public static String JNI_GLOBAL_REFERENCE = "JNI_GLOBAL";
37
public static String JNI_LOCAL_REFERENCE = "JNI_LOCAL";
38
public static String JNI_WEAK_REFERENCE = "JNI_WEAK";
39
40
// used to create references of all types
41
private static String USE_ALL_REFERENCE_TYPES = "ALL_REFERENCE_TYPES";
42
43
private Map<String, Collection<ReferringObjectSet>> instances = new TreeMap<String, Collection<ReferringObjectSet>>();
44
45
public static Set<String> primitiveArrayClassNames = new HashSet<String>();
46
47
static
48
{
49
primitiveArrayClassNames.add("boolean[]");
50
primitiveArrayClassNames.add("byte[]");
51
primitiveArrayClassNames.add("char[]");
52
primitiveArrayClassNames.add("int[]");
53
primitiveArrayClassNames.add("long[]");
54
primitiveArrayClassNames.add("float[]");
55
primitiveArrayClassNames.add("double[]");
56
}
57
58
59
public static Set<String> allReferenceTypes = new HashSet<String>();
60
61
static
62
{
63
allReferenceTypes.add(ObjectInstancesManager.STRONG_REFERENCE);
64
allReferenceTypes.add(ObjectInstancesManager.WEAK_REFERENCE);
65
allReferenceTypes.add(ObjectInstancesManager.SOFT_REFERENCE);
66
allReferenceTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);
67
allReferenceTypes.add(ObjectInstancesManager.JNI_GLOBAL_REFERENCE);
68
allReferenceTypes.add(ObjectInstancesManager.JNI_LOCAL_REFERENCE);
69
allReferenceTypes.add(ObjectInstancesManager.JNI_WEAK_REFERENCE);
70
}
71
72
public static boolean isWeak(String type) {
73
return !(type.equals(ObjectInstancesManager.JNI_GLOBAL_REFERENCE)
74
|| type.equals(ObjectInstancesManager.JNI_LOCAL_REFERENCE)
75
|| type.equals(ObjectInstancesManager.STRONG_REFERENCE));
76
77
}
78
79
public static Log log;
80
81
public ObjectInstancesManager(Log log)
82
{
83
ObjectInstancesManager.log = log;
84
}
85
86
// delete a given number of referrers
87
public void deleteReferrers(String className, int referrersCount, Set<String> referrerTypes)
88
{
89
Collection<ReferringObjectSet> objectInstances;
90
91
objectInstances = instances.get(className);
92
93
if(objectInstances == null)
94
{
95
log.display("Error command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");
96
System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
97
return;
98
}
99
100
Iterator<ReferringObjectSet> iterator = objectInstances.iterator();
101
102
while(iterator.hasNext())
103
{
104
ReferringObjectSet debugeeObjectReference = iterator.next();
105
if (referrerTypes.isEmpty() || referrerTypes.contains(debugeeObjectReference.getReferenceType())) {
106
debugeeObjectReference.delete(referrersCount);
107
108
if(debugeeObjectReference.getReferrerCount() == 0)
109
iterator.remove();
110
}
111
}
112
}
113
114
// delete all object referrers, it is equal to make object unreacheable
115
public void deleteAllReferrers(int count, String className)
116
{
117
Collection<ReferringObjectSet> objectInstances;
118
119
objectInstances = instances.get(className);
120
121
if(objectInstances == null)
122
{
123
throw new TestBug("Command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");
124
}
125
126
Iterator<ReferringObjectSet> iterator = objectInstances.iterator();
127
128
if(count == 0)
129
count = objectInstances.size();
130
131
for(int i = 0; i < count; i++)
132
{
133
ReferringObjectSet debugeeObjectReference = iterator.next();
134
debugeeObjectReference.deleteAll();
135
136
iterator.remove();
137
}
138
}
139
140
// create object instance with referrers of all possible types
141
public void createAllTypeReferences(String className, int count)
142
{
143
createReferences(count, className, 1, allReferenceTypes);
144
}
145
146
// create a given number of object instances with given number of referrers
147
public void createReferences(int count, String className, int referrerCount, Set<String> referrerTypes)
148
{
149
Collection<ReferringObjectSet> objectInstances;
150
151
Class klass = null;
152
153
if(!primitiveArrayClassNames.contains(className))
154
{
155
try
156
{
157
klass = Class.forName(className);
158
}
159
catch(ClassNotFoundException e)
160
{
161
log.display("Can't find class: " + className);
162
System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
163
return;
164
}
165
}
166
167
objectInstances = instances.get(className);
168
169
if(objectInstances == null)
170
{
171
objectInstances = new ArrayList<ReferringObjectSet>();
172
instances.put(className, objectInstances);
173
}
174
175
for(int i = 0; i < count; i++)
176
{
177
try
178
{
179
Object instance;
180
181
if(!primitiveArrayClassNames.contains(className))
182
{
183
instance = klass.newInstance();
184
}
185
else
186
{
187
instance = createPrimitiveTypeArray(className);
188
}
189
190
for(String type : referrerTypes) {
191
objectInstances.add(new ReferringObjectSet(instance, referrerCount, type));
192
}
193
}
194
catch(Exception e)
195
{
196
log.display("Unexpected exception: " + e);
197
System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
198
}
199
}
200
}
201
202
public Object createPrimitiveTypeArray(String typeName)
203
{
204
int arraySize = 1;
205
206
if(typeName.equals("boolean[]"))
207
return new boolean[arraySize];
208
else
209
if(typeName.equals("byte[]"))
210
return new byte[arraySize];
211
else
212
if(typeName.equals("char[]"))
213
return new char[arraySize];
214
else
215
if(typeName.equals("int[]"))
216
return new int[arraySize];
217
else
218
if(typeName.equals("long[]"))
219
return new long[arraySize];
220
else
221
if(typeName.equals("float[]"))
222
return new float[arraySize];
223
else
224
if(typeName.equals("double[]"))
225
return new double[arraySize];
226
else
227
{
228
throw new TestBug("Invalid primitive type array type name: " + typeName);
229
}
230
}
231
232
}
233
234