View Javadoc
1   /*
2    * Copyright (c) 2001, 2012, 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.memory;
26  
27  import java.util.*;
28  import sun.jvm.hotspot.debugger.*;
29  import sun.jvm.hotspot.oops.*;
30  import sun.jvm.hotspot.runtime.*;
31  import sun.jvm.hotspot.types.*;
32  
33  public class SystemDictionary {
34    private static AddressField dictionaryField;
35    private static AddressField sharedDictionaryField;
36    private static AddressField placeholdersField;
37    private static AddressField loaderConstraintTableField;
38    private static sun.jvm.hotspot.types.OopField javaSystemLoaderField;
39  
40    private static AddressField objectKlassField;
41    private static AddressField classLoaderKlassField;
42    private static AddressField stringKlassField;
43    private static AddressField systemKlassField;
44    private static AddressField threadKlassField;
45    private static AddressField threadGroupKlassField;
46    private static AddressField methodHandleKlassField;
47  
48    static {
49      VM.registerVMInitializedObserver(new Observer() {
50          public void update(Observable o, Object data) {
51            initialize(VM.getVM().getTypeDataBase());
52          }
53        });
54    }
55  
56    private static synchronized void initialize(TypeDataBase db) {
57      Type type = db.lookupType("SystemDictionary");
58  
59      dictionaryField = type.getAddressField("_dictionary");
60      sharedDictionaryField = type.getAddressField("_shared_dictionary");
61      placeholdersField = type.getAddressField("_placeholders");
62      loaderConstraintTableField = type.getAddressField("_loader_constraints");
63      javaSystemLoaderField = type.getOopField("_java_system_loader");
64  
65      objectKlassField = type.getAddressField(WK_KLASS("Object_klass"));
66      classLoaderKlassField = type.getAddressField(WK_KLASS("ClassLoader_klass"));
67      stringKlassField = type.getAddressField(WK_KLASS("String_klass"));
68      systemKlassField = type.getAddressField(WK_KLASS("System_klass"));
69      threadKlassField = type.getAddressField(WK_KLASS("Thread_klass"));
70      threadGroupKlassField = type.getAddressField(WK_KLASS("ThreadGroup_klass"));
71      methodHandleKlassField = type.getAddressField(WK_KLASS("MethodHandle_klass"));
72    }
73  
74    // This WK functions must follow the definitions in systemDictionary.hpp:
75    private static String WK_KLASS(String name) {
76        //#define WK_KLASS(name) _well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)]
77        return ("_well_known_klasses[SystemDictionary::"+WK_KLASS_ENUM_NAME(name)+"]");
78    }
79    private static String WK_KLASS_ENUM_NAME(String kname) {
80        //#define WK_KLASS_ENUM_NAME(kname)    kname##_knum
81        return (kname+"_knum");
82    }
83  
84    public Dictionary dictionary() {
85      Address tmp = dictionaryField.getValue();
86      return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
87    }
88  
89    public Dictionary sharedDictionary() {
90      Address tmp = sharedDictionaryField.getValue();
91      return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);
92    }
93  
94    public PlaceholderTable placeholders() {
95      Address tmp = placeholdersField.getValue();
96      return (PlaceholderTable) VMObjectFactory.newObject(PlaceholderTable.class, tmp);
97    }
98  
99    public LoaderConstraintTable constraints() {
100     Address tmp = placeholdersField.getValue();
101     return (LoaderConstraintTable) VMObjectFactory.newObject(LoaderConstraintTable.class, tmp);
102   }
103 
104   // few well known classes -- not all are added here.
105   // add more if needed.
106   public static InstanceKlass getThreadKlass() {
107     return (InstanceKlass)Metadata.instantiateWrapperFor(threadKlassField.getValue());
108   }
109 
110   public static InstanceKlass getThreadGroupKlass() {
111     return (InstanceKlass)Metadata.instantiateWrapperFor(threadGroupKlassField.getValue());
112   }
113 
114   public static InstanceKlass getObjectKlass() {
115     return (InstanceKlass)Metadata.instantiateWrapperFor(objectKlassField.getValue());
116   }
117 
118   public static InstanceKlass getStringKlass() {
119     return (InstanceKlass)Metadata.instantiateWrapperFor(stringKlassField.getValue());
120   }
121 
122   public static InstanceKlass getClassLoaderKlass() {
123     return (InstanceKlass)Metadata.instantiateWrapperFor(classLoaderKlassField.getValue());
124   }
125 
126   public static InstanceKlass getSystemKlass() {
127     return (InstanceKlass)Metadata.instantiateWrapperFor(systemKlassField.getValue());
128   }
129 
130   public static InstanceKlass getMethodHandleKlass() {
131     return (InstanceKlass)Metadata.instantiateWrapperFor(methodHandleKlassField.getValue());
132   }
133 
134   public InstanceKlass getAbstractOwnableSynchronizerKlass() {
135     return (InstanceKlass) find("java/util/concurrent/locks/AbstractOwnableSynchronizer",
136                                 null, null);
137   }
138 
139   public static Oop javaSystemLoader() {
140     return newOop(javaSystemLoaderField.getValue());
141   }
142 
143   private static Oop newOop(OopHandle handle) {
144     return VM.getVM().getObjectHeap().newOop(handle);
145   }
146 
147   /** Lookup an already loaded class. If not found null is returned. */
148   public Klass find(String className, Oop classLoader, Oop protectionDomain) {
149     Symbol sym = VM.getVM().getSymbolTable().probe(className);
150     if (sym == null) return null;
151     return find(sym, classLoader, protectionDomain);
152   }
153 
154   /** Lookup an already loaded class. If not found null is returned. */
155   public Klass find(Symbol className, Oop classLoader, Oop protectionDomain) {
156     Dictionary dict = dictionary();
157     long hash = dict.computeHash(className, classLoader);
158     int index = dict.hashToIndex(hash);
159     return dict.find(index, hash, className, classLoader, protectionDomain);
160   }
161 
162   /** Interface for iterating through all classes in dictionary */
163   public static interface ClassVisitor {
164     public void visit(Klass k);
165   }
166 
167   /** Interface for iterating through all classes and their class
168       loaders in dictionary */
169   public static interface ClassAndLoaderVisitor {
170     public void visit(Klass k, Oop loader);
171   }
172 
173   /** Iterate over all klasses - including object, primitive
174       array klasses */
175   public void allClassesDo(final ClassVisitor v) {
176     ClassVisitor visitor = new ClassVisitor() {
177       public void visit(Klass k) {
178         for (Klass l = k; l != null; l = l.arrayKlassOrNull()) {
179           v.visit(l);
180         }
181       }
182     };
183     classesDo(visitor);
184     VM.getVM().getUniverse().basicTypeClassesDo(visitor);
185   }
186 
187   /** Iterate over all klasses in dictionary; just the classes from
188       declaring class loaders */
189   public void classesDo(ClassVisitor v) {
190     dictionary().classesDo(v);
191   }
192 
193   /** All classes, and their class loaders */
194   public void classesDo(ClassAndLoaderVisitor v) {
195     dictionary().classesDo(v);
196   }
197 
198   /** All array classes of primitive type, and their class loaders */
199   public void primArrayClassesDo(ClassAndLoaderVisitor v) {
200     placeholders().primArrayClassesDo(v);
201   }
202 }