1 /*
   2  * Copyright (c) 1999, 2013, 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 #ifndef SHARE_VM_CI_CIINSTANCEKLASS_HPP
  26 #define SHARE_VM_CI_CIINSTANCEKLASS_HPP
  27 
  28 #include "ci/ciConstantPoolCache.hpp"
  29 #include "ci/ciFlags.hpp"
  30 #include "ci/ciKlass.hpp"
  31 #include "ci/ciSymbol.hpp"
  32 
  33 // ciInstanceKlass
  34 //
  35 // This class represents a Klass* in the HotSpot virtual machine
  36 // whose Klass part is an InstanceKlass.  It may or may not
  37 // be loaded.
  38 class ciInstanceKlass : public ciKlass {
  39   CI_PACKAGE_ACCESS
  40   friend class ciBytecodeStream;
  41   friend class ciEnv;
  42   friend class ciExceptionHandler;
  43   friend class ciMethod;
  44   friend class ciField;
  45 
  46 private:
  47   jobject                _loader;
  48   jobject                _protection_domain;
  49 
  50   InstanceKlass::ClassState _init_state;           // state of class
  51   bool                   _is_shared;
  52   bool                   _has_finalizer;
  53   bool                   _has_subklass;
  54   bool                   _has_nonstatic_fields;
  55   bool                   _has_default_methods;
  56 
  57   ciFlags                _flags;
  58   jint                   _nonstatic_field_size;
  59   jint                   _nonstatic_oop_map_size;
  60 
  61   // Lazy fields get filled in only upon request.
  62   ciInstanceKlass*       _super;
  63   ciInstance*            _java_mirror;
  64 
  65   ciConstantPoolCache*   _field_cache;  // cached map index->field
  66   GrowableArray<ciField*>* _nonstatic_fields;
  67 
  68   // The possible values of the _implementor fall into following three cases:
  69   //   NULL: no implementor.
  70   //   A ciInstanceKlass that's not itself: one implementor.
  71   //   Itsef: more than one implementors.
  72   ciInstanceKlass*       _implementor;
  73 
  74 protected:
  75   ciInstanceKlass(KlassHandle h_k);
  76   ciInstanceKlass(ciSymbol* name, jobject loader, jobject protection_domain);
  77 
  78   InstanceKlass* get_instanceKlass() const {
  79     return (InstanceKlass*)get_Klass();
  80   }
  81 
  82   oop loader();
  83   jobject loader_handle();
  84 
  85   oop protection_domain();
  86   jobject protection_domain_handle();
  87 
  88   const char* type_string() { return "ciInstanceKlass"; }
  89 
  90   bool is_in_package_impl(const char* packagename, int len);
  91 
  92   void print_impl(outputStream* st);
  93 
  94   ciConstantPoolCache* field_cache();
  95 
  96   bool is_shared() { return _is_shared; }
  97 
  98   void compute_shared_init_state();
  99   bool compute_shared_has_subklass();
 100   int  compute_nonstatic_fields();
 101   GrowableArray<ciField*>* compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields);
 102 
 103   // Update the init_state for shared klasses
 104   void update_if_shared(InstanceKlass::ClassState expected) {
 105     if (_is_shared && _init_state != expected) {
 106       if (is_loaded()) compute_shared_init_state();
 107     }
 108   }
 109 
 110 public:
 111   // Has this klass been initialized?
 112   bool                   is_initialized() {
 113     update_if_shared(InstanceKlass::fully_initialized);
 114     return _init_state == InstanceKlass::fully_initialized;
 115   }
 116   // Is this klass being initialized?
 117   bool                   is_being_initialized() {
 118     update_if_shared(InstanceKlass::being_initialized);
 119     return _init_state == InstanceKlass::being_initialized;
 120   }
 121   // Has this klass been linked?
 122   bool                   is_linked() {
 123     update_if_shared(InstanceKlass::linked);
 124     return _init_state >= InstanceKlass::linked;
 125   }
 126 
 127   // General klass information.
 128   ciFlags                flags()          {
 129     assert(is_loaded(), "must be loaded");
 130     return _flags;
 131   }
 132   bool                   has_finalizer()  {
 133     assert(is_loaded(), "must be loaded");
 134     return _has_finalizer; }
 135   bool                   has_subklass()   {
 136     assert(is_loaded(), "must be loaded");
 137     if (_is_shared && !_has_subklass) {
 138       if (flags().is_final()) {
 139         return false;
 140       } else {
 141         return compute_shared_has_subklass();
 142       }
 143     }
 144     return _has_subklass;
 145   }
 146   jint                   size_helper()  {
 147     return (Klass::layout_helper_size_in_bytes(layout_helper())
 148             >> LogHeapWordSize);
 149   }
 150   jint                   nonstatic_field_size()  {
 151     assert(is_loaded(), "must be loaded");
 152     return _nonstatic_field_size; }
 153   jint                   has_nonstatic_fields()  {
 154     assert(is_loaded(), "must be loaded");
 155     return _has_nonstatic_fields; }
 156   jint                   nonstatic_oop_map_size()  {
 157     assert(is_loaded(), "must be loaded");
 158     return _nonstatic_oop_map_size; }
 159   ciInstanceKlass*       super();
 160   jint                   nof_implementors() {
 161     ciInstanceKlass* impl;
 162     assert(is_loaded(), "must be loaded");
 163     impl = implementor();
 164     if (impl == NULL) {
 165       return 0;
 166     } else if (impl != this) {
 167       return 1;
 168     } else {
 169       return 2;
 170     }
 171   }
 172 
 173   bool has_default_methods()  {
 174     assert(is_loaded(), "must be loaded");
 175     return _has_default_methods;
 176   }
 177 
 178   ciInstanceKlass* get_canonical_holder(int offset);
 179   ciField* get_field_by_offset(int field_offset, bool is_static);
 180   ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
 181 
 182   // total number of nonstatic fields (including inherited):
 183   int nof_nonstatic_fields() {
 184     if (_nonstatic_fields == NULL)
 185       return compute_nonstatic_fields();
 186     else
 187       return _nonstatic_fields->length();
 188   }
 189   // nth nonstatic field (presented by ascending address)
 190   ciField* nonstatic_field_at(int i) {
 191     assert(_nonstatic_fields != NULL, "");
 192     return _nonstatic_fields->at(i);
 193   }
 194 
 195   ciInstanceKlass* unique_concrete_subklass();
 196   bool has_finalizable_subclass();
 197 
 198   bool contains_field_offset(int offset) {
 199     return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
 200   }
 201 
 202   // Get the instance of java.lang.Class corresponding to
 203   // this klass.  This instance is used for locking of
 204   // synchronized static methods of this klass.
 205   ciInstance*            java_mirror();
 206 
 207   // Java access flags
 208   bool is_public      () { return flags().is_public(); }
 209   bool is_final       () { return flags().is_final(); }
 210   bool is_super       () { return flags().is_super(); }
 211   bool is_interface   () { return flags().is_interface(); }
 212   bool is_abstract    () { return flags().is_abstract(); }
 213 
 214   ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
 215   // Note:  To find a method from name and type strings, use ciSymbol::make,
 216   // but consider adding to vmSymbols.hpp instead.
 217 
 218   bool is_leaf_type();
 219   ciInstanceKlass* implementor();
 220 
 221   // Is the defining class loader of this class the default loader?
 222   bool uses_default_loader() const;
 223 
 224   bool is_java_lang_Object() const;
 225 
 226   BasicType box_klass_type() const;
 227   bool is_box_klass() const;
 228   bool is_boxed_value_offset(int offset) const;
 229 
 230   // Is this klass in the given package?
 231   bool is_in_package(const char* packagename) {
 232     return is_in_package(packagename, (int) strlen(packagename));
 233   }
 234   bool is_in_package(const char* packagename, int len);
 235 
 236   // What kind of ciObject is this?
 237   bool is_instance_klass() const { return true; }
 238   bool is_java_klass() const     { return true; }
 239 
 240   virtual ciKlass* exact_klass() {
 241     if (is_loaded() && is_final() && !is_interface()) {
 242       return this;
 243     }
 244     return NULL;
 245   }
 246 
 247   // Dump the current state of this klass for compilation replay.
 248   virtual void dump_replay_data(outputStream* out);
 249 };
 250 
 251 #endif // SHARE_VM_CI_CIINSTANCEKLASS_HPP