File: ObjectInstancesManager.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (232 lines) | stat: -rw-r--r-- 9,391 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package nsk.share;

import java.util.*;

/*
 *  Class create/delete instances with given reference type and given referrers number
 */
public class ObjectInstancesManager
{
        public static String STRONG_REFERENCE = "STRONG";
        public static String WEAK_REFERENCE = "WEAK";
        public static String SOFT_REFERENCE = "SOFT";
        public static String PHANTOM_REFERENCE = "PHANTOM";
        public static String JNI_GLOBAL_REFERENCE = "JNI_GLOBAL";
        public static String JNI_LOCAL_REFERENCE = "JNI_LOCAL";
        public static String JNI_WEAK_REFERENCE = "JNI_WEAK";

        // used to create references of all types
        private static String USE_ALL_REFERENCE_TYPES = "ALL_REFERENCE_TYPES";

        private Map<String, Collection<ReferringObjectSet>> instances = new TreeMap<String, Collection<ReferringObjectSet>>();

        public static Set<String> primitiveArrayClassNames = new HashSet<String>();

        static
        {
                primitiveArrayClassNames.add("boolean[]");
                primitiveArrayClassNames.add("byte[]");
                primitiveArrayClassNames.add("char[]");
                primitiveArrayClassNames.add("int[]");
                primitiveArrayClassNames.add("long[]");
                primitiveArrayClassNames.add("float[]");
                primitiveArrayClassNames.add("double[]");
        }


        public static Set<String> allReferenceTypes = new HashSet<String>();

        static
        {
                allReferenceTypes.add(ObjectInstancesManager.STRONG_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.WEAK_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.SOFT_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.JNI_GLOBAL_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.JNI_LOCAL_REFERENCE);
                allReferenceTypes.add(ObjectInstancesManager.JNI_WEAK_REFERENCE);
        }

        public static boolean isWeak(String type) {
            return !(type.equals(ObjectInstancesManager.JNI_GLOBAL_REFERENCE)
                    || type.equals(ObjectInstancesManager.JNI_LOCAL_REFERENCE)
                    || type.equals(ObjectInstancesManager.STRONG_REFERENCE));

        }

        public static Log log;

        public ObjectInstancesManager(Log log)
        {
                ObjectInstancesManager.log = log;
        }

        // delete a given number of referrers
        public void deleteReferrers(String className, int referrersCount, Set<String> referrerTypes)
        {
                Collection<ReferringObjectSet> objectInstances;

                objectInstances = instances.get(className);

                if(objectInstances == null)
                {
                        log.display("Error command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");
                        System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
                        return;
                }

                Iterator<ReferringObjectSet> iterator = objectInstances.iterator();

                while(iterator.hasNext())
                {
                        ReferringObjectSet debugeeObjectReference = iterator.next();
                        if (referrerTypes.isEmpty() || referrerTypes.contains(debugeeObjectReference.getReferenceType())) {
                                debugeeObjectReference.delete(referrersCount);

                                if(debugeeObjectReference.getReferrerCount() == 0)
                                        iterator.remove();
                        }
                }
        }

        // delete all object referrers, it is equal to make object unreacheable
        public void deleteAllReferrers(int count, String className)
        {
                Collection<ReferringObjectSet> objectInstances;

                objectInstances = instances.get(className);

                if(objectInstances == null)
                {
                        throw new TestBug("Command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");
                }

                Iterator<ReferringObjectSet> iterator = objectInstances.iterator();

                if(count == 0)
                        count = objectInstances.size();

                for(int i = 0; i < count; i++)
                {
                        ReferringObjectSet debugeeObjectReference = iterator.next();
                        debugeeObjectReference.deleteAll();

                        iterator.remove();
                }
        }

        // create object instance with referrers of all possible types
        public void createAllTypeReferences(String className, int count)
        {
                createReferences(count, className, 1, allReferenceTypes);
        }

        // create a given number of object instances with given number of referrers
        public void createReferences(int count, String className, int referrerCount, Set<String> referrerTypes)
        {
                Collection<ReferringObjectSet> objectInstances;

                Class klass = null;

                if(!primitiveArrayClassNames.contains(className))
                {
                        try
                        {
                                klass = Class.forName(className);
                        }
                        catch(ClassNotFoundException e)
                        {
                                log.display("Can't find class: " + className);
                                System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
                                return;
                        }
                }

                objectInstances = instances.get(className);

                if(objectInstances == null)
                {
                        objectInstances = new ArrayList<ReferringObjectSet>();
                        instances.put(className, objectInstances);
                }

                for(int i = 0; i < count; i++)
                {
                        try
                        {
                                Object instance;

                                if(!primitiveArrayClassNames.contains(className))
                                {
                                        instance = klass.newInstance();
                                }
                                else
                                {
                                        instance = createPrimitiveTypeArray(className);
                                }

                                for(String type : referrerTypes) {
                                        objectInstances.add(new ReferringObjectSet(instance, referrerCount, type));
                                }
                        }
                        catch(Exception e)
                        {
                                log.display("Unexpected exception: " + e);
                                System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
                        }
                }
        }

        public Object createPrimitiveTypeArray(String typeName)
        {
                int arraySize = 1;

                if(typeName.equals("boolean[]"))
                        return new boolean[arraySize];
                else
                if(typeName.equals("byte[]"))
                        return new byte[arraySize];
                else
                if(typeName.equals("char[]"))
                        return new char[arraySize];
                else
                if(typeName.equals("int[]"))
                        return new int[arraySize];
                else
                if(typeName.equals("long[]"))
                        return new long[arraySize];
                else
                if(typeName.equals("float[]"))
                        return new float[arraySize];
                else
                if(typeName.equals("double[]"))
                        return new double[arraySize];
                else
                {
                        throw new TestBug("Invalid primitive type array type name: " + typeName);
                }
        }

}