< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page
rev 50604 : imported patch jep181-rev1
rev 50605 : imported patch jep181-rev2
rev 50606 : imported patch jep181-rev3


 128 
 129 static inline bool is_class_loader(const Symbol* class_name,
 130                                    const ClassFileParser& parser) {
 131   assert(class_name != NULL, "invariant");
 132 
 133   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 134     return true;
 135   }
 136 
 137   if (SystemDictionary::ClassLoader_klass_loaded()) {
 138     const Klass* const super_klass = parser.super_klass();
 139     if (super_klass != NULL) {
 140       if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
 141         return true;
 142       }
 143     }
 144   }
 145   return false;
 146 }
 147 
































































































































































































 148 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 149   const int size = InstanceKlass::size(parser.vtable_size(),
 150                                        parser.itable_size(),
 151                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 152                                        parser.is_interface(),
 153                                        parser.is_anonymous(),
 154                                        should_store_fingerprint(parser.is_anonymous()));
 155 
 156   const Symbol* const class_name = parser.class_name();
 157   assert(class_name != NULL, "invariant");
 158   ClassLoaderData* loader_data = parser.loader_data();
 159   assert(loader_data != NULL, "invariant");
 160 
 161   InstanceKlass* ik;
 162 
 163   // Allocation
 164   if (REF_NONE == parser.reference_type()) {
 165     if (class_name == vmSymbols::java_lang_Class()) {
 166       // mirror
 167       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 168     }
 169     else if (is_class_loader(class_name, parser)) {
 170       // class loader
 171       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 172     }
 173     else {
 174       // normal
 175       ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
 176     }
 177   }
 178   else {
 179     // reference
 180     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 181   }
 182 
 183   // Check for pending exception before adding to the loader data and incrementing
 184   // class count.  Can get OOM here.
 185   if (HAS_PENDING_EXCEPTION) {
 186     return NULL;
 187   }
 188 
 189   return ik;
 190 }
 191 
 192 
 193 // copy method ordering from resource area to Metaspace
 194 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 195   if (m != NULL) {
 196     // allocate a new array and copy contents (memcpy?)
 197     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 198     for (int i = 0; i < m->length(); i++) {
 199       _method_ordering->at_put(i, m->at(i));
 200     }
 201   } else {
 202     _method_ordering = Universe::the_empty_int_array();
 203   }
 204 }
 205 
 206 // create a new array of vtable_indices for default methods
 207 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 208   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 209   assert(default_vtable_indices() == NULL, "only create once");
 210   set_default_vtable_indices(vtable_indices);
 211   return vtable_indices;
 212 }
 213 
 214 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind) :
 215   _static_field_size(parser.static_field_size()),
 216   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 217   _itable_len(parser.itable_size()),
 218   _reference_type(parser.reference_type()) {



 219     set_vtable_length(parser.vtable_size());
 220     set_kind(kind);
 221     set_access_flags(parser.access_flags());
 222     set_is_anonymous(parser.is_anonymous());
 223     set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 224                                                     false));
 225 
 226     assert(NULL == _methods, "underlying memory not zeroed?");
 227     assert(is_instance_klass(), "is layout incorrect?");
 228     assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 229 }
 230 
 231 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 232                                        Array<Method*>* methods) {
 233   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 234       !methods->is_shared()) {
 235     for (int i = 0; i < methods->length(); i++) {
 236       Method* method = methods->at(i);
 237       if (method == NULL) continue;  // maybe null if error processing
 238       // Only want to delete methods that are not executing for RedefineClasses.


 342   // If a method from a redefined class is using this constant pool, don't
 343   // delete it, yet.  The new class's previous version will point to this.
 344   if (constants() != NULL) {
 345     assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
 346     if (!constants()->is_shared()) {
 347       MetadataFactory::free_metadata(loader_data, constants());
 348     }
 349     // Delete any cached resolution errors for the constant pool
 350     SystemDictionary::delete_resolution_error(constants());
 351 
 352     set_constants(NULL);
 353   }
 354 
 355   if (inner_classes() != NULL &&
 356       inner_classes() != Universe::the_empty_short_array() &&
 357       !inner_classes()->is_shared()) {
 358     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 359   }
 360   set_inner_classes(NULL);
 361 







 362   // We should deallocate the Annotations instance if it's not in shared spaces.
 363   if (annotations() != NULL && !annotations()->is_shared()) {
 364     MetadataFactory::free_metadata(loader_data, annotations());
 365   }
 366   set_annotations(NULL);
 367 }
 368 
 369 bool InstanceKlass::should_be_initialized() const {
 370   return !is_initialized();
 371 }
 372 
 373 klassItable InstanceKlass::itable() const {
 374   return klassItable(const_cast<InstanceKlass*>(this));
 375 }
 376 
 377 void InstanceKlass::eager_initialize(Thread *thread) {
 378   if (!EagerInitialization) return;
 379 
 380   if (this->is_not_initialized()) {
 381     // abort if the the class has a class initializer


 626         itable().initialize_itable(true, CHECK_false);
 627       }
 628 #ifdef ASSERT
 629       else {
 630         vtable().verify(tty, true);
 631         // In case itable verification is ever added.
 632         // itable().verify(tty, true);
 633       }
 634 #endif
 635       set_init_state(linked);
 636       if (JvmtiExport::should_post_class_prepare()) {
 637         Thread *thread = THREAD;
 638         assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 639         JvmtiExport::post_class_prepare((JavaThread *) thread, this);
 640       }
 641     }
 642   }
 643   return true;
 644 }
 645 
 646 
 647 // Rewrite the byte codes of all of the methods of a class.
 648 // The rewriter must be called exactly once. Rewriting must happen after
 649 // verification but before the first method of the class is executed.
 650 void InstanceKlass::rewrite_class(TRAPS) {
 651   assert(is_loaded(), "must be loaded");
 652   if (is_rewritten()) {
 653     assert(is_shared(), "rewriting an unshared class?");
 654     return;
 655   }
 656   Rewriter::rewrite(this, CHECK);
 657   set_rewritten();
 658 }
 659 
 660 // Now relocate and link method entry points after class is rewritten.
 661 // This is outside is_rewritten flag. In case of an exception, it can be
 662 // executed more than once.
 663 void InstanceKlass::link_methods(TRAPS) {
 664   int len = methods()->length();
 665   for (int i = len-1; i >= 0; i--) {
 666     methodHandle m(THREAD, methods()->at(i));


1342   return find_method_impl(name, signature, find_overpass, find_static, find_private);
1343 }
1344 
1345 Method* InstanceKlass::find_method_impl(const Symbol* name,
1346                                         const Symbol* signature,
1347                                         OverpassLookupMode overpass_mode,
1348                                         StaticLookupMode static_mode,
1349                                         PrivateLookupMode private_mode) const {
1350   return InstanceKlass::find_method_impl(methods(),
1351                                          name,
1352                                          signature,
1353                                          overpass_mode,
1354                                          static_mode,
1355                                          private_mode);
1356 }
1357 
1358 // find_instance_method looks up the name/signature in the local methods array
1359 // and skips over static methods
1360 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods,
1361                                             const Symbol* name,
1362                                             const Symbol* signature) {

1363   Method* const meth = InstanceKlass::find_method_impl(methods,
1364                                                  name,
1365                                                  signature,
1366                                                  find_overpass,
1367                                                  skip_static,
1368                                                  find_private);
1369   assert(((meth == NULL) || !meth->is_static()),
1370     "find_instance_method should have skipped statics");
1371   return meth;
1372 }
1373 
1374 // find_instance_method looks up the name/signature in the local methods array
1375 // and skips over static methods
1376 Method* InstanceKlass::find_instance_method(const Symbol* name, const Symbol* signature) const {
1377   return InstanceKlass::find_instance_method(methods(), name, signature);


1378 }
1379 
1380 // Find looks up the name/signature in the local methods array
1381 // and filters on the overpass, static and private flags
1382 // This returns the first one found
1383 // note that the local methods array can have up to one overpass, one static
1384 // and one instance (private or not) with the same name/signature
1385 Method* InstanceKlass::find_local_method(const Symbol* name,
1386                                          const Symbol* signature,
1387                                          OverpassLookupMode overpass_mode,
1388                                          StaticLookupMode static_mode,
1389                                          PrivateLookupMode private_mode) const {
1390   return InstanceKlass::find_method_impl(methods(),
1391                                          name,
1392                                          signature,
1393                                          overpass_mode,
1394                                          static_mode,
1395                                          private_mode);
1396 }
1397 


