annotate agent/src/share/classes/sun/jvm/hotspot/utilities/SystemDictionaryHelper.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.utilities;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 public class SystemDictionaryHelper {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 static {
a61af66fc99e Initial load
duke
parents:
diff changeset
34 VM.registerVMInitializedObserver(new Observer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 public void update(Observable o, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
37 }
a61af66fc99e Initial load
duke
parents:
diff changeset
38 });
a61af66fc99e Initial load
duke
parents:
diff changeset
39 }
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 private static synchronized void initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 klasses = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 }
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // Instance klass array sorted by name.
a61af66fc99e Initial load
duke
parents:
diff changeset
46 private static InstanceKlass[] klasses;
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // side-effect!. caches the instance klass array.
a61af66fc99e Initial load
duke
parents:
diff changeset
49 public static synchronized InstanceKlass[] getAllInstanceKlasses() {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 if (klasses != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
51 return klasses;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 final Vector tmp = new Vector();
a61af66fc99e Initial load
duke
parents:
diff changeset
55 SystemDictionary dict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
56 dict.classesDo(new SystemDictionary.ClassVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 public void visit(Klass k) {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 if (k instanceof InstanceKlass) {
a61af66fc99e Initial load
duke
parents:
diff changeset
59 InstanceKlass ik = (InstanceKlass) k;
a61af66fc99e Initial load
duke
parents:
diff changeset
60 tmp.add(ik);
a61af66fc99e Initial load
duke
parents:
diff changeset
61 }
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63 });
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 Object[] tmpArray = tmp.toArray();
a61af66fc99e Initial load
duke
parents:
diff changeset
66 klasses = new InstanceKlass[tmpArray.length];
a61af66fc99e Initial load
duke
parents:
diff changeset
67 System.arraycopy(tmpArray, 0, klasses, 0, tmpArray.length);
a61af66fc99e Initial load
duke
parents:
diff changeset
68 Arrays.sort(klasses, new Comparator() {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 public int compare(Object o1, Object o2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 InstanceKlass k1 = (InstanceKlass) o1;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 InstanceKlass k2 = (InstanceKlass) o2;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 Symbol s1 = k1.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 Symbol s2 = k2.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
74 return s1.asString().compareTo(s2.asString());
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76 });
a61af66fc99e Initial load
duke
parents:
diff changeset
77 return klasses;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // returns array of instance klasses whose name contains given namePart
a61af66fc99e Initial load
duke
parents:
diff changeset
81 public static InstanceKlass[] findInstanceKlasses(String namePart) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 namePart = namePart.replace('.', '/');
a61af66fc99e Initial load
duke
parents:
diff changeset
83 InstanceKlass[] tmpKlasses = getAllInstanceKlasses();
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 Vector tmp = new Vector();
a61af66fc99e Initial load
duke
parents:
diff changeset
86 for (int i = 0; i < tmpKlasses.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 String name = tmpKlasses[i].getName().asString();
a61af66fc99e Initial load
duke
parents:
diff changeset
88 if (name.indexOf(namePart) != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 tmp.add(tmpKlasses[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
90 }
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 Object[] tmpArray = tmp.toArray();
a61af66fc99e Initial load
duke
parents:
diff changeset
94 InstanceKlass[] searchResult = new InstanceKlass[tmpArray.length];
a61af66fc99e Initial load
duke
parents:
diff changeset
95 System.arraycopy(tmpArray, 0, searchResult, 0, tmpArray.length);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return searchResult;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 // find first class whose name matches exactly the given argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
100 public static InstanceKlass findInstanceKlass(String className) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // convert to internal name
a61af66fc99e Initial load
duke
parents:
diff changeset
102 className = className.replace('.', '/');
a61af66fc99e Initial load
duke
parents:
diff changeset
103 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
104
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // check whether we have a bootstrap class of given name
a61af66fc99e Initial load
duke
parents:
diff changeset
106 Klass klass = sysDict.find(className, null, null);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (klass != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 return (InstanceKlass) klass;
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 // check whether we have a system class of given name
a61af66fc99e Initial load
duke
parents:
diff changeset
112 klass = sysDict.find(className, sysDict.javaSystemLoader(), null);
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if (klass != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 return (InstanceKlass) klass;
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // didn't find bootstrap or system class of given name.
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // search through the entire dictionary..
a61af66fc99e Initial load
duke
parents:
diff changeset
119 InstanceKlass[] tmpKlasses = getAllInstanceKlasses();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // instance klass array is sorted by name. do binary search
a61af66fc99e Initial load
duke
parents:
diff changeset
121 int low = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 int high = tmpKlasses.length-1;
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 int mid = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
125 while (low <= high) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 mid = (low + high) >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 InstanceKlass midVal = tmpKlasses[mid];
a61af66fc99e Initial load
duke
parents:
diff changeset
128 int cmp = midVal.getName().asString().compareTo(className);
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 if (cmp < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 low = mid + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 } else if (cmp > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 high = mid - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 } else { // match found
a61af66fc99e Initial load
duke
parents:
diff changeset
135 return tmpKlasses[mid];
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // no match ..
a61af66fc99e Initial load
duke
parents:
diff changeset
139 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 }
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }