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