0
|
1 /*
|
|
2 * Copyright 2002-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any 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 });
|
|
77 return klasses;
|
|
78 }
|
|
79
|
|
80 // returns array of instance klasses whose name contains given namePart
|
|
81 public static InstanceKlass[] findInstanceKlasses(String namePart) {
|
|
82 namePart = namePart.replace('.', '/');
|
|
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 }
|