Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/ObjectInstancesManager.java
40948 views
/*1* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share;2324import java.util.*;2526/*27* Class create/delete instances with given reference type and given referrers number28*/29public class ObjectInstancesManager30{31public static String STRONG_REFERENCE = "STRONG";32public static String WEAK_REFERENCE = "WEAK";33public static String SOFT_REFERENCE = "SOFT";34public static String PHANTOM_REFERENCE = "PHANTOM";35public static String JNI_GLOBAL_REFERENCE = "JNI_GLOBAL";36public static String JNI_LOCAL_REFERENCE = "JNI_LOCAL";37public static String JNI_WEAK_REFERENCE = "JNI_WEAK";3839// used to create references of all types40private static String USE_ALL_REFERENCE_TYPES = "ALL_REFERENCE_TYPES";4142private Map<String, Collection<ReferringObjectSet>> instances = new TreeMap<String, Collection<ReferringObjectSet>>();4344public static Set<String> primitiveArrayClassNames = new HashSet<String>();4546static47{48primitiveArrayClassNames.add("boolean[]");49primitiveArrayClassNames.add("byte[]");50primitiveArrayClassNames.add("char[]");51primitiveArrayClassNames.add("int[]");52primitiveArrayClassNames.add("long[]");53primitiveArrayClassNames.add("float[]");54primitiveArrayClassNames.add("double[]");55}565758public static Set<String> allReferenceTypes = new HashSet<String>();5960static61{62allReferenceTypes.add(ObjectInstancesManager.STRONG_REFERENCE);63allReferenceTypes.add(ObjectInstancesManager.WEAK_REFERENCE);64allReferenceTypes.add(ObjectInstancesManager.SOFT_REFERENCE);65allReferenceTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);66allReferenceTypes.add(ObjectInstancesManager.JNI_GLOBAL_REFERENCE);67allReferenceTypes.add(ObjectInstancesManager.JNI_LOCAL_REFERENCE);68allReferenceTypes.add(ObjectInstancesManager.JNI_WEAK_REFERENCE);69}7071public static boolean isWeak(String type) {72return !(type.equals(ObjectInstancesManager.JNI_GLOBAL_REFERENCE)73|| type.equals(ObjectInstancesManager.JNI_LOCAL_REFERENCE)74|| type.equals(ObjectInstancesManager.STRONG_REFERENCE));7576}7778public static Log log;7980public ObjectInstancesManager(Log log)81{82ObjectInstancesManager.log = log;83}8485// delete a given number of referrers86public void deleteReferrers(String className, int referrersCount, Set<String> referrerTypes)87{88Collection<ReferringObjectSet> objectInstances;8990objectInstances = instances.get(className);9192if(objectInstances == null)93{94log.display("Error command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");95System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);96return;97}9899Iterator<ReferringObjectSet> iterator = objectInstances.iterator();100101while(iterator.hasNext())102{103ReferringObjectSet debugeeObjectReference = iterator.next();104if (referrerTypes.isEmpty() || referrerTypes.contains(debugeeObjectReference.getReferenceType())) {105debugeeObjectReference.delete(referrersCount);106107if(debugeeObjectReference.getReferrerCount() == 0)108iterator.remove();109}110}111}112113// delete all object referrers, it is equal to make object unreacheable114public void deleteAllReferrers(int count, String className)115{116Collection<ReferringObjectSet> objectInstances;117118objectInstances = instances.get(className);119120if(objectInstances == null)121{122throw new TestBug("Command 'deleteObjectInstances' is requsted: instances of class " + className + " was not created");123}124125Iterator<ReferringObjectSet> iterator = objectInstances.iterator();126127if(count == 0)128count = objectInstances.size();129130for(int i = 0; i < count; i++)131{132ReferringObjectSet debugeeObjectReference = iterator.next();133debugeeObjectReference.deleteAll();134135iterator.remove();136}137}138139// create object instance with referrers of all possible types140public void createAllTypeReferences(String className, int count)141{142createReferences(count, className, 1, allReferenceTypes);143}144145// create a given number of object instances with given number of referrers146public void createReferences(int count, String className, int referrerCount, Set<String> referrerTypes)147{148Collection<ReferringObjectSet> objectInstances;149150Class klass = null;151152if(!primitiveArrayClassNames.contains(className))153{154try155{156klass = Class.forName(className);157}158catch(ClassNotFoundException e)159{160log.display("Can't find class: " + className);161System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);162return;163}164}165166objectInstances = instances.get(className);167168if(objectInstances == null)169{170objectInstances = new ArrayList<ReferringObjectSet>();171instances.put(className, objectInstances);172}173174for(int i = 0; i < count; i++)175{176try177{178Object instance;179180if(!primitiveArrayClassNames.contains(className))181{182instance = klass.newInstance();183}184else185{186instance = createPrimitiveTypeArray(className);187}188189for(String type : referrerTypes) {190objectInstances.add(new ReferringObjectSet(instance, referrerCount, type));191}192}193catch(Exception e)194{195log.display("Unexpected exception: " + e);196System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);197}198}199}200201public Object createPrimitiveTypeArray(String typeName)202{203int arraySize = 1;204205if(typeName.equals("boolean[]"))206return new boolean[arraySize];207else208if(typeName.equals("byte[]"))209return new byte[arraySize];210else211if(typeName.equals("char[]"))212return new char[arraySize];213else214if(typeName.equals("int[]"))215return new int[arraySize];216else217if(typeName.equals("long[]"))218return new long[arraySize];219else220if(typeName.equals("float[]"))221return new float[arraySize];222else223if(typeName.equals("double[]"))224return new double[arraySize];225else226{227throw new TestBug("Invalid primitive type array type name: " + typeName);228}229}230231}232233234