1 /* 2 * Copyright (c) 2000, 2025, 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.oops; 26 27 import java.io.*; 28 import java.util.*; 29 import sun.jvm.hotspot.debugger.*; 30 import sun.jvm.hotspot.classfile.*; 31 import sun.jvm.hotspot.runtime.*; 32 import sun.jvm.hotspot.types.*; 33 import sun.jvm.hotspot.utilities.Observable; 34 import sun.jvm.hotspot.utilities.Observer; 35 36 public class Klass extends Metadata implements ClassConstants { 37 static { 38 VM.registerVMInitializedObserver(new Observer() { 39 public void update(Observable o, Object data) { 40 initialize(VM.getVM().getTypeDataBase()); 41 } 42 }); 43 } 44 45 // anon-enum constants for _layout_helper. 46 public static int LH_INSTANCE_SLOW_PATH_BIT; 47 public static int LH_LOG2_ELEMENT_SIZE_SHIFT; 48 public static int LH_ELEMENT_TYPE_SHIFT; 49 public static int LH_HEADER_SIZE_SHIFT; 50 public static int LH_ARRAY_TAG_SHIFT; 51 public static int LH_ARRAY_TAG_TYPE_VALUE; 52 53 54 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { 55 Type type = db.lookupType("Klass"); 56 javaMirrorFieldOffset = type.getField("_java_mirror").getOffset(); 57 superField = new MetadataField(type.getAddressField("_super"), 0); 58 layoutHelper = new IntField(type.getJIntField("_layout_helper"), 0); 59 name = type.getAddressField("_name"); 60 accessFlags = new CIntField(type.getCIntegerField("_access_flags"), 0); 61 try { 62 traceIDField = type.getField("_trace_id"); 63 } catch(Exception e) { 64 } 65 subklass = new MetadataField(type.getAddressField("_subklass"), 0); 66 nextSibling = new MetadataField(type.getAddressField("_next_sibling"), 0); 67 nextLink = new MetadataField(type.getAddressField("_next_link"), 0); 68 vtableLen = new CIntField(type.getCIntegerField("_vtable_len"), 0); 69 classLoaderData = type.getAddressField("_class_loader_data"); 70 71 LH_INSTANCE_SLOW_PATH_BIT = db.lookupIntConstant("Klass::_lh_instance_slow_path_bit").intValue(); 72 LH_LOG2_ELEMENT_SIZE_SHIFT = db.lookupIntConstant("Klass::_lh_log2_element_size_shift").intValue(); 73 LH_ELEMENT_TYPE_SHIFT = db.lookupIntConstant("Klass::_lh_element_type_shift").intValue(); 74 LH_HEADER_SIZE_SHIFT = db.lookupIntConstant("Klass::_lh_header_size_shift").intValue(); 75 LH_ARRAY_TAG_SHIFT = db.lookupIntConstant("Klass::_lh_array_tag_shift").intValue(); 76 LH_ARRAY_TAG_TYPE_VALUE = db.lookupIntConstant("Klass::_lh_array_tag_type_value").intValue(); 77 } 78 79 80 public Klass(Address addr) { 81 super(addr); 82 } 83 84 // jvmdi support - see also class_status in VM code 85 public int getClassStatus() { 86 return 0; // overridden in derived classes 87 } 88 89 public boolean isKlass() { return true; } 90 public boolean isArrayKlass() { return false; } 91 92 // Fields 93 private static long javaMirrorFieldOffset; 94 private static MetadataField superField; 95 private static IntField layoutHelper; 96 private static AddressField name; 97 private static CIntField accessFlags; 98 private static MetadataField subklass; 99 private static MetadataField nextSibling; 100 private static MetadataField nextLink; 101 private static sun.jvm.hotspot.types.Field traceIDField; 102 private static CIntField vtableLen; 103 private static AddressField classLoaderData; 104 105 protected Symbol getSymbol(AddressField field) { 106 return Symbol.create(addr.getAddressAt(field.getOffset())); 107 } 108 109 // Accessors for declared fields 110 public Instance getJavaMirror() { 111 Address addr = getAddress().addOffsetTo(javaMirrorFieldOffset); 112 VMOopHandle vmOopHandle = VMObjectFactory.newObject(VMOopHandle.class, addr); 113 return vmOopHandle.resolve(); 114 } 115 public Klass getSuper() { return (Klass) superField.getValue(this); } 116 public Klass getJavaSuper() { return null; } 117 public int getLayoutHelper() { return layoutHelper.getValue(this); } 118 public Symbol getName() { return getSymbol(name); } 119 public long getAccessFlags() { return accessFlags.getValue(this); } 120 // Convenience routine 121 public AccessFlags getAccessFlagsObj(){ return new AccessFlags(getAccessFlags()); } 122 public Klass getSubklassKlass() { return (Klass) subklass.getValue(this); } 123 public Klass getNextSiblingKlass() { return (Klass) nextSibling.getValue(this); } 124 public Klass getNextLinkKlass() { return (Klass) nextLink.getValue(this); } 125 public long getVtableLen() { return vtableLen.getValue(this); } 126 127 public ClassLoaderData getClassLoaderData() { return ClassLoaderData.instantiateWrapperFor(classLoaderData.getValue(getAddress())); } 128 public Oop getClassLoader() { return getClassLoaderData().getClassLoader(); } 129 130 public long traceID() { 131 if (traceIDField == null) return 0; 132 return traceIDField.getJLong(addr); 133 } 134 135 // subclass check 136 public boolean isSubclassOf(Klass k) { 137 if (k != null) { 138 Klass t = this; 139 // Run up the super chain and check 140 while (t != null) { 141 if (t.equals(k)) return true; 142 t = t.getSuper(); 143 } 144 } 145 return false; 146 } 147 148 // subtype check 149 public boolean isSubtypeOf(Klass k) { 150 return computeSubtypeOf(k); 151 } 152 153 boolean computeSubtypeOf(Klass k) { 154 return isSubclassOf(k); 155 } 156 157 // Find LCA (Least Common Ancester) in class hierarchy 158 public Klass lca( Klass k2 ) { 159 Klass k1 = this; 160 while ( true ) { 161 if ( k1.isSubtypeOf(k2) ) return k2; 162 if ( k2.isSubtypeOf(k1) ) return k1; 163 k1 = k1.getSuper(); 164 k2 = k2.getSuper(); 165 } 166 } 167 168 public void printValueOn(PrintStream tty) { 169 tty.print("Klass"); 170 } 171 172 public void iterateFields(MetadataVisitor visitor) { 173 // visitor.doOop(javaMirror, true); 174 visitor.doMetadata(superField, true); 175 visitor.doInt(layoutHelper, true); 176 // visitor.doOop(name, true); 177 visitor.doCInt(accessFlags, true); 178 visitor.doMetadata(subklass, true); 179 visitor.doMetadata(nextSibling, true); 180 visitor.doCInt(vtableLen, true); 181 } 182 183 public long getObjectSize() { 184 throw new RuntimeException("should not reach here"); 185 } 186 187 /** Array class with specific rank */ 188 public Klass arrayKlass(int rank) { return arrayKlassImpl(false, rank); } 189 /** Array class with this klass as element type */ 190 public Klass arrayKlass() { return arrayKlassImpl(false); } 191 /** These will return null instead of allocating on the heap */ 192 public Klass arrayKlassOrNull(int rank) { return arrayKlassImpl(true, rank); } 193 public Klass arrayKlassOrNull() { return arrayKlassImpl(true); } 194 195 public Klass arrayKlassImpl(boolean orNull, int rank) { 196 throw new RuntimeException("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); 197 } 198 199 public Klass arrayKlassImpl(boolean orNull) { 200 throw new RuntimeException("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); 201 } 202 203 // This returns the name in the form java/lang/String which isn't really a signature 204 // The subclasses override this to produce the correct form, eg 205 // Ljava/lang/String; For ArrayKlasses getName itself is the signature. 206 public String signature() { return getName().asString(); } 207 208 // Convenience routines 209 public boolean isPublic() { return getAccessFlagsObj().isPublic(); } 210 public boolean isFinal() { return getAccessFlagsObj().isFinal(); } 211 public boolean isInterface() { return getAccessFlagsObj().isInterface(); } 212 public boolean isAbstract() { return getAccessFlagsObj().isAbstract(); } 213 public boolean isSuper() { return getAccessFlagsObj().isSuper(); } 214 public boolean isSynthetic() { return getAccessFlagsObj().isSynthetic(); } 215 }