instanceKlass.hpp revision 8413:92457dfb91bd
1/*
2 * Copyright (c) 1997, 2015, 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_OOPS_INSTANCEKLASS_HPP
26#define SHARE_VM_OOPS_INSTANCEKLASS_HPP
27
28#include "classfile/classLoaderData.hpp"
29#include "gc/shared/specialized_oop_closures.hpp"
30#include "memory/referenceType.hpp"
31#include "oops/annotations.hpp"
32#include "oops/constMethod.hpp"
33#include "oops/fieldInfo.hpp"
34#include "oops/instanceOop.hpp"
35#include "oops/klassVtable.hpp"
36#include "runtime/handles.hpp"
37#include "runtime/os.hpp"
38#include "trace/traceMacros.hpp"
39#include "utilities/accessFlags.hpp"
40#include "utilities/bitMap.inline.hpp"
41#include "utilities/macros.hpp"
42
43// An InstanceKlass is the VM level representation of a Java class.
44// It contains all information needed for at class at execution runtime.
45
46//  InstanceKlass embedded field layout (after declared fields):
47//    [EMBEDDED Java vtable             ] size in words = vtable_len
48//    [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
49//      The embedded nonstatic oop-map blocks are short pairs (offset, length)
50//      indicating where oops are located in instances of this klass.
51//    [EMBEDDED implementor of the interface] only exist for interface
52//    [EMBEDDED host klass        ] only exist for an anonymous class (JSR 292 enabled)
53
54
55// forward declaration for class -- see below for definition
56class SuperTypeClosure;
57class JNIid;
58class jniIdMapBase;
59class BreakpointInfo;
60class fieldDescriptor;
61class DepChange;
62class nmethodBucket;
63class JvmtiCachedClassFieldMap;
64class MemberNameTable;
65
66// This is used in iterators below.
67class FieldClosure: public StackObj {
68public:
69  virtual void do_field(fieldDescriptor* fd) = 0;
70};
71
72#ifndef PRODUCT
73// Print fields.
74// If "obj" argument to constructor is NULL, prints static fields, otherwise prints non-static fields.
75class FieldPrinter: public FieldClosure {
76   oop _obj;
77   outputStream* _st;
78 public:
79   FieldPrinter(outputStream* st, oop obj = NULL) : _obj(obj), _st(st) {}
80   void do_field(fieldDescriptor* fd);
81};
82#endif  // !PRODUCT
83
84// ValueObjs embedded in klass. Describes where oops are located in instances of
85// this klass.
86class OopMapBlock VALUE_OBJ_CLASS_SPEC {
87 public:
88  // Byte offset of the first oop mapped by this block.
89  int offset() const          { return _offset; }
90  void set_offset(int offset) { _offset = offset; }
91
92  // Number of oops in this block.
93  uint count() const         { return _count; }
94  void set_count(uint count) { _count = count; }
95
96  // sizeof(OopMapBlock) in HeapWords.
97  static const int size_in_words() {
98    return align_size_up(int(sizeof(OopMapBlock)), HeapWordSize) >>
99      LogHeapWordSize;
100  }
101
102 private:
103  int  _offset;
104  uint _count;
105};
106
107struct JvmtiCachedClassFileData;
108
109class InstanceKlass: public Klass {
110  friend class VMStructs;
111  friend class ClassFileParser;
112  friend class CompileReplay;
113
114 protected:
115  // Constructor
116  InstanceKlass(int vtable_len,
117                int itable_len,
118                int static_field_size,
119                int nonstatic_oop_map_size,
120                ReferenceType rt,
121                AccessFlags access_flags,
122                bool is_anonymous);
123 public:
124  static InstanceKlass* allocate_instance_klass(
125                                          ClassLoaderData* loader_data,
126                                          int vtable_len,
127                                          int itable_len,
128                                          int static_field_size,
129                                          int nonstatic_oop_map_size,
130                                          ReferenceType rt,
131                                          AccessFlags access_flags,
132                                          Symbol* name,
133                                          Klass* super_klass,
134                                          bool is_anonymous,
135                                          TRAPS);
136
137  InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
138
139  // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
140  // of the class loading & initialization procedure, and the use of the states.
141  enum ClassState {
142    allocated,                          // allocated (but not yet linked)
143    loaded,                             // loaded and inserted in class hierarchy (but not linked yet)
144    linked,                             // successfully linked/verified (but not initialized yet)
145    being_initialized,                  // currently running class initializer
146    fully_initialized,                  // initialized (successfull final state)
147    initialization_error                // error happened during initialization
148  };
149
150  static int number_of_instance_classes() { return _total_instanceKlass_count; }
151
152 private:
153  static volatile int _total_instanceKlass_count;
154
155 protected:
156  // Annotations for this class
157  Annotations*    _annotations;
158  // Array classes holding elements of this class.
159  Klass*          _array_klasses;
160  // Constant pool for this class.
161  ConstantPool* _constants;
162  // The InnerClasses attribute and EnclosingMethod attribute. The
163  // _inner_classes is an array of shorts. If the class has InnerClasses
164  // attribute, then the _inner_classes array begins with 4-tuples of shorts
165  // [inner_class_info_index, outer_class_info_index,
166  // inner_name_index, inner_class_access_flags] for the InnerClasses
167  // attribute. If the EnclosingMethod attribute exists, it occupies the
168  // last two shorts [class_index, method_index] of the array. If only
169  // the InnerClasses attribute exists, the _inner_classes array length is
170  // number_of_inner_classes * 4. If the class has both InnerClasses
171  // and EnclosingMethod attributes the _inner_classes array length is
172  // number_of_inner_classes * 4 + enclosing_method_attribute_size.
173  Array<jushort>* _inner_classes;
174
175  // the source debug extension for this klass, NULL if not specified.
176  // Specified as UTF-8 string without terminating zero byte in the classfile,
177  // it is stored in the instanceklass as a NULL-terminated UTF-8 string
178  char*           _source_debug_extension;
179  // Array name derived from this class which needs unreferencing
180  // if this class is unloaded.
181  Symbol*         _array_name;
182
183  // Number of heapOopSize words used by non-static fields in this klass
184  // (including inherited fields but after header_size()).
185  int             _nonstatic_field_size;
186  int             _static_field_size;    // number words used by static fields (oop and non-oop) in this klass
187  // Constant pool index to the utf8 entry of the Generic signature,
188  // or 0 if none.
189  u2              _generic_signature_index;
190  // Constant pool index to the utf8 entry for the name of source file
191  // containing this klass, 0 if not specified.
192  u2              _source_file_name_index;
193  u2              _static_oop_field_count;// number of static oop fields in this klass
194  u2              _java_fields_count;    // The number of declared Java fields
195  int             _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
196
197  // _is_marked_dependent can be set concurrently, thus cannot be part of the
198  // _misc_flags.
199  bool            _is_marked_dependent;  // used for marking during flushing and deoptimization
200  bool            _has_unloaded_dependent;
201
202  enum {
203    _misc_rewritten                = 1 << 0, // methods rewritten.
204    _misc_has_nonstatic_fields     = 1 << 1, // for sizing with UseCompressedOops
205    _misc_should_verify_class      = 1 << 2, // allow caching of preverification
206    _misc_is_anonymous             = 1 << 3, // has embedded _host_klass field
207    _misc_is_contended             = 1 << 4, // marked with contended annotation
208    _misc_has_default_methods      = 1 << 5, // class/superclass/implemented interfaces has default methods
209    _misc_declares_default_methods = 1 << 6, // directly declares default methods (any access)
210    _misc_has_been_redefined       = 1 << 7, // class has been redefined
211    _misc_is_scratch_class         = 1 << 8  // class is the redefined scratch class
212  };
213  u2              _misc_flags;
214  u2              _minor_version;        // minor version number of class file
215  u2              _major_version;        // major version number of class file
216  Thread*         _init_thread;          // Pointer to current thread doing initialization (to handle recusive initialization)
217  int             _vtable_len;           // length of Java vtable (in words)
218  int             _itable_len;           // length of Java itable (in words)
219  OopMapCache*    volatile _oop_map_cache;   // OopMapCache for all methods in the klass (allocated lazily)
220  MemberNameTable* _member_names;        // Member names
221  JNIid*          _jni_ids;              // First JNI identifier for static fields in this class
222  jmethodID*      _methods_jmethod_ids;  // jmethodIDs corresponding to method_idnum, or NULL if none
223  nmethodBucket*  _dependencies;         // list of dependent nmethods
224  nmethod*        _osr_nmethods_head;    // Head of list of on-stack replacement nmethods for this class
225  BreakpointInfo* _breakpoints;          // bpt lists, managed by Method*
226  // Linked instanceKlasses of previous versions
227  InstanceKlass* _previous_versions;
228  // JVMTI fields can be moved to their own structure - see 6315920
229  // JVMTI: cached class file, before retransformable agent modified it in CFLH
230  JvmtiCachedClassFileData* _cached_class_file;
231
232  volatile u2     _idnum_allocated_count;         // JNI/JVMTI: increments with the addition of methods, old ids don't change
233
234  // Class states are defined as ClassState (see above).
235  // Place the _init_state here to utilize the unused 2-byte after
236  // _idnum_allocated_count.
237  u1              _init_state;                    // state of class
238  u1              _reference_type;                // reference type
239
240  JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map;  // JVMTI: used during heap iteration
241
242  NOT_PRODUCT(int _verify_count;)  // to avoid redundant verifies
243
244  // Method array.
245  Array<Method*>* _methods;
246  // Default Method Array, concrete methods inherited from interfaces
247  Array<Method*>* _default_methods;
248  // Interface (Klass*s) this class declares locally to implement.
249  Array<Klass*>* _local_interfaces;
250  // Interface (Klass*s) this class implements transitively.
251  Array<Klass*>* _transitive_interfaces;
252  // Int array containing the original order of method in the class file (for JVMTI).
253  Array<int>*     _method_ordering;
254  // Int array containing the vtable_indices for default_methods
255  // offset matches _default_methods offset
256  Array<int>*     _default_vtable_indices;
257
258  // Instance and static variable information, starts with 6-tuples of shorts
259  // [access, name index, sig index, initval index, low_offset, high_offset]
260  // for all fields, followed by the generic signature data at the end of
261  // the array. Only fields with generic signature attributes have the generic
262  // signature data set in the array. The fields array looks like following:
263  //
264  // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
265  // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
266  //      ...
267  // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
268  //     [generic signature index]
269  //     [generic signature index]
270  //     ...
271  Array<u2>*      _fields;
272
273  // embedded Java vtable follows here
274  // embedded Java itables follows here
275  // embedded static fields follows here
276  // embedded nonstatic oop-map blocks follows here
277  // embedded implementor of this interface follows here
278  //   The embedded implementor only exists if the current klass is an
279  //   iterface. The possible values of the implementor fall into following
280  //   three cases:
281  //     NULL: no implementor.
282  //     A Klass* that's not itself: one implementor.
283  //     Itself: more than one implementors.
284  // embedded host klass follows here
285  //   The embedded host klass only exists in an anonymous class for
286  //   dynamic language support (JSR 292 enabled). The host class grants
287  //   its access privileges to this class also. The host class is either
288  //   named, or a previously loaded anonymous class. A non-anonymous class
289  //   or an anonymous class loaded through normal classloading does not
290  //   have this embedded field.
291  //
292
293  friend class SystemDictionary;
294
295 public:
296  bool has_nonstatic_fields() const        {
297    return (_misc_flags & _misc_has_nonstatic_fields) != 0;
298  }
299  void set_has_nonstatic_fields(bool b)    {
300    if (b) {
301      _misc_flags |= _misc_has_nonstatic_fields;
302    } else {
303      _misc_flags &= ~_misc_has_nonstatic_fields;
304    }
305  }
306
307  // field sizes
308  int nonstatic_field_size() const         { return _nonstatic_field_size; }
309  void set_nonstatic_field_size(int size)  { _nonstatic_field_size = size; }
310
311  int static_field_size() const            { return _static_field_size; }
312  void set_static_field_size(int size)     { _static_field_size = size; }
313
314  int static_oop_field_count() const       { return (int)_static_oop_field_count; }
315  void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
316
317  // Java vtable
318  int  vtable_length() const               { return _vtable_len; }
319  void set_vtable_length(int len)          { _vtable_len = len; }
320
321  // Java itable
322  int  itable_length() const               { return _itable_len; }
323  void set_itable_length(int len)          { _itable_len = len; }
324
325  // array klasses
326  Klass* array_klasses() const             { return _array_klasses; }
327  void set_array_klasses(Klass* k)         { _array_klasses = k; }
328
329  // methods
330  Array<Method*>* methods() const          { return _methods; }
331  void set_methods(Array<Method*>* a)      { _methods = a; }
332  Method* method_with_idnum(int idnum);
333  Method* method_with_orig_idnum(int idnum);
334  Method* method_with_orig_idnum(int idnum, int version);
335
336  // method ordering
337  Array<int>* method_ordering() const     { return _method_ordering; }
338  void set_method_ordering(Array<int>* m) { _method_ordering = m; }
339  void copy_method_ordering(intArray* m, TRAPS);
340
341  // default_methods
342  Array<Method*>* default_methods() const  { return _default_methods; }
343  void set_default_methods(Array<Method*>* a) { _default_methods = a; }
344
345  // default method vtable_indices
346  Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
347  void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
348  Array<int>* create_new_default_vtable_indices(int len, TRAPS);
349
350  // interfaces
351  Array<Klass*>* local_interfaces() const          { return _local_interfaces; }
352  void set_local_interfaces(Array<Klass*>* a)      {
353    guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
354    _local_interfaces = a; }
355
356  Array<Klass*>* transitive_interfaces() const     { return _transitive_interfaces; }
357  void set_transitive_interfaces(Array<Klass*>* a) {
358    guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
359    _transitive_interfaces = a;
360  }
361
362 private:
363  friend class fieldDescriptor;
364  FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
365
366 public:
367  int     field_offset      (int index) const { return field(index)->offset(); }
368  int     field_access_flags(int index) const { return field(index)->access_flags(); }
369  Symbol* field_name        (int index) const { return field(index)->name(constants()); }
370  Symbol* field_signature   (int index) const { return field(index)->signature(constants()); }
371
372  // Number of Java declared fields
373  int java_fields_count() const           { return (int)_java_fields_count; }
374
375  Array<u2>* fields() const            { return _fields; }
376  void set_fields(Array<u2>* f, u2 java_fields_count) {
377    guarantee(_fields == NULL || f == NULL, "Just checking");
378    _fields = f;
379    _java_fields_count = java_fields_count;
380  }
381
382  // inner classes
383  Array<u2>* inner_classes() const       { return _inner_classes; }
384  void set_inner_classes(Array<u2>* f)   { _inner_classes = f; }
385
386  enum InnerClassAttributeOffset {
387    // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814
388    inner_class_inner_class_info_offset = 0,
389    inner_class_outer_class_info_offset = 1,
390    inner_class_inner_name_offset = 2,
391    inner_class_access_flags_offset = 3,
392    inner_class_next_offset = 4
393  };
394
395  enum EnclosingMethodAttributeOffset {
396    enclosing_method_class_index_offset = 0,
397    enclosing_method_method_index_offset = 1,
398    enclosing_method_attribute_size = 2
399  };
400
401  // method override check
402  bool is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS);
403
404  // package
405  bool is_same_class_package(Klass* class2);
406  bool is_same_class_package(oop classloader2, Symbol* classname2);
407  static bool is_same_class_package(oop class_loader1, Symbol* class_name1, oop class_loader2, Symbol* class_name2);
408
409  // find an enclosing class
410  Klass* compute_enclosing_class(bool* inner_is_member, TRAPS) {
411    instanceKlassHandle self(THREAD, this);
412    return compute_enclosing_class_impl(self, inner_is_member, THREAD);
413  }
414  static Klass* compute_enclosing_class_impl(instanceKlassHandle self,
415                                             bool* inner_is_member, TRAPS);
416
417  // Find InnerClasses attribute for k and return outer_class_info_index & inner_name_index.
418  static bool find_inner_classes_attr(instanceKlassHandle k,
419                                      int* ooff, int* noff, TRAPS);
420
421  // tell if two classes have the same enclosing class (at package level)
422  bool is_same_package_member(Klass* class2, TRAPS) {
423    instanceKlassHandle self(THREAD, this);
424    return is_same_package_member_impl(self, class2, THREAD);
425  }
426  static bool is_same_package_member_impl(instanceKlassHandle self,
427                                          Klass* class2, TRAPS);
428
429  // initialization state
430  bool is_loaded() const                   { return _init_state >= loaded; }
431  bool is_linked() const                   { return _init_state >= linked; }
432  bool is_initialized() const              { return _init_state == fully_initialized; }
433  bool is_not_initialized() const          { return _init_state <  being_initialized; }
434  bool is_being_initialized() const        { return _init_state == being_initialized; }
435  bool is_in_error_state() const           { return _init_state == initialization_error; }
436  bool is_reentrant_initialization(Thread *thread)  { return thread == _init_thread; }
437  ClassState  init_state()                 { return (ClassState)_init_state; }
438  bool is_rewritten() const                { return (_misc_flags & _misc_rewritten) != 0; }
439
440  // defineClass specified verification
441  bool should_verify_class() const         {
442    return (_misc_flags & _misc_should_verify_class) != 0;
443  }
444  void set_should_verify_class(bool value) {
445    if (value) {
446      _misc_flags |= _misc_should_verify_class;
447    } else {
448      _misc_flags &= ~_misc_should_verify_class;
449    }
450  }
451
452  // marking
453  bool is_marked_dependent() const         { return _is_marked_dependent; }
454  void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
455
456  bool has_unloaded_dependent() const         { return _has_unloaded_dependent; }
457  void set_has_unloaded_dependent(bool value) { _has_unloaded_dependent = value; }
458
459  // initialization (virtuals from Klass)
460  bool should_be_initialized() const;  // means that initialize should be called
461  void initialize(TRAPS);
462  void link_class(TRAPS);
463  bool link_class_or_fail(TRAPS); // returns false on failure
464  void unlink_class();
465  void rewrite_class(TRAPS);
466  void link_methods(TRAPS);
467  Method* class_initializer();
468
469  // set the class to initialized if no static initializer is present
470  void eager_initialize(Thread *thread);
471
472  // reference type
473  ReferenceType reference_type() const     { return (ReferenceType)_reference_type; }
474  void set_reference_type(ReferenceType t) {
475    assert(t == (u1)t, "overflow");
476    _reference_type = (u1)t;
477  }
478
479  static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
480
481  // find local field, returns true if found
482  bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
483  // find field in direct superinterfaces, returns the interface in which the field is defined
484  Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
485  // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
486  Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
487  // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
488  Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
489
490  // find a non-static or static field given its offset within the class.
491  bool contains_field_offset(int offset) {
492    return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
493  }
494
495  bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
496  bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
497
498  // find a local method (returns NULL if not found)
499  Method* find_method(Symbol* name, Symbol* signature) const;
500  static Method* find_method(Array<Method*>* methods, Symbol* name, Symbol* signature);
501
502  // find a local method, but skip static methods
503  Method* find_instance_method(Symbol* name, Symbol* signature);
504  static Method* find_instance_method(Array<Method*>* methods, Symbol* name, Symbol* signature);
505
506  // true if method matches signature and conforms to skipping_X conditions.
507  static bool method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static);
508
509  // find a local method index in default_methods (returns -1 if not found)
510  static int find_method_index(Array<Method*>* methods, Symbol* name, Symbol* signature,
511                               OverpassLookupMode overpass_mode, StaticLookupMode static_mode);
512
513  // lookup operation (returns NULL if not found)
514  Method* uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const;
515
516  // lookup a method in all the interfaces that this class implements
517  // (returns NULL if not found)
518  Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const;
519
520  // lookup a method in local defaults then in all interfaces
521  // (returns NULL if not found)
522  Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const;
523
524  // Find method indices by name.  If a method with the specified name is
525  // found the index to the first method is returned, and 'end' is filled in
526  // with the index of first non-name-matching method.  If no method is found
527  // -1 is returned.
528  int find_method_by_name(Symbol* name, int* end);
529  static int find_method_by_name(Array<Method*>* methods, Symbol* name, int* end);
530
531  // constant pool
532  ConstantPool* constants() const        { return _constants; }
533  void set_constants(ConstantPool* c)    { _constants = c; }
534
535  // protection domain
536  oop protection_domain() const;
537
538  // signers
539  objArrayOop signers() const;
540
541  // host class
542  Klass* host_klass() const              {
543    Klass** hk = (Klass**)adr_host_klass();
544    if (hk == NULL) {
545      return NULL;
546    } else {
547      assert(*hk != NULL, "host klass should always be set if the address is not null");
548      return *hk;
549    }
550  }
551  void set_host_klass(Klass* host)            {
552    assert(is_anonymous(), "not anonymous");
553    Klass** addr = (Klass**)adr_host_klass();
554    assert(addr != NULL, "no reversed space");
555    if (addr != NULL) {
556      *addr = host;
557    }
558  }
559  bool is_anonymous() const                {
560    return (_misc_flags & _misc_is_anonymous) != 0;
561  }
562  void set_is_anonymous(bool value)        {
563    if (value) {
564      _misc_flags |= _misc_is_anonymous;
565    } else {
566      _misc_flags &= ~_misc_is_anonymous;
567    }
568  }
569
570  // Oop that keeps the metadata for this class from being unloaded
571  // in places where the metadata is stored in other places, like nmethods
572  oop klass_holder() const {
573    return is_anonymous() ? java_mirror() : class_loader();
574  }
575
576  bool is_contended() const                {
577    return (_misc_flags & _misc_is_contended) != 0;
578  }
579  void set_is_contended(bool value)        {
580    if (value) {
581      _misc_flags |= _misc_is_contended;
582    } else {
583      _misc_flags &= ~_misc_is_contended;
584    }
585  }
586
587  // source file name
588  Symbol* source_file_name() const               {
589    return (_source_file_name_index == 0) ?
590      (Symbol*)NULL : _constants->symbol_at(_source_file_name_index);
591  }
592  u2 source_file_name_index() const              {
593    return _source_file_name_index;
594  }
595  void set_source_file_name_index(u2 sourcefile_index) {
596    _source_file_name_index = sourcefile_index;
597  }
598
599  // minor and major version numbers of class file
600  u2 minor_version() const                 { return _minor_version; }
601  void set_minor_version(u2 minor_version) { _minor_version = minor_version; }
602  u2 major_version() const                 { return _major_version; }
603  void set_major_version(u2 major_version) { _major_version = major_version; }
604
605  // source debug extension
606  char* source_debug_extension() const     { return _source_debug_extension; }
607  void set_source_debug_extension(char* array, int length);
608
609  // symbol unloading support (refcount already added)
610  Symbol* array_name()                     { return _array_name; }
611  void set_array_name(Symbol* name)        { assert(_array_name == NULL  || name == NULL, "name already created"); _array_name = name; }
612
613  // nonstatic oop-map blocks
614  static int nonstatic_oop_map_size(unsigned int oop_map_count) {
615    return oop_map_count * OopMapBlock::size_in_words();
616  }
617  unsigned int nonstatic_oop_map_count() const {
618    return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
619  }
620  int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
621  void set_nonstatic_oop_map_size(int words) {
622    _nonstatic_oop_map_size = words;
623  }
624
625  // RedefineClasses() support for previous versions:
626  void add_previous_version(instanceKlassHandle ikh, int emcp_method_count);
627
628  InstanceKlass* previous_versions() const { return _previous_versions; }
629
630  InstanceKlass* get_klass_version(int version) {
631    for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) {
632      if (ik->constants()->version() == version) {
633        return ik;
634      }
635    }
636    return NULL;
637  }
638
639  bool has_been_redefined() const {
640    return (_misc_flags & _misc_has_been_redefined) != 0;
641  }
642  void set_has_been_redefined() {
643    _misc_flags |= _misc_has_been_redefined;
644  }
645
646  bool is_scratch_class() const {
647    return (_misc_flags & _misc_is_scratch_class) != 0;
648  }
649
650  void set_is_scratch_class() {
651    _misc_flags |= _misc_is_scratch_class;
652  }
653
654  void init_previous_versions() {
655    _previous_versions = NULL;
656  }
657
658 private:
659  static int  _previous_version_count;
660 public:
661  static void purge_previous_versions(InstanceKlass* ik);
662  static bool has_previous_versions() { return _previous_version_count > 0; }
663
664  // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
665  void set_cached_class_file(JvmtiCachedClassFileData *data) {
666    _cached_class_file = data;
667  }
668  JvmtiCachedClassFileData * get_cached_class_file() { return _cached_class_file; }
669  jint get_cached_class_file_len();
670  unsigned char * get_cached_class_file_bytes();
671
672  // JVMTI: Support for caching of field indices, types, and offsets
673  void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) {
674    _jvmti_cached_class_field_map = descriptor;
675  }
676  JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const {
677    return _jvmti_cached_class_field_map;
678  }
679
680  bool has_default_methods() const {
681    return (_misc_flags & _misc_has_default_methods) != 0;
682  }
683  void set_has_default_methods(bool b) {
684    if (b) {
685      _misc_flags |= _misc_has_default_methods;
686    } else {
687      _misc_flags &= ~_misc_has_default_methods;
688    }
689  }
690
691  bool declares_default_methods() const {
692    return (_misc_flags & _misc_declares_default_methods) != 0;
693  }
694  void set_declares_default_methods(bool b) {
695    if (b) {
696      _misc_flags |= _misc_declares_default_methods;
697    } else {
698      _misc_flags &= ~_misc_declares_default_methods;
699    }
700  }
701
702  // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available
703  inline u2 next_method_idnum();
704  void set_initial_method_idnum(u2 value)             { _idnum_allocated_count = value; }
705
706  // generics support
707  Symbol* generic_signature() const                   {
708    return (_generic_signature_index == 0) ?
709      (Symbol*)NULL : _constants->symbol_at(_generic_signature_index);
710  }
711  u2 generic_signature_index() const                  {
712    return _generic_signature_index;
713  }
714  void set_generic_signature_index(u2 sig_index)      {
715    _generic_signature_index = sig_index;
716  }
717
718  u2 enclosing_method_data(int offset);
719  u2 enclosing_method_class_index() {
720    return enclosing_method_data(enclosing_method_class_index_offset);
721  }
722  u2 enclosing_method_method_index() {
723    return enclosing_method_data(enclosing_method_method_index_offset);
724  }
725  void set_enclosing_method_indices(u2 class_index,
726                                    u2 method_index);
727
728  // jmethodID support
729  static jmethodID get_jmethod_id(instanceKlassHandle ik_h,
730                     methodHandle method_h);
731  static jmethodID get_jmethod_id_fetch_or_update(instanceKlassHandle ik_h,
732                     size_t idnum, jmethodID new_id, jmethodID* new_jmeths,
733                     jmethodID* to_dealloc_id_p,
734                     jmethodID** to_dealloc_jmeths_p);
735  static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
736                size_t *length_p, jmethodID* id_p);
737  void ensure_space_for_methodids(int start_offset = 0);
738  jmethodID jmethod_id_or_null(Method* method);
739
740  // annotations support
741  Annotations* annotations() const          { return _annotations; }
742  void set_annotations(Annotations* anno)   { _annotations = anno; }
743
744  AnnotationArray* class_annotations() const {
745    return (_annotations != NULL) ? _annotations->class_annotations() : NULL;
746  }
747  Array<AnnotationArray*>* fields_annotations() const {
748    return (_annotations != NULL) ? _annotations->fields_annotations() : NULL;
749  }
750  AnnotationArray* class_type_annotations() const {
751    return (_annotations != NULL) ? _annotations->class_type_annotations() : NULL;
752  }
753  Array<AnnotationArray*>* fields_type_annotations() const {
754    return (_annotations != NULL) ? _annotations->fields_type_annotations() : NULL;
755  }
756  // allocation
757  instanceOop allocate_instance(TRAPS);
758
759  // additional member function to return a handle
760  instanceHandle allocate_instance_handle(TRAPS)      { return instanceHandle(THREAD, allocate_instance(THREAD)); }
761
762  objArrayOop allocate_objArray(int n, int length, TRAPS);
763  // Helper function
764  static instanceOop register_finalizer(instanceOop i, TRAPS);
765
766  // Check whether reflection/jni/jvm code is allowed to instantiate this class;
767  // if not, throw either an Error or an Exception.
768  virtual void check_valid_for_instantiation(bool throwError, TRAPS);
769
770  // initialization
771  void call_class_initializer(TRAPS);
772  void set_initialization_state_and_notify(ClassState state, TRAPS);
773
774  // OopMapCache support
775  OopMapCache* oop_map_cache()               { return _oop_map_cache; }
776  void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; }
777  void mask_for(methodHandle method, int bci, InterpreterOopMap* entry);
778
779  // JNI identifier support (for static fields - for jni performance)
780  JNIid* jni_ids()                               { return _jni_ids; }
781  void set_jni_ids(JNIid* ids)                   { _jni_ids = ids; }
782  JNIid* jni_id_for(int offset);
783
784  // maintenance of deoptimization dependencies
785  int mark_dependent_nmethods(DepChange& changes);
786  void add_dependent_nmethod(nmethod* nm);
787  void remove_dependent_nmethod(nmethod* nm);
788
789  // On-stack replacement support
790  nmethod* osr_nmethods_head() const         { return _osr_nmethods_head; };
791  void set_osr_nmethods_head(nmethod* h)     { _osr_nmethods_head = h; };
792  void add_osr_nmethod(nmethod* n);
793  void remove_osr_nmethod(nmethod* n);
794  int mark_osr_nmethods(const Method* m);
795  nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
796
797  // Breakpoint support (see methods on Method* for details)
798  BreakpointInfo* breakpoints() const       { return _breakpoints; };
799  void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
800
801  // support for stub routines
802  static ByteSize init_state_offset()  { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
803  TRACE_DEFINE_OFFSET;
804  static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
805
806  // subclass/subinterface checks
807  bool implements_interface(Klass* k) const;
808  bool is_same_or_direct_interface(Klass* k) const;
809
810#ifdef ASSERT
811  // check whether this class or one of its superclasses was redefined
812  bool has_redefined_this_or_super() const;
813#endif
814
815  // Access to the implementor of an interface.
816  Klass* implementor() const
817  {
818    Klass** k = adr_implementor();
819    if (k == NULL) {
820      return NULL;
821    } else {
822      return *k;
823    }
824  }
825
826  void set_implementor(Klass* k) {
827    assert(is_interface(), "not interface");
828    Klass** addr = adr_implementor();
829    assert(addr != NULL, "null addr");
830    if (addr != NULL) {
831      *addr = k;
832    }
833  }
834
835  int  nof_implementors() const       {
836    Klass* k = implementor();
837    if (k == NULL) {
838      return 0;
839    } else if (k != this) {
840      return 1;
841    } else {
842      return 2;
843    }
844  }
845
846  void add_implementor(Klass* k);  // k is a new class that implements this interface
847  void init_implementor();           // initialize
848
849  // link this class into the implementors list of every interface it implements
850  void process_interfaces(Thread *thread);
851
852  // virtual operations from Klass
853  bool is_leaf_class() const               { return _subklass == NULL; }
854  GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots);
855  bool compute_is_subtype_of(Klass* k);
856  bool can_be_primary_super_slow() const;
857  int oop_size(oop obj)  const             { return size_helper(); }
858  bool oop_is_instance_slow() const        { return true; }
859
860  // Iterators
861  void do_local_static_fields(FieldClosure* cl);
862  void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
863  void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS);
864
865  void methods_do(void f(Method* method));
866  void array_klasses_do(void f(Klass* k));
867  void array_klasses_do(void f(Klass* k, TRAPS), TRAPS);
868  bool super_types_do(SuperTypeClosure* blk);
869
870  // Casting from Klass*
871  static InstanceKlass* cast(Klass* k) {
872    assert(k == NULL || k->is_klass(), "must be");
873    assert(k == NULL || k->oop_is_instance(), "cast to InstanceKlass");
874    return (InstanceKlass*) k;
875  }
876
877  InstanceKlass* java_super() const {
878    return (super() == NULL) ? NULL : cast(super());
879  }
880
881  // Sizing (in words)
882  static int header_size()            { return align_object_offset(sizeof(InstanceKlass)/HeapWordSize); }
883
884  static int size(int vtable_length, int itable_length,
885                  int nonstatic_oop_map_size,
886                  bool is_interface, bool is_anonymous) {
887    return align_object_size(header_size() +
888           align_object_offset(vtable_length) +
889           align_object_offset(itable_length) +
890           ((is_interface || is_anonymous) ?
891             align_object_offset(nonstatic_oop_map_size) :
892             nonstatic_oop_map_size) +
893           (is_interface ? (int)sizeof(Klass*)/HeapWordSize : 0) +
894           (is_anonymous ? (int)sizeof(Klass*)/HeapWordSize : 0));
895  }
896  int size() const                    { return size(vtable_length(),
897                                               itable_length(),
898                                               nonstatic_oop_map_size(),
899                                               is_interface(),
900                                               is_anonymous());
901  }
902#if INCLUDE_SERVICES
903  virtual void collect_statistics(KlassSizeStats *sz) const;
904#endif
905
906  static int vtable_start_offset()    { return header_size(); }
907  static int vtable_length_offset()   { return offset_of(InstanceKlass, _vtable_len) / HeapWordSize; }
908
909  intptr_t* start_of_vtable() const        { return ((intptr_t*)this) + vtable_start_offset(); }
910  intptr_t* start_of_itable() const        { return start_of_vtable() + align_object_offset(vtable_length()); }
911  int  itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
912
913  intptr_t* end_of_itable() const          { return start_of_itable() + itable_length(); }
914
915  address static_field_addr(int offset);
916
917  OopMapBlock* start_of_nonstatic_oop_maps() const {
918    return (OopMapBlock*)(start_of_itable() + align_object_offset(itable_length()));
919  }
920
921  Klass** end_of_nonstatic_oop_maps() const {
922    return (Klass**)(start_of_nonstatic_oop_maps() +
923                     nonstatic_oop_map_count());
924  }
925
926  Klass** adr_implementor() const {
927    if (is_interface()) {
928      return (Klass**)end_of_nonstatic_oop_maps();
929    } else {
930      return NULL;
931    }
932  };
933
934  Klass** adr_host_klass() const {
935    if (is_anonymous()) {
936      Klass** adr_impl = adr_implementor();
937      if (adr_impl != NULL) {
938        return adr_impl + 1;
939      } else {
940        return end_of_nonstatic_oop_maps();
941      }
942    } else {
943      return NULL;
944    }
945  }
946
947  // Use this to return the size of an instance in heap words:
948  int size_helper() const {
949    return layout_helper_to_size_helper(layout_helper());
950  }
951
952  // This bit is initialized in classFileParser.cpp.
953  // It is false under any of the following conditions:
954  //  - the class is abstract (including any interface)
955  //  - the class has a finalizer (if !RegisterFinalizersAtInit)
956  //  - the class size is larger than FastAllocateSizeLimit
957  //  - the class is java/lang/Class, which cannot be allocated directly
958  bool can_be_fastpath_allocated() const {
959    return !layout_helper_needs_slow_path(layout_helper());
960  }
961
962  // Java vtable/itable
963  klassVtable* vtable() const;        // return new klassVtable wrapper
964  inline Method* method_at_vtable(int index);
965  klassItable* itable() const;        // return new klassItable wrapper
966  Method* method_at_itable(Klass* holder, int index, TRAPS);
967
968#if INCLUDE_JVMTI
969  void adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed);
970#endif // INCLUDE_JVMTI
971
972  void clean_implementors_list(BoolObjectClosure* is_alive);
973  void clean_method_data(BoolObjectClosure* is_alive);
974  void clean_dependent_nmethods();
975
976  // Explicit metaspace deallocation of fields
977  // For RedefineClasses and class file parsing errors, we need to deallocate
978  // instanceKlasses and the metadata they point to.
979  void deallocate_contents(ClassLoaderData* loader_data);
980  static void deallocate_methods(ClassLoaderData* loader_data,
981                                 Array<Method*>* methods);
982  void static deallocate_interfaces(ClassLoaderData* loader_data,
983                                    Klass* super_klass,
984                                    Array<Klass*>* local_interfaces,
985                                    Array<Klass*>* transitive_interfaces);
986
987  // The constant pool is on stack if any of the methods are executing or
988  // referenced by handles.
989  bool on_stack() const { return _constants->on_stack(); }
990
991  // callbacks for actions during class unloading
992  static void notify_unload_class(InstanceKlass* ik);
993  static void release_C_heap_structures(InstanceKlass* ik);
994
995  // Naming
996  const char* signature_name() const;
997
998  // GC specific object visitors
999  //
1000  // Mark Sweep
1001  void oop_ms_follow_contents(oop obj);
1002  int  oop_ms_adjust_pointers(oop obj);
1003#if INCLUDE_ALL_GCS
1004  // Parallel Scavenge
1005  void oop_ps_push_contents(  oop obj, PSPromotionManager* pm);
1006  // Parallel Compact
1007  void oop_pc_follow_contents(oop obj, ParCompactionManager* cm);
1008  void oop_pc_update_pointers(oop obj);
1009#endif
1010
1011  // Oop fields (and metadata) iterators
1012  //  [nv = true]  Use non-virtual calls to do_oop_nv.
1013  //  [nv = false] Use virtual calls to do_oop.
1014  //
1015  // The InstanceKlass iterators also visits the Object's klass.
1016
1017  // Forward iteration
1018 public:
1019  // Iterate over all oop fields in the oop maps.
1020  template <bool nv, class OopClosureType>
1021  inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1022
1023 protected:
1024  // Iterate over all oop fields and metadata.
1025  template <bool nv, class OopClosureType>
1026  inline int oop_oop_iterate(oop obj, OopClosureType* closure);
1027
1028 private:
1029  // Iterate over all oop fields in the oop maps.
1030  // Specialized for [T = oop] or [T = narrowOop].
1031  template <bool nv, typename T, class OopClosureType>
1032  inline void oop_oop_iterate_oop_maps_specialized(oop obj, OopClosureType* closure);
1033
1034  // Iterate over all oop fields in one oop map.
1035  template <bool nv, typename T, class OopClosureType>
1036  inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1037
1038
1039  // Reverse iteration
1040#if INCLUDE_ALL_GCS
1041 public:
1042  // Iterate over all oop fields in the oop maps.
1043  template <bool nv, class OopClosureType>
1044  inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure);
1045
1046 protected:
1047  // Iterate over all oop fields and metadata.
1048  template <bool nv, class OopClosureType>
1049  inline int oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
1050
1051 private:
1052  // Iterate over all oop fields in the oop maps.
1053  // Specialized for [T = oop] or [T = narrowOop].
1054  template <bool nv, typename T, class OopClosureType>
1055  inline void oop_oop_iterate_oop_maps_specialized_reverse(oop obj, OopClosureType* closure);
1056
1057  // Iterate over all oop fields in one oop map.
1058  template <bool nv, typename T, class OopClosureType>
1059  inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure);
1060#endif
1061
1062
1063  // Bounded range iteration
1064 public:
1065  // Iterate over all oop fields in the oop maps.
1066  template <bool nv, class OopClosureType>
1067  inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1068
1069 protected:
1070  // Iterate over all oop fields and metadata.
1071  template <bool nv, class OopClosureType>
1072  inline int oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1073
1074 private:
1075  // Iterate over all oop fields in the oop maps.
1076  // Specialized for [T = oop] or [T = narrowOop].
1077  template <bool nv, typename T, class OopClosureType>
1078  inline void oop_oop_iterate_oop_maps_specialized_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1079
1080  // Iterate over all oop fields in one oop map.
1081  template <bool nv, typename T, class OopClosureType>
1082  inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr);
1083
1084
1085 public:
1086
1087#define InstanceKlass_OOP_OOP_ITERATE_DECL(OopClosureType, nv_suffix)                   \
1088  int  oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure);                    \
1089  int  oop_oop_iterate##nv_suffix##_m(oop obj, OopClosureType* closure, MemRegion mr);
1090
1091  ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DECL)
1092  ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DECL)
1093
1094#if INCLUDE_ALL_GCS
1095#define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DECL(OopClosureType, nv_suffix)  \
1096  int  oop_oop_iterate_backwards##nv_suffix(oop obj, OopClosureType* closure);
1097
1098  ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DECL)
1099  ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DECL)
1100#endif // INCLUDE_ALL_GCS
1101
1102  u2 idnum_allocated_count() const      { return _idnum_allocated_count; }
1103
1104public:
1105  void set_in_error_state() {
1106    assert(DumpSharedSpaces, "only call this when dumping archive");
1107    _init_state = initialization_error;
1108  }
1109  bool check_sharing_error_state();
1110
1111private:
1112  // initialization state
1113#ifdef ASSERT
1114  void set_init_state(ClassState state);
1115#else
1116  void set_init_state(ClassState state) { _init_state = (u1)state; }
1117#endif
1118  void set_rewritten()                  { _misc_flags |= _misc_rewritten; }
1119  void set_init_thread(Thread *thread)  { _init_thread = thread; }
1120
1121  // The RedefineClasses() API can cause new method idnums to be needed
1122  // which will cause the caches to grow. Safety requires different
1123  // cache management logic if the caches can grow instead of just
1124  // going from NULL to non-NULL.
1125  bool idnum_can_increment() const      { return has_been_redefined(); }
1126  jmethodID* methods_jmethod_ids_acquire() const
1127         { return (jmethodID*)OrderAccess::load_ptr_acquire(&_methods_jmethod_ids); }
1128  void release_set_methods_jmethod_ids(jmethodID* jmeths)
1129         { OrderAccess::release_store_ptr(&_methods_jmethod_ids, jmeths); }
1130
1131  // Lock during initialization
1132public:
1133  // Lock for (1) initialization; (2) access to the ConstantPool of this class.
1134  // Must be one per class and it has to be a VM internal object so java code
1135  // cannot lock it (like the mirror).
1136  // It has to be an object not a Mutex because it's held through java calls.
1137  oop init_lock() const;
1138private:
1139  void fence_and_clear_init_lock();
1140
1141  // Static methods that are used to implement member methods where an exposed this pointer
1142  // is needed due to possible GCs
1143  static bool link_class_impl                           (instanceKlassHandle this_k, bool throw_verifyerror, TRAPS);
1144  static bool verify_code                               (instanceKlassHandle this_k, bool throw_verifyerror, TRAPS);
1145  static void initialize_impl                           (instanceKlassHandle this_k, TRAPS);
1146  static void initialize_super_interfaces               (instanceKlassHandle this_k, TRAPS);
1147  static void eager_initialize_impl                     (instanceKlassHandle this_k);
1148  static void set_initialization_state_and_notify_impl  (instanceKlassHandle this_k, ClassState state, TRAPS);
1149  static void call_class_initializer_impl               (instanceKlassHandle this_k, TRAPS);
1150  static Klass* array_klass_impl                        (instanceKlassHandle this_k, bool or_null, int n, TRAPS);
1151  static void do_local_static_fields_impl               (instanceKlassHandle this_k, void f(fieldDescriptor* fd, Handle, TRAPS), Handle, TRAPS);
1152  /* jni_id_for_impl for jfieldID only */
1153  static JNIid* jni_id_for_impl                         (instanceKlassHandle this_k, int offset);
1154
1155  // Returns the array class for the n'th dimension
1156  Klass* array_klass_impl(bool or_null, int n, TRAPS);
1157
1158  // Returns the array class with this class as element type
1159  Klass* array_klass_impl(bool or_null, TRAPS);
1160
1161  // find a local method (returns NULL if not found)
1162  Method* find_method_impl(Symbol* name, Symbol* signature,
1163                           OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const;
1164  static Method* find_method_impl(Array<Method*>* methods, Symbol* name, Symbol* signature,
1165                                  OverpassLookupMode overpass_mode, StaticLookupMode static_mode);
1166
1167  // Free CHeap allocated fields.
1168  void release_C_heap_structures();
1169
1170  // RedefineClasses support
1171  void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1172  void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1173public:
1174  // CDS support - remove and restore oops from metadata. Oops are not shared.
1175  virtual void remove_unshareable_info();
1176  virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
1177
1178  // jvm support
1179  jint compute_modifier_flags(TRAPS) const;
1180
1181  // JSR-292 support
1182  MemberNameTable* member_names() { return _member_names; }
1183  void set_member_names(MemberNameTable* member_names) { _member_names = member_names; }
1184  bool add_member_name(Handle member_name);
1185
1186public:
1187  // JVMTI support
1188  jint jvmti_class_status() const;
1189
1190 public:
1191  // Printing
1192#ifndef PRODUCT
1193  void print_on(outputStream* st) const;
1194#endif
1195  void print_value_on(outputStream* st) const;
1196
1197  void oop_print_value_on(oop obj, outputStream* st);
1198
1199#ifndef PRODUCT
1200  void oop_print_on      (oop obj, outputStream* st);
1201
1202  void print_dependent_nmethods(bool verbose = false);
1203  bool is_dependent_nmethod(nmethod* nm);
1204#endif
1205
1206  const char* internal_name() const;
1207
1208  // Verification
1209  void verify_on(outputStream* st);
1210
1211  void oop_verify_on(oop obj, outputStream* st);
1212};
1213
1214inline Method* InstanceKlass::method_at_vtable(int index)  {
1215#ifndef PRODUCT
1216  assert(index >= 0, "valid vtable index");
1217  if (DebugVtables) {
1218    verify_vtable_index(index);
1219  }
1220#endif
1221  vtableEntry* ve = (vtableEntry*)start_of_vtable();
1222  return ve[index].method();
1223}
1224
1225// for adding methods
1226// UNSET_IDNUM return means no more ids available
1227inline u2 InstanceKlass::next_method_idnum() {
1228  if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) {
1229    return ConstMethod::UNSET_IDNUM; // no more ids available
1230  } else {
1231    return _idnum_allocated_count++;
1232  }
1233}
1234
1235
1236/* JNIid class for jfieldIDs only */
1237class JNIid: public CHeapObj<mtClass> {
1238  friend class VMStructs;
1239 private:
1240  Klass*             _holder;
1241  JNIid*             _next;
1242  int                _offset;
1243#ifdef ASSERT
1244  bool               _is_static_field_id;
1245#endif
1246
1247 public:
1248  // Accessors
1249  Klass* holder() const           { return _holder; }
1250  int offset() const              { return _offset; }
1251  JNIid* next()                   { return _next; }
1252  // Constructor
1253  JNIid(Klass* holder, int offset, JNIid* next);
1254  // Identifier lookup
1255  JNIid* find(int offset);
1256
1257  bool find_local_field(fieldDescriptor* fd) {
1258    return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd);
1259  }
1260
1261  static void deallocate(JNIid* id);
1262  // Debugging
1263#ifdef ASSERT
1264  bool is_static_field_id() const { return _is_static_field_id; }
1265  void set_is_static_field_id()   { _is_static_field_id = true; }
1266#endif
1267  void verify(Klass* holder);
1268};
1269
1270
1271//
1272// nmethodBucket is used to record dependent nmethods for
1273// deoptimization.  nmethod dependencies are actually <klass, method>
1274// pairs but we really only care about the klass part for purposes of
1275// finding nmethods which might need to be deoptimized.  Instead of
1276// recording the method, a count of how many times a particular nmethod
1277// was recorded is kept.  This ensures that any recording errors are
1278// noticed since an nmethod should be removed as many times are it's
1279// added.
1280//
1281class nmethodBucket: public CHeapObj<mtClass> {
1282  friend class VMStructs;
1283 private:
1284  nmethod*       _nmethod;
1285  int            _count;
1286  nmethodBucket* _next;
1287
1288 public:
1289  nmethodBucket(nmethod* nmethod, nmethodBucket* next) {
1290    _nmethod = nmethod;
1291    _next = next;
1292    _count = 1;
1293  }
1294  int count()                             { return _count; }
1295  int increment()                         { _count += 1; return _count; }
1296  int decrement();
1297  nmethodBucket* next()                   { return _next; }
1298  void set_next(nmethodBucket* b)         { _next = b; }
1299  nmethod* get_nmethod()                  { return _nmethod; }
1300};
1301
1302// An iterator that's used to access the inner classes indices in the
1303// InstanceKlass::_inner_classes array.
1304class InnerClassesIterator : public StackObj {
1305 private:
1306  Array<jushort>* _inner_classes;
1307  int _length;
1308  int _idx;
1309 public:
1310
1311  InnerClassesIterator(instanceKlassHandle k) {
1312    _inner_classes = k->inner_classes();
1313    if (k->inner_classes() != NULL) {
1314      _length = _inner_classes->length();
1315      // The inner class array's length should be the multiple of
1316      // inner_class_next_offset if it only contains the InnerClasses
1317      // attribute data, or it should be
1318      // n*inner_class_next_offset+enclosing_method_attribute_size
1319      // if it also contains the EnclosingMethod data.
1320      assert((_length % InstanceKlass::inner_class_next_offset == 0 ||
1321              _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size),
1322             "just checking");
1323      // Remove the enclosing_method portion if exists.
1324      if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) {
1325        _length -= InstanceKlass::enclosing_method_attribute_size;
1326      }
1327    } else {
1328      _length = 0;
1329    }
1330    _idx = 0;
1331  }
1332
1333  int length() const {
1334    return _length;
1335  }
1336
1337  void next() {
1338    _idx += InstanceKlass::inner_class_next_offset;
1339  }
1340
1341  bool done() const {
1342    return (_idx >= _length);
1343  }
1344
1345  u2 inner_class_info_index() const {
1346    return _inner_classes->at(
1347               _idx + InstanceKlass::inner_class_inner_class_info_offset);
1348  }
1349
1350  void set_inner_class_info_index(u2 index) {
1351    _inner_classes->at_put(
1352               _idx + InstanceKlass::inner_class_inner_class_info_offset, index);
1353  }
1354
1355  u2 outer_class_info_index() const {
1356    return _inner_classes->at(
1357               _idx + InstanceKlass::inner_class_outer_class_info_offset);
1358  }
1359
1360  void set_outer_class_info_index(u2 index) {
1361    _inner_classes->at_put(
1362               _idx + InstanceKlass::inner_class_outer_class_info_offset, index);
1363  }
1364
1365  u2 inner_name_index() const {
1366    return _inner_classes->at(
1367               _idx + InstanceKlass::inner_class_inner_name_offset);
1368  }
1369
1370  void set_inner_name_index(u2 index) {
1371    _inner_classes->at_put(
1372               _idx + InstanceKlass::inner_class_inner_name_offset, index);
1373  }
1374
1375  u2 inner_access_flags() const {
1376    return _inner_classes->at(
1377               _idx + InstanceKlass::inner_class_access_flags_offset);
1378  }
1379};
1380
1381#endif // SHARE_VM_OOPS_INSTANCEKLASS_HPP
1382