1 /*
   2  * Copyright 1999-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // ciInstanceKlass
  26 //
  27 // This class represents a klassOop in the HotSpot virtual machine
  28 // whose Klass part is an instanceKlass.  It may or may not
  29 // be loaded.
  30 class ciInstanceKlass : public ciKlass {
  31   CI_PACKAGE_ACCESS
  32   friend class ciBytecodeStream;
  33   friend class ciEnv;
  34   friend class ciExceptionHandler;
  35   friend class ciMethod;
  36   friend class ciField;
  37 
  38 private:
  39   jobject                _loader;
  40   jobject                _protection_domain;
  41 
  42   bool                   _is_shared;
  43   bool                   _is_initialized;
  44   bool                   _is_linked;
  45   bool                   _has_finalizer;
  46   bool                   _has_subklass;
  47   bool                   _has_nonstatic_fields;
  48 
  49   ciFlags                _flags;
  50   jint                   _nonstatic_field_size;
  51   jint                   _nonstatic_oop_map_size;
  52 
  53   // Lazy fields get filled in only upon request.
  54   ciInstanceKlass*       _super;
  55   ciInstance*            _java_mirror;
  56 
  57   ciConstantPoolCache*   _field_cache;  // cached map index->field
  58   GrowableArray<ciField*>* _nonstatic_fields;
  59 
  60   enum { implementors_limit = instanceKlass::implementors_limit };
  61   ciInstanceKlass*       _implementors[implementors_limit];
  62   jint                   _nof_implementors;
  63 
  64   GrowableArray<ciField*>* _non_static_fields;
  65 
  66 protected:
  67   ciInstanceKlass(KlassHandle h_k);
  68   ciInstanceKlass(ciSymbol* name, jobject loader, jobject protection_domain);
  69 
  70   instanceKlass* get_instanceKlass() const {
  71     return (instanceKlass*)get_Klass();
  72   }
  73 
  74   oop loader();
  75   jobject loader_handle();
  76 
  77   oop protection_domain();
  78   jobject protection_domain_handle();
  79 
  80   const char* type_string() { return "ciInstanceKlass"; }
  81 
  82   void print_impl(outputStream* st);
  83 
  84   ciConstantPoolCache* field_cache();
  85 
  86   bool is_shared() { return _is_shared; }
  87 
  88   bool compute_shared_is_initialized();
  89   bool compute_shared_is_linked();
  90   bool compute_shared_has_subklass();
  91   int  compute_shared_nof_implementors();
  92   int  compute_nonstatic_fields();
  93   GrowableArray<ciField*>* compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields);
  94 
  95 public:
  96   // Has this klass been initialized?
  97   bool                   is_initialized() {
  98     if (_is_shared && !_is_initialized) {
  99       return is_loaded() && compute_shared_is_initialized();
 100     }
 101     return _is_initialized;
 102   }
 103   // Has this klass been linked?
 104   bool                   is_linked() {
 105     if (_is_shared && !_is_linked) {
 106       return is_loaded() && compute_shared_is_linked();
 107     }
 108     return _is_linked;
 109   }
 110 
 111   // General klass information.
 112   ciFlags                flags()          {
 113     assert(is_loaded(), "must be loaded");
 114     return _flags;
 115   }
 116   bool                   has_finalizer()  {
 117     assert(is_loaded(), "must be loaded");
 118     return _has_finalizer; }
 119   bool                   has_subklass()   {
 120     assert(is_loaded(), "must be loaded");
 121     if (_is_shared && !_has_subklass) {
 122       if (flags().is_final()) {
 123         return false;
 124       } else {
 125         return compute_shared_has_subklass();
 126       }
 127     }
 128     return _has_subklass;
 129   }
 130   jint                   size_helper()  {
 131     return (Klass::layout_helper_size_in_bytes(layout_helper())
 132             >> LogHeapWordSize);
 133   }
 134   jint                   nonstatic_field_size()  {
 135     assert(is_loaded(), "must be loaded");
 136     return _nonstatic_field_size; }
 137   jint                   has_nonstatic_fields()  {
 138     assert(is_loaded(), "must be loaded");
 139     return _has_nonstatic_fields; }
 140   jint                   nonstatic_oop_map_size()  {
 141     assert(is_loaded(), "must be loaded");
 142     return _nonstatic_oop_map_size; }
 143   ciInstanceKlass*       super();
 144   jint                   nof_implementors()  {
 145     assert(is_loaded(), "must be loaded");
 146     if (_is_shared)  return compute_shared_nof_implementors();
 147     return _nof_implementors;
 148   }
 149 
 150   ciInstanceKlass* get_canonical_holder(int offset);
 151   ciField* get_field_by_offset(int field_offset, bool is_static);
 152   ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
 153 
 154   GrowableArray<ciField*>* non_static_fields();
 155 
 156   // total number of nonstatic fields (including inherited):
 157   int nof_nonstatic_fields() {
 158     if (_nonstatic_fields == NULL)
 159       return compute_nonstatic_fields();
 160     else
 161       return _nonstatic_fields->length();
 162   }
 163   // nth nonstatic field (presented by ascending address)
 164   ciField* nonstatic_field_at(int i) {
 165     assert(_nonstatic_fields != NULL, "");
 166     return _nonstatic_fields->at(i);
 167   }
 168 
 169   ciInstanceKlass* unique_concrete_subklass();
 170   bool has_finalizable_subclass();
 171 
 172   bool contains_field_offset(int offset) {
 173     return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
 174   }
 175 
 176   // Get the instance of java.lang.Class corresponding to
 177   // this klass.  This instance is used for locking of
 178   // synchronized static methods of this klass.
 179   ciInstance*            java_mirror();
 180 
 181   // Java access flags
 182   bool is_public      () { return flags().is_public(); }
 183   bool is_final       () { return flags().is_final(); }
 184   bool is_super       () { return flags().is_super(); }
 185   bool is_interface   () { return flags().is_interface(); }
 186   bool is_abstract    () { return flags().is_abstract(); }
 187 
 188   ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
 189   // Note:  To find a method from name and type strings, use ciSymbol::make,
 190   // but consider adding to vmSymbols.hpp instead.
 191 
 192   bool is_leaf_type();
 193   ciInstanceKlass* implementor(int n);
 194 
 195   // Is the defining class loader of this class the default loader?
 196   bool uses_default_loader();
 197 
 198   bool is_java_lang_Object();
 199 
 200   // What kind of ciObject is this?
 201   bool is_instance_klass() { return true; }
 202   bool is_java_klass()     { return true; }
 203 };