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