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.oops.*; 29 import sun.jvm.hotspot.memory.*; 30 import sun.jvm.hotspot.runtime.*; 31 32 public class SystemDictionaryHelper { 33 static { 34 VM.registerVMInitializedObserver(new Observer() { 35 public void update(Observable o, Object data) { 36 initialize(); 37 } 38 }); 39 } 40 41 private static synchronized void initialize() { 42 klasses = null; 43 } 44 45 // Instance klass array sorted by name. 46 private static InstanceKlass[] klasses; 47 48 // side-effect!. caches the instance klass array. 49 public static synchronized InstanceKlass[] getAllInstanceKlasses() { 50 if (klasses != null) { 51 return klasses; 52 } 53 54 final Vector tmp = new Vector(); 55 SystemDictionary dict = VM.getVM().getSystemDictionary(); 56 dict.classesDo(new SystemDictionary.ClassVisitor() { 57 public void visit(Klass k) { 58 if (k instanceof InstanceKlass) { 59 InstanceKlass ik = (InstanceKlass) k; 60 tmp.add(ik); 61 } 62 } 63 }); 64 65 Object[] tmpArray = tmp.toArray(); 66 klasses = new InstanceKlass[tmpArray.length]; 67 System.arraycopy(tmpArray, 0, klasses, 0, tmpArray.length); 68 Arrays.sort(klasses, new Comparator() { 69 public int compare(Object o1, Object o2) { 70 InstanceKlass k1 = (InstanceKlass) o1; 71 InstanceKlass k2 = (InstanceKlass) o2; 72 Symbol s1 = k1.getName(); 73 Symbol s2 = k2.getName(); 74 return s1.asString().compareTo(s2.asString()); 75 } 76 }); 83 InstanceKlass[] tmpKlasses = getAllInstanceKlasses(); 84 85 Vector tmp = new Vector(); 86 for (int i = 0; i < tmpKlasses.length; i++) { 87 String name = tmpKlasses[i].getName().asString(); 88 if (name.indexOf(namePart) != -1) { 89 tmp.add(tmpKlasses[i]); 90 } 91 } 92 93 Object[] tmpArray = tmp.toArray(); 94 InstanceKlass[] searchResult = new InstanceKlass[tmpArray.length]; 95 System.arraycopy(tmpArray, 0, searchResult, 0, tmpArray.length); 96 return searchResult; 97 } 98 99 // find first class whose name matches exactly the given argument. 100 public static InstanceKlass findInstanceKlass(String className) { 101 // convert to internal name 102 className = className.replace('.', '/'); 103 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 104 105 // check whether we have a bootstrap class of given name 106 Klass klass = sysDict.find(className, null, null); 107 if (klass != null) { 108 return (InstanceKlass) klass; 109 } 110 111 // check whether we have a system class of given name 112 klass = sysDict.find(className, sysDict.javaSystemLoader(), null); 113 if (klass != null) { 114 return (InstanceKlass) klass; 115 } 116 117 // didn't find bootstrap or system class of given name. 118 // search through the entire dictionary.. 119 InstanceKlass[] tmpKlasses = getAllInstanceKlasses(); 120 // instance klass array is sorted by name. do binary search 121 int low = 0; 122 int high = tmpKlasses.length-1; 123 124 int mid = -1; 125 while (low <= high) { 126 mid = (low + high) >> 1; 127 InstanceKlass midVal = tmpKlasses[mid]; 128 int cmp = midVal.getName().asString().compareTo(className); 129 130 if (cmp < 0) { 131 low = mid + 1; 132 } else if (cmp > 0) { 133 high = mid - 1; 134 } else { // match found 135 return tmpKlasses[mid]; 136 } 137 } 138 // no match .. 139 return null; 140 } 141 } | 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 }); 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 } |