1514 int InstanceKlass::find_method_by_name(const Symbol* name, int* end) const {
1515   return find_method_by_name(methods(), name, end);
1516 }
1517 
1518 int InstanceKlass::find_method_by_name(const Array<Method*>* methods,
1519                                        const Symbol* name,
1520                                        int* end_ptr) {
1521   assert(end_ptr != NULL, "just checking");
1522   int start = binary_search(methods, name);
1523   int end = start + 1;
1524   if (start != -1) {
1525     while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
1526     while (end < methods->length() && (methods->at(end))->name() == name) ++end;
1527     *end_ptr = end;
1528     return start;
1529   }
1530   return -1;
1531 }
1532 
1533 // uncached_lookup_method searches both the local class methods array and all
1534 // superclasses methods arrays, skipping any overpass methods in superclasses.

1535 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
1536                                               const Symbol* signature,
1537                                               OverpassLookupMode overpass_mode) const {

1538   OverpassLookupMode overpass_local_mode = overpass_mode;
1539   const Klass* klass = this;
1540   while (klass != NULL) {
1541     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
1542                                                                         signature,
1543                                                                         overpass_local_mode,
1544                                                                         find_static,
1545                                                                         find_private);
1546     if (method != NULL) {
1547       return method;
1548     }
1549     klass = klass->super();
1550     overpass_local_mode = skip_overpass;   // Always ignore overpass methods in superclasses
1551   }
1552   return NULL;
1553 }
1554 
1555 #ifdef ASSERT
1556 // search through class hierarchy and return true if this class or
1557 // one of the superclasses was redefined
1558 bool InstanceKlass::has_redefined_this_or_super() const {
1559   const Klass* klass = this;
1560   while (klass != NULL) {
1561     if (InstanceKlass::cast(klass)->has_been_redefined()) {
1562       return true;
1563     }
1564     klass = klass->super();
1565   }


2027   it->push(&_default_vtable_indices);
2028   it->push(&_fields);
2029 
2030   if (itable_length() > 0) {
2031     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2032     int method_table_offset_in_words = ioe->offset()/wordSize;
2033     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2034                          / itableOffsetEntry::size();
2035 
2036     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2037       if (ioe->interface_klass() != NULL) {
2038         it->push(ioe->interface_klass_addr());
2039         itableMethodEntry* ime = ioe->first_method_entry(this);
2040         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2041         for (int index = 0; index < n; index ++) {
2042           it->push(ime[index].method_addr());
2043         }
2044       }
2045     }
2046   }


2047 }
2048 
2049 void InstanceKlass::remove_unshareable_info() {
2050   Klass::remove_unshareable_info();
2051 
2052   if (is_in_error_state()) {
2053     // Classes are attempted to link during dumping and may fail,
2054     // but these classes are still in the dictionary and class list in CLD.
2055     // Check in_error state first because in_error is > linked state, so
2056     // is_linked() is true.
2057     // If there's a linking error, there is nothing else to remove.
2058     return;
2059   }
2060 
2061   // Unlink the class
2062   if (is_linked()) {
2063     unlink_class();
2064   }
2065   init_implementor();
2066 


2074   // do array classes also.
2075   if (array_klasses() != NULL) {
2076     array_klasses()->remove_unshareable_info();
2077   }
2078 
2079   // These are not allocated from metaspace, but they should should all be empty
2080   // during dump time, so we don't need to worry about them in InstanceKlass::iterate().
2081   guarantee(_source_debug_extension == NULL, "must be");
2082   guarantee(_dep_context == DependencyContext::EMPTY, "must be");
2083   guarantee(_osr_nmethods_head == NULL, "must be");
2084 
2085 #if INCLUDE_JVMTI
2086   guarantee(_breakpoints == NULL, "must be");
2087   guarantee(_previous_versions == NULL, "must be");
2088 #endif
2089 
2090  _init_thread = NULL;
2091  _methods_jmethod_ids = NULL;
2092  _jni_ids = NULL;
2093  _oop_map_cache = NULL;


2094 }
2095 
2096 void InstanceKlass::remove_java_mirror() {
2097   Klass::remove_java_mirror();
2098 
2099   // do array classes also.
2100   if (array_klasses() != NULL) {
2101     array_klasses()->remove_java_mirror();
2102   }
2103 }
2104 
2105 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2106   set_package(loader_data, CHECK);
2107   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2108 
2109   Array<Method*>* methods = this->methods();
2110   int num_methods = methods->length();
2111   for (int index2 = 0; index2 < num_methods; ++index2) {
2112     methodHandle m(THREAD, methods->at(index2));
2113     m->restore_unshareable_info(CHECK);


2925   {
2926     bool have_pv = false;
2927     // previous versions are linked together through the InstanceKlass
2928     for (InstanceKlass* pv_node = previous_versions();
2929          pv_node != NULL;
2930          pv_node = pv_node->previous_versions()) {
2931       if (!have_pv)
2932         st->print(BULLET"previous version:  ");
2933       have_pv = true;
2934       pv_node->constants()->print_value_on(st);
2935     }
2936     if (have_pv) st->cr();
2937   }
2938 
2939   if (generic_signature() != NULL) {
2940     st->print(BULLET"generic signature: ");
2941     generic_signature()->print_value_on(st);
2942     st->cr();
2943   }
2944   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();

2945   st->print(BULLET"java mirror:       "); java_mirror()->print_value_on(st);       st->cr();
2946   st->print(BULLET"vtable length      %d  (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
2947   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
2948   st->print(BULLET"itable length      %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
2949   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_itable(), itable_length(), st);
2950   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
2951   FieldPrinter print_static_field(st);
2952   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
2953   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
2954   FieldPrinter print_nonstatic_field(st);
2955   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
2956   ik->do_nonstatic_fields(&print_nonstatic_field);
2957 
2958   st->print(BULLET"non-static oop maps: ");
2959   OopMapBlock* map     = start_of_nonstatic_oop_maps();
2960   OopMapBlock* end_map = map + nonstatic_oop_map_count();
2961   while (map < end_map) {
2962     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
2963     map++;
2964   }


3167   }
3168 }
3169 
3170 #if INCLUDE_SERVICES
3171 // Size Statistics
3172 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const {
3173   Klass::collect_statistics(sz);
3174 
3175   sz->_inst_size  = wordSize * size_helper();
3176   sz->_vtab_bytes = wordSize * vtable_length();
3177   sz->_itab_bytes = wordSize * itable_length();
3178   sz->_nonstatic_oopmap_bytes = wordSize * nonstatic_oop_map_size();
3179 
3180   int n = 0;
3181   n += (sz->_methods_array_bytes         = sz->count_array(methods()));
3182   n += (sz->_method_ordering_bytes       = sz->count_array(method_ordering()));
3183   n += (sz->_local_interfaces_bytes      = sz->count_array(local_interfaces()));
3184   n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces()));
3185   n += (sz->_fields_bytes                = sz->count_array(fields()));
3186   n += (sz->_inner_classes_bytes         = sz->count_array(inner_classes()));

3187   sz->_ro_bytes += n;
3188 
3189   const ConstantPool* cp = constants();
3190   if (cp) {
3191     cp->collect_statistics(sz);
3192   }
3193 
3194   const Annotations* anno = annotations();
3195   if (anno) {
3196     anno->collect_statistics(sz);
3197   }
3198 
3199   const Array<Method*>* methods_array = methods();
3200   if (methods()) {
3201     for (int i = 0; i < methods_array->length(); i++) {
3202       Method* method = methods_array->at(i);
3203       if (method) {
3204         sz->_method_count ++;
3205         method->collect_statistics(sz);
3206       }




 128 
 129 static inline bool is_class_loader(const Symbol* class_name,
 130                                    const ClassFileParser& parser) {
 131   assert(class_name != NULL, "invariant");
 132 
 133   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 134     return true;
 135   }
 136 
 137   if (SystemDictionary::ClassLoader_klass_loaded()) {
 138     const Klass* const super_klass = parser.super_klass();
 139     if (super_klass != NULL) {
 140       if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
 141         return true;
 142       }
 143     }
 144   }
 145   return false;
 146 }
 147 
 148 // called to verify that k is a member of this nest
 149 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
 150   if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
 151     if (log_is_enabled(Trace, class, nestmates)) {
 152       ResourceMark rm(THREAD);
 153       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 154                                   k->external_name(), this->external_name());
 155     }
 156     return false;
 157   }
 158 
 159   if (log_is_enabled(Trace, class, nestmates)) {
 160     ResourceMark rm(THREAD);
 161     log_trace(class, nestmates)("Checking nest membership of %s in %s",
 162                                 k->external_name(), this->external_name());
 163   }
 164 
 165   // Check names first and if they match then check actual klass. This avoids
 166   // resolving anything unnecessarily.
 167   for (int i = 0; i < _nest_members->length(); i++) {
 168     int cp_index = _nest_members->at(i);
 169     Symbol* name = _constants->klass_name_at(cp_index);
 170     if (name == k->name()) {
 171       log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
 172 
 173       // names match so check actual klass - this may trigger class loading if
 174       // it doesn't match (but that should be impossible)
 175       Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
 176       if (k2 == k) {
 177         log_trace(class, nestmates)("- class is listed as a nest member");
 178         return true;
 179       } else {
 180         // same name but different klass!
 181         log_trace(class, nestmates)(" - klass comparison failed!");
 182         // can't have different classes for the same name, so we're done
 183         return false;
 184       }
 185     }
 186   }
 187   log_trace(class, nestmates)("- class is NOT a nest member!");
 188   return false;
 189 }
 190 
 191 // Return nest-host class, resolving, validating and saving it if needed.
 192 // In cases where this is called from a thread that can not do classloading
 193 // (such as a native JIT thread) then we simply return NULL, which in turn
 194 // causes the access check to return false. Such code will retry the access
 195 // from a more suitable environment later.
 196 InstanceKlass* InstanceKlass::nest_host(Symbol* validationException, TRAPS) {
 197   InstanceKlass* nest_host_k = _nest_host;
 198   if (nest_host_k == NULL) {
 199     // need to resolve and save our nest-host class. This could be attempted
 200     // concurrently but as the result is idempotent and we don't use the class
 201     // then we do not need any synchronization beyond what is implicitly used
 202     // during class loading.
 203     if (_nest_host_index != 0) { // we have a real nest_host
 204       // Before trying to resolve check if we're in a suitable context
 205       if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
 206         if (log_is_enabled(Trace, class, nestmates)) {
 207           ResourceMark rm(THREAD);
 208           log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
 209                                       this->external_name());
 210         }
 211         return NULL;
 212       }
 213 
 214       if (log_is_enabled(Trace, class, nestmates)) {
 215         ResourceMark rm(THREAD);
 216         log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
 217                                     this->external_name(),
 218                                     _constants->klass_name_at(_nest_host_index)->as_C_string());
 219       }
 220 
 221       Klass* k = _constants->klass_at(_nest_host_index, THREAD);
 222       if (HAS_PENDING_EXCEPTION) {
 223         Handle exc_h = Handle(THREAD, PENDING_EXCEPTION);
 224         if (exc_h->is_a(SystemDictionary::NoClassDefFoundError_klass())) {
 225           // throw a new CDNFE with the original as its cause, and a clear msg
 226           ResourceMark rm(THREAD);
 227           char buf[200];
 228           CLEAR_PENDING_EXCEPTION;
 229           jio_snprintf(buf, sizeof(buf),
 230                        "Unable to load nest-host class (%s) of %s",
 231                        _constants->klass_name_at(_nest_host_index)->as_C_string(),
 232                        this->external_name());
 233           log_trace(class, nestmates)("%s - NoClassDefFoundError", buf);
 234           THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), buf, exc_h);
 235         }
 236         // All other exceptions pass through (OOME, StackOverflowError, LinkageErrors etc).
 237         return NULL;
 238       }
 239 
 240       // A valid nest-host is an instance class in the current package that lists this
 241       // class as a nest member. If any of these conditions are not met we post the
 242       // requested exception type (if any) and return NULL
 243 
 244       const char* error = NULL;
 245 
 246       // JVMS 5.4.4 indicates package check comes first
 247       if (is_same_class_package(k)) {
 248 
 249         // Now check actual membership. We can't be a member if our "host" is
 250         // not an instance class.
 251         if (k->is_instance_klass()) {
 252           nest_host_k = InstanceKlass::cast(k);
 253 
 254           bool is_member = nest_host_k->has_nest_member(this, CHECK_NULL);
 255           if (is_member) {
 256             // save resolved nest-host value
 257             _nest_host = nest_host_k;
 258 
 259             if (log_is_enabled(Trace, class, nestmates)) {
 260               ResourceMark rm(THREAD);
 261               log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 262                                           this->external_name(), k->external_name());
 263             }
 264             return nest_host_k;
 265           }
 266         }
 267         error = "current type is not listed as a nest member";
 268       } else {
 269         error = "types are in different packages";
 270       }
 271 
 272       if (log_is_enabled(Trace, class, nestmates)) {
 273         ResourceMark rm(THREAD);
 274         log_trace(class, nestmates)("Type %s is not a nest member of resolved type %s: %s",
 275                                     this->external_name(),
 276                                     k->external_name(),
 277                                     error);
 278       }
 279 
 280       if (validationException != NULL) {
 281         ResourceMark rm(THREAD);
 282         Exceptions::fthrow(THREAD_AND_LOCATION,
 283                            validationException,
 284                            "Type %s is not a nest member of %s: %s",
 285                            this->external_name(),
 286                            k->external_name(),
 287                            error
 288                            );
 289       }
 290       return NULL;
 291     } else {
 292       if (log_is_enabled(Trace, class, nestmates)) {
 293         ResourceMark rm(THREAD);
 294         log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 295                                     this->external_name());
 296       }
 297       // save resolved nest-host value
 298       return (_nest_host = this);
 299     }
 300   }
 301   return nest_host_k;
 302 }
 303 
 304 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 305 // or we are k's nest_host - all of which is covered by comparing the two
 306 // resolved_nest_hosts
 307 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 308 
 309   assert(this != k, "this should be handled by higher-level code");
 310 
 311   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 312   // the target class k. Resolution exceptions will be passed on by upper
 313   // layers. IncompatibleClassChangeErrors from membership validation failures
 314   // will also be passed through.
 315 
 316   Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
 317   InstanceKlass* cur_host = nest_host(icce, CHECK_false);
 318   if (cur_host == NULL) {
 319     return false;
 320   }
 321 
 322   Klass* k_nest_host = k->nest_host(icce, CHECK_false);
 323   if (k_nest_host == NULL) {
 324     return false;
 325   }
 326 
 327   bool access = (cur_host == k_nest_host);
 328 
 329   if (log_is_enabled(Trace, class, nestmates)) {
 330     ResourceMark rm(THREAD);
 331     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 332                                 this->external_name(),
 333                                 access ? "" : "NOT ",
 334                                 k->external_name());
 335   }
 336 
 337   return access;
 338 }
 339 
 340 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 341   const int size = InstanceKlass::size(parser.vtable_size(),
 342                                        parser.itable_size(),
 343                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 344                                        parser.is_interface(),
 345                                        parser.is_anonymous(),
 346                                        should_store_fingerprint(parser.is_anonymous()));
 347 
 348   const Symbol* const class_name = parser.class_name();
 349   assert(class_name != NULL, "invariant");
 350   ClassLoaderData* loader_data = parser.loader_data();
 351   assert(loader_data != NULL, "invariant");
 352 
 353   InstanceKlass* ik;
 354 
 355   // Allocation
 356   if (REF_NONE == parser.reference_type()) {
 357     if (class_name == vmSymbols::java_lang_Class()) {
 358       // mirror
 359       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 360     }
 361     else if (is_class_loader(class_name, parser)) {
 362       // class loader
 363       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 364     } else {

 365       // normal
 366       ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
 367     }
 368   } else {

 369     // reference
 370     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 371   }
 372 
 373   // Check for pending exception before adding to the loader data and incrementing
 374   // class count.  Can get OOM here.
 375   if (HAS_PENDING_EXCEPTION) {
 376     return NULL;
 377   }
 378 
 379   return ik;
 380 }
 381 
 382 
 383 // copy method ordering from resource area to Metaspace
 384 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 385   if (m != NULL) {
 386     // allocate a new array and copy contents (memcpy?)
 387     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 388     for (int i = 0; i < m->length(); i++) {
 389       _method_ordering->at_put(i, m->at(i));
 390     }
 391   } else {
 392     _method_ordering = Universe::the_empty_int_array();
 393   }
 394 }
 395 
 396 // create a new array of vtable_indices for default methods
 397 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 398   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 399   assert(default_vtable_indices() == NULL, "only create once");
 400   set_default_vtable_indices(vtable_indices);
 401   return vtable_indices;
 402 }
 403 
 404 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind) :
 405   _static_field_size(parser.static_field_size()),
 406   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 407   _itable_len(parser.itable_size()),
 408   _reference_type(parser.reference_type()),
 409   _nest_members(NULL),
 410   _nest_host_index(0),
 411   _nest_host(NULL) {
 412     set_vtable_length(parser.vtable_size());
 413     set_kind(kind);
 414     set_access_flags(parser.access_flags());
 415     set_is_anonymous(parser.is_anonymous());
 416     set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 417                                                     false));
 418 
 419     assert(NULL == _methods, "underlying memory not zeroed?");
 420     assert(is_instance_klass(), "is layout incorrect?");
 421     assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 422 }
 423 
 424 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 425                                        Array<Method*>* methods) {
 426   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 427       !methods->is_shared()) {
 428     for (int i = 0; i < methods->length(); i++) {
 429       Method* method = methods->at(i);
 430       if (method == NULL) continue;  // maybe null if error processing
 431       // Only want to delete methods that are not executing for RedefineClasses.


 535   // If a method from a redefined class is using this constant pool, don't
 536   // delete it, yet.  The new class's previous version will point to this.
 537   if (constants() != NULL) {
 538     assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
 539     if (!constants()->is_shared()) {
 540       MetadataFactory::free_metadata(loader_data, constants());
 541     }
 542     // Delete any cached resolution errors for the constant pool
 543     SystemDictionary::delete_resolution_error(constants());
 544 
 545     set_constants(NULL);
 546   }
 547 
 548   if (inner_classes() != NULL &&
 549       inner_classes() != Universe::the_empty_short_array() &&
 550       !inner_classes()->is_shared()) {
 551     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 552   }
 553   set_inner_classes(NULL);
 554 
 555   if (nest_members() != NULL &&
 556       nest_members() != Universe::the_empty_short_array() &&
 557       !nest_members()->is_shared()) {
 558     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 559   }
 560   set_nest_members(NULL);
 561 
 562   // We should deallocate the Annotations instance if it's not in shared spaces.
 563   if (annotations() != NULL && !annotations()->is_shared()) {
 564     MetadataFactory::free_metadata(loader_data, annotations());
 565   }
 566   set_annotations(NULL);
 567 }
 568 
 569 bool InstanceKlass::should_be_initialized() const {
 570   return !is_initialized();
 571 }
 572 
 573 klassItable InstanceKlass::itable() const {
 574   return klassItable(const_cast<InstanceKlass*>(this));
 575 }
 576 
 577 void InstanceKlass::eager_initialize(Thread *thread) {
 578   if (!EagerInitialization) return;
 579 
 580   if (this->is_not_initialized()) {
 581     // abort if the the class has a class initializer


 826         itable().initialize_itable(true, CHECK_false);
 827       }
 828 #ifdef ASSERT
 829       else {
 830         vtable().verify(tty, true);
 831         // In case itable verification is ever added.
 832         // itable().verify(tty, true);
 833       }
 834 #endif
 835       set_init_state(linked);
 836       if (JvmtiExport::should_post_class_prepare()) {
 837         Thread *thread = THREAD;
 838         assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 839         JvmtiExport::post_class_prepare((JavaThread *) thread, this);
 840       }
 841     }
 842   }
 843   return true;
 844 }
 845 

 846 // Rewrite the byte codes of all of the methods of a class.
 847 // The rewriter must be called exactly once. Rewriting must happen after
 848 // verification but before the first method of the class is executed.
 849 void InstanceKlass::rewrite_class(TRAPS) {
 850   assert(is_loaded(), "must be loaded");
 851   if (is_rewritten()) {
 852     assert(is_shared(), "rewriting an unshared class?");
 853     return;
 854   }
 855   Rewriter::rewrite(this, CHECK);
 856   set_rewritten();
 857 }
 858 
 859 // Now relocate and link method entry points after class is rewritten.
 860 // This is outside is_rewritten flag. In case of an exception, it can be
 861 // executed more than once.
 862 void InstanceKlass::link_methods(TRAPS) {
 863   int len = methods()->length();
 864   for (int i = len-1; i >= 0; i--) {
 865     methodHandle m(THREAD, methods()->at(i));


1541   return find_method_impl(name, signature, find_overpass, find_static, find_private);
1542 }
1543 
1544 Method* InstanceKlass::find_method_impl(const Symbol* name,
1545                                         const Symbol* signature,
1546                                         OverpassLookupMode overpass_mode,
1547                                         StaticLookupMode static_mode,
1548                                         PrivateLookupMode private_mode) const {
1549   return InstanceKlass::find_method_impl(methods(),
1550                                          name,
1551                                          signature,
1552                                          overpass_mode,
1553                                          static_mode,
1554                                          private_mode);
1555 }
1556 
1557 // find_instance_method looks up the name/signature in the local methods array
1558 // and skips over static methods
1559 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods,
1560                                             const Symbol* name,
1561                                             const Symbol* signature,
1562                                             PrivateLookupMode private_mode) {
1563   Method* const meth = InstanceKlass::find_method_impl(methods,
1564                                                  name,
1565                                                  signature,
1566                                                  find_overpass,
1567                                                  skip_static,
1568                                                  private_mode);
1569   assert(((meth == NULL) || !meth->is_static()),
1570     "find_instance_method should have skipped statics");
1571   return meth;
1572 }
1573 
1574 // find_instance_method looks up the name/signature in the local methods array
1575 // and skips over static methods
1576 Method* InstanceKlass::find_instance_method(const Symbol* name,
1577                                             const Symbol* signature,
1578                                             PrivateLookupMode private_mode) const {
1579   return InstanceKlass::find_instance_method(methods(), name, signature, private_mode);
1580 }
1581 
1582 // Find looks up the name/signature in the local methods array
1583 // and filters on the overpass, static and private flags
1584 // This returns the first one found
1585 // note that the local methods array can have up to one overpass, one static
1586 // and one instance (private or not) with the same name/signature
1587 Method* InstanceKlass::find_local_method(const Symbol* name,
1588                                          const Symbol* signature,
1589                                          OverpassLookupMode overpass_mode,
1590                                          StaticLookupMode static_mode,
1591                                          PrivateLookupMode private_mode) const {
1592   return InstanceKlass::find_method_impl(methods(),
1593                                          name,
1594                                          signature,
1595                                          overpass_mode,
1596                                          static_mode,
1597                                          private_mode);
1598 }
1599 


1716 int InstanceKlass::find_method_by_name(const Symbol* name, int* end) const {
1717   return find_method_by_name(methods(), name, end);
1718 }
1719 
1720 int InstanceKlass::find_method_by_name(const Array<Method*>* methods,
1721                                        const Symbol* name,
1722                                        int* end_ptr) {
1723   assert(end_ptr != NULL, "just checking");
1724   int start = binary_search(methods, name);
1725   int end = start + 1;
1726   if (start != -1) {
1727     while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
1728     while (end < methods->length() && (methods->at(end))->name() == name) ++end;
1729     *end_ptr = end;
1730     return start;
1731   }
1732   return -1;
1733 }
1734 
1735 // uncached_lookup_method searches both the local class methods array and all
1736 // superclasses methods arrays, skipping any overpass methods in superclasses,
1737 // and possibly skipping private methods.
1738 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
1739                                               const Symbol* signature,
1740                                               OverpassLookupMode overpass_mode,
1741                                               PrivateLookupMode private_mode) const {
1742   OverpassLookupMode overpass_local_mode = overpass_mode;
1743   const Klass* klass = this;
1744   while (klass != NULL) {
1745     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
1746                                                                         signature,
1747                                                                         overpass_local_mode,
1748                                                                         find_static,
1749                                                                         private_mode);
1750     if (method != NULL) {
1751       return method;
1752     }
1753     klass = klass->super();
1754     overpass_local_mode = skip_overpass;   // Always ignore overpass methods in superclasses
1755   }
1756   return NULL;
1757 }
1758 
1759 #ifdef ASSERT
1760 // search through class hierarchy and return true if this class or
1761 // one of the superclasses was redefined
1762 bool InstanceKlass::has_redefined_this_or_super() const {
1763   const Klass* klass = this;
1764   while (klass != NULL) {
1765     if (InstanceKlass::cast(klass)->has_been_redefined()) {
1766       return true;
1767     }
1768     klass = klass->super();
1769   }


2231   it->push(&_default_vtable_indices);
2232   it->push(&_fields);
2233 
2234   if (itable_length() > 0) {
2235     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2236     int method_table_offset_in_words = ioe->offset()/wordSize;
2237     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2238                          / itableOffsetEntry::size();
2239 
2240     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2241       if (ioe->interface_klass() != NULL) {
2242         it->push(ioe->interface_klass_addr());
2243         itableMethodEntry* ime = ioe->first_method_entry(this);
2244         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2245         for (int index = 0; index < n; index ++) {
2246           it->push(ime[index].method_addr());
2247         }
2248       }
2249     }
2250   }
2251 
2252   it->push(&_nest_members);
2253 }
2254 
2255 void InstanceKlass::remove_unshareable_info() {
2256   Klass::remove_unshareable_info();
2257 
2258   if (is_in_error_state()) {
2259     // Classes are attempted to link during dumping and may fail,
2260     // but these classes are still in the dictionary and class list in CLD.
2261     // Check in_error state first because in_error is > linked state, so
2262     // is_linked() is true.
2263     // If there's a linking error, there is nothing else to remove.
2264     return;
2265   }
2266 
2267   // Unlink the class
2268   if (is_linked()) {
2269     unlink_class();
2270   }
2271   init_implementor();
2272 


2280   // do array classes also.
2281   if (array_klasses() != NULL) {
2282     array_klasses()->remove_unshareable_info();
2283   }
2284 
2285   // These are not allocated from metaspace, but they should should all be empty
2286   // during dump time, so we don't need to worry about them in InstanceKlass::iterate().
2287   guarantee(_source_debug_extension == NULL, "must be");
2288   guarantee(_dep_context == DependencyContext::EMPTY, "must be");
2289   guarantee(_osr_nmethods_head == NULL, "must be");
2290 
2291 #if INCLUDE_JVMTI
2292   guarantee(_breakpoints == NULL, "must be");
2293   guarantee(_previous_versions == NULL, "must be");
2294 #endif
2295 
2296   _init_thread = NULL;
2297   _methods_jmethod_ids = NULL;
2298   _jni_ids = NULL;
2299   _oop_map_cache = NULL;
2300   // clear _nest_host to ensure re-load at runtime
2301   _nest_host = NULL;
2302 }
2303 
2304 void InstanceKlass::remove_java_mirror() {
2305   Klass::remove_java_mirror();
2306 
2307   // do array classes also.
2308   if (array_klasses() != NULL) {
2309     array_klasses()->remove_java_mirror();
2310   }
2311 }
2312 
2313 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2314   set_package(loader_data, CHECK);
2315   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2316 
2317   Array<Method*>* methods = this->methods();
2318   int num_methods = methods->length();
2319   for (int index2 = 0; index2 < num_methods; ++index2) {
2320     methodHandle m(THREAD, methods->at(index2));
2321     m->restore_unshareable_info(CHECK);


3133   {
3134     bool have_pv = false;
3135     // previous versions are linked together through the InstanceKlass
3136     for (InstanceKlass* pv_node = previous_versions();
3137          pv_node != NULL;
3138          pv_node = pv_node->previous_versions()) {
3139       if (!have_pv)
3140         st->print(BULLET"previous version:  ");
3141       have_pv = true;
3142       pv_node->constants()->print_value_on(st);
3143     }
3144     if (have_pv) st->cr();
3145   }
3146 
3147   if (generic_signature() != NULL) {
3148     st->print(BULLET"generic signature: ");
3149     generic_signature()->print_value_on(st);
3150     st->cr();
3151   }
3152   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3153   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3154   st->print(BULLET"java mirror:       "); java_mirror()->print_value_on(st);       st->cr();
3155   st->print(BULLET"vtable length      %d  (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3156   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3157   st->print(BULLET"itable length      %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3158   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_itable(), itable_length(), st);
3159   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3160   FieldPrinter print_static_field(st);
3161   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3162   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3163   FieldPrinter print_nonstatic_field(st);
3164   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3165   ik->do_nonstatic_fields(&print_nonstatic_field);
3166 
3167   st->print(BULLET"non-static oop maps: ");
3168   OopMapBlock* map     = start_of_nonstatic_oop_maps();
3169   OopMapBlock* end_map = map + nonstatic_oop_map_count();
3170   while (map < end_map) {
3171     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3172     map++;
3173   }


3376   }
3377 }
3378 
3379 #if INCLUDE_SERVICES
3380 // Size Statistics
3381 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const {
3382   Klass::collect_statistics(sz);
3383 
3384   sz->_inst_size  = wordSize * size_helper();
3385   sz->_vtab_bytes = wordSize * vtable_length();
3386   sz->_itab_bytes = wordSize * itable_length();
3387   sz->_nonstatic_oopmap_bytes = wordSize * nonstatic_oop_map_size();
3388 
3389   int n = 0;
3390   n += (sz->_methods_array_bytes         = sz->count_array(methods()));
3391   n += (sz->_method_ordering_bytes       = sz->count_array(method_ordering()));
3392   n += (sz->_local_interfaces_bytes      = sz->count_array(local_interfaces()));
3393   n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces()));
3394   n += (sz->_fields_bytes                = sz->count_array(fields()));
3395   n += (sz->_inner_classes_bytes         = sz->count_array(inner_classes()));
3396   n += (sz->_nest_members_bytes          = sz->count_array(nest_members()));
3397   sz->_ro_bytes += n;
3398 
3399   const ConstantPool* cp = constants();
3400   if (cp) {
3401     cp->collect_statistics(sz);
3402   }
3403 
3404   const Annotations* anno = annotations();
3405   if (anno) {
3406     anno->collect_statistics(sz);
3407   }
3408 
3409   const Array<Method*>* methods_array = methods();
3410   if (methods()) {
3411     for (int i = 0; i < methods_array->length(); i++) {
3412       Method* method = methods_array->at(i);
3413       if (method) {
3414         sz->_method_count ++;
3415         method->collect_statistics(sz);
3416       }


< prev index next >