1 /* 2 * Copyright (c) 2002, 2005, 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 package sun.jvm.hotspot.utilities; 26 27 import java.util.*; 28 import sun.jvm.hotspot.classfile.*; 29 import sun.jvm.hotspot.oops.*; 30 import sun.jvm.hotspot.memory.*; 31 import sun.jvm.hotspot.runtime.*; 32 33 public class SystemDictionaryHelper { 34 static { 35 VM.registerVMInitializedObserver(new Observer() { 36 public void update(Observable o, Object data) { 37 initialize(); 38 } 39 }); 40 } 41 42 private static synchronized void initialize() { 43 klasses = null; 44 } 45 46 // Instance klass array sorted by name. 47 private static InstanceKlass[] klasses; 48 49 // side-effect!. caches the instance klass array. 50 public static synchronized InstanceKlass[] getAllInstanceKlasses() { 51 if (klasses != null) { 52 return klasses; 53 } 54 55 final Vector tmp = new Vector(); 56 ClassLoaderDataGraph cldg = VM.getVM().getClassLoaderDataGraph(); 57 cldg.classesDo(new ClassLoaderDataGraph.ClassVisitor() { 58 public void visit(Klass k) { 59 if (k instanceof InstanceKlass) { 60 InstanceKlass ik = (InstanceKlass) k; 61 tmp.add(ik); 62 } 63 } 64 }); 65 66 Object[] tmpArray = tmp.toArray(); 67 klasses = new InstanceKlass[tmpArray.length]; 68 System.arraycopy(tmpArray, 0, klasses, 0, tmpArray.length); 69 Arrays.sort(klasses, new Comparator() { 70 public int compare(Object o1, Object o2) { 71 InstanceKlass k1 = (InstanceKlass) o1; 72 InstanceKlass k2 = (InstanceKlass) o2; 73 Symbol s1 = k1.getName(); 74 Symbol s2 = k2.getName(); 75 return s1.asString().compareTo(s2.asString()); 76 } 77 }); 78 return klasses; 79 } 80 81 // returns array of instance klasses whose name contains given namePart 82 public static InstanceKlass[] findInstanceKlasses(String namePart) { 83 namePart = namePart.replace('.', '/'); 84 InstanceKlass[] tmpKlasses = getAllInstanceKlasses(); 85 86 Vector tmp = new Vector(); 87 for (int i = 0; i < tmpKlasses.length; i++) { 88 String name = tmpKlasses[i].getName().asString(); 89 if (name.indexOf(namePart) != -1) { 90 tmp.add(tmpKlasses[i]); 91 } 92 } 93 94 Object[] tmpArray = tmp.toArray(); 95 InstanceKlass[] searchResult = new InstanceKlass[tmpArray.length]; 96 System.arraycopy(tmpArray, 0, searchResult, 0, tmpArray.length); 97 return searchResult; 98 } 99 100 // find first class whose name matches exactly the given argument. 101 public static InstanceKlass findInstanceKlass(String className) { 102 // convert to internal name 103 className = className.replace('.', '/'); 104 ClassLoaderDataGraph cldg = VM.getVM().getClassLoaderDataGraph(); 105 106 // check whether we have a class of given name 107 Klass klass = cldg.find(className); 108 if (klass != null && klass instanceof InstanceKlass) { 109 return (InstanceKlass) klass; 110 } else { 111 // no match .. 112 return null; 113 } 114 } 115 }