annotate hotspot/src/share/vm/oops/instanceKlass.cpp @ 34666:1c7168ea0034

8140485: Class load and creation cleanup Reviewed-by: hseigel, coleenp, sspitsyn
author mgronlun
date Tue, 08 Dec 2015 20:04:03 +0100
parents d1e1bfdca413
children be6025ebffea
rev   line source
duke@1 1 /*
lfoltan@28731 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
mgronlun@34666 26 #include "classfile/classFileParser.hpp"
stefank@7397 27 #include "classfile/javaClasses.hpp"
stefank@7397 28 #include "classfile/systemDictionary.hpp"
stefank@7397 29 #include "classfile/verifier.hpp"
stefank@7397 30 #include "classfile/vmSymbols.hpp"
vlivanov@34195 31 #include "code/dependencyContext.hpp"
stefank@7397 32 #include "compiler/compileBroker.hpp"
pliden@30764 33 #include "gc/shared/collectedHeap.inline.hpp"
pliden@30764 34 #include "gc/shared/specialized_oop_closures.hpp"
stefank@7397 35 #include "interpreter/oopMapCache.hpp"
stefank@7397 36 #include "interpreter/rewriter.hpp"
stefank@7397 37 #include "jvmtifiles/jvmti.h"
acorn@15437 38 #include "memory/heapInspection.hpp"
stefank@25356 39 #include "memory/iterator.inline.hpp"
coleenp@13728 40 #include "memory/metadataFactory.hpp"
stefank@7397 41 #include "memory/oopFactory.hpp"
never@10546 42 #include "oops/fieldStreams.hpp"
coleenp@13728 43 #include "oops/instanceClassLoaderKlass.hpp"
stefank@30150 44 #include "oops/instanceKlass.inline.hpp"
never@8725 45 #include "oops/instanceMirrorKlass.hpp"
stefank@7397 46 #include "oops/instanceOop.hpp"
coleenp@13728 47 #include "oops/klass.inline.hpp"
coleenp@13728 48 #include "oops/method.hpp"
stefank@7397 49 #include "oops/oop.inline.hpp"
coleenp@8076 50 #include "oops/symbol.hpp"
stefank@7397 51 #include "prims/jvmtiExport.hpp"
pliden@30764 52 #include "prims/jvmtiRedefineClasses.hpp"
stefank@7397 53 #include "prims/jvmtiRedefineClassesTrace.hpp"
jbachorik@22897 54 #include "prims/jvmtiThreadState.hpp"
coleenp@15099 55 #include "prims/methodComparator.hpp"
goetz@25351 56 #include "runtime/atomic.inline.hpp"
stefank@7397 57 #include "runtime/fieldDescriptor.hpp"
stefank@7397 58 #include "runtime/handles.inline.hpp"
stefank@7397 59 #include "runtime/javaCalls.hpp"
stefank@7397 60 #include "runtime/mutexLocker.hpp"
goetz@24351 61 #include "runtime/orderAccess.inline.hpp"
stefank@14583 62 #include "runtime/thread.inline.hpp"
coleenp@17075 63 #include "services/classLoadingService.hpp"
stefank@7397 64 #include "services/threadService.hpp"
stefank@7397 65 #include "utilities/dtrace.hpp"
jprovino@15482 66 #include "utilities/macros.hpp"
mockner@34628 67 #include "logging/log.hpp"
stefank@7397 68 #ifdef COMPILER1
stefank@7397 69 #include "c1/c1_Compiler.hpp"
stefank@7397 70 #endif
duke@1 71
fparain@5089 72 #ifdef DTRACE_ENABLED
fparain@5089 73
dcubed@10739 74
dcubed@10739 75 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
dcubed@10739 76 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
dcubed@10739 77 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
dcubed@10739 78 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
dcubed@10739 79 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
dcubed@10739 80 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
dcubed@10739 81 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
dcubed@10739 82 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
dcubed@10739 83 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
dcubed@10739 84 { \
dcubed@10739 85 char* data = NULL; \
dcubed@10739 86 int len = 0; \
dcubed@10739 87 Symbol* name = (clss)->name(); \
dcubed@10739 88 if (name != NULL) { \
dcubed@10739 89 data = (char*)name->bytes(); \
dcubed@10739 90 len = name->utf8_length(); \
dcubed@10739 91 } \
dcubed@10739 92 HOTSPOT_CLASS_INITIALIZATION_##type( \
dcubed@10739 93 data, len, (clss)->class_loader(), thread_type); \
dcubed@10739 94 }
dcubed@10739 95
dcubed@10739 96 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
dcubed@10739 97 { \
dcubed@10739 98 char* data = NULL; \
dcubed@10739 99 int len = 0; \
dcubed@10739 100 Symbol* name = (clss)->name(); \
dcubed@10739 101 if (name != NULL) { \
dcubed@10739 102 data = (char*)name->bytes(); \
dcubed@10739 103 len = name->utf8_length(); \
dcubed@10739 104 } \
dcubed@10739 105 HOTSPOT_CLASS_INITIALIZATION_##type( \
dcubed@10739 106 data, len, (clss)->class_loader(), thread_type, wait); \
dcubed@10739 107 }
fparain@5089 108
fparain@5089 109 #else // ndef DTRACE_ENABLED
fparain@5089 110
fparain@5089 111 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
fparain@5089 112 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
fparain@5089 113
fparain@5089 114 #endif // ndef DTRACE_ENABLED
fparain@5089 115
zgu@15104 116 volatile int InstanceKlass::_total_instanceKlass_count = 0;
zgu@15104 117
mgronlun@34666 118 static inline bool is_class_loader(const Symbol* class_name,
mgronlun@34666 119 const ClassFileParser& parser) {
mgronlun@34666 120 assert(class_name != NULL, "invariant");
mgronlun@34666 121
mgronlun@34666 122 if (class_name == vmSymbols::java_lang_ClassLoader()) {
mgronlun@34666 123 return true;
mgronlun@34666 124 }
mgronlun@34666 125
mgronlun@34666 126 if (SystemDictionary::ClassLoader_klass_loaded()) {
mgronlun@34666 127 const Klass* const super_klass = parser.super_klass();
mgronlun@34666 128 if (super_klass != NULL) {
mgronlun@34666 129 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
mgronlun@34666 130 return true;
mgronlun@34666 131 }
mgronlun@34666 132 }
mgronlun@34666 133 }
mgronlun@34666 134 return false;
mgronlun@34666 135 }
mgronlun@34666 136
mgronlun@34666 137 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
mgronlun@34666 138 const int size = InstanceKlass::size(parser.vtable_size(),
mgronlun@34666 139 parser.itable_size(),
mgronlun@34666 140 nonstatic_oop_map_size(parser.total_oop_map_count()),
mgronlun@34666 141 parser.is_interface(),
mgronlun@34666 142 parser.is_anonymous());
mgronlun@34666 143
mgronlun@34666 144 const Symbol* const class_name = parser.class_name();
mgronlun@34666 145 assert(class_name != NULL, "invariant");
mgronlun@34666 146 ClassLoaderData* loader_data = parser.loader_data();
mgronlun@34666 147 assert(loader_data != NULL, "invariant");
mgronlun@34666 148
mgronlun@34666 149 InstanceKlass* ik;
coleenp@13728 150
coleenp@13728 151 // Allocation
mgronlun@34666 152 if (REF_NONE == parser.reference_type()) {
mgronlun@34666 153 if (class_name == vmSymbols::java_lang_Class()) {
mgronlun@34666 154 // mirror
mgronlun@34666 155 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
mgronlun@34666 156 }
mgronlun@34666 157 else if (is_class_loader(class_name, parser)) {
mgronlun@34666 158 // class loader
mgronlun@34666 159 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
coleenp@13728 160 }
mgronlun@34666 161 else {
mgronlun@34666 162 // normal
mgronlun@34666 163 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
mgronlun@34666 164 }
mgronlun@34666 165 }
mgronlun@34666 166 else {
mgronlun@34666 167 // reference
mgronlun@34666 168 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
coleenp@13728 169 }
coleenp@13728 170
coleenp@15935 171 // Check for pending exception before adding to the loader data and incrementing
coleenp@15935 172 // class count. Can get OOM here.
coleenp@15935 173 if (HAS_PENDING_EXCEPTION) {
coleenp@15935 174 return NULL;
coleenp@15935 175 }
coleenp@15935 176
mgronlun@34666 177 assert(ik != NULL, "invariant");
mgronlun@34666 178
mgronlun@34666 179 const bool publicize = !parser.is_internal();
mgronlun@34666 180
coleenp@15935 181 // Add all classes to our internal class loader list here,
coleenp@15935 182 // including classes in the bootstrap (NULL) class loader.
mgronlun@34666 183 loader_data->add_class(ik, publicize);
zgu@15104 184 Atomic::inc(&_total_instanceKlass_count);
mgronlun@34666 185
coleenp@13728 186 return ik;
coleenp@13728 187 }
coleenp@13728 188
coleenp@15935 189
coleenp@15935 190 // copy method ordering from resource area to Metaspace
mgronlun@34666 191 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
coleenp@15935 192 if (m != NULL) {
coleenp@15935 193 // allocate a new array and copy contents (memcpy?)
coleenp@15935 194 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
coleenp@15935 195 for (int i = 0; i < m->length(); i++) {
coleenp@15935 196 _method_ordering->at_put(i, m->at(i));
coleenp@15935 197 }
coleenp@15935 198 } else {
coleenp@15935 199 _method_ordering = Universe::the_empty_int_array();
coleenp@15935 200 }
coleenp@15935 201 }
coleenp@15935 202
acorn@20391 203 // create a new array of vtable_indices for default methods
acorn@20391 204 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
acorn@20391 205 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
acorn@20391 206 assert(default_vtable_indices() == NULL, "only create once");
acorn@20391 207 set_default_vtable_indices(vtable_indices);
acorn@20391 208 return vtable_indices;
acorn@20391 209 }
coleenp@15935 210
mgronlun@34666 211 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind) :
mgronlun@34666 212 _static_field_size(parser.static_field_size()),
mgronlun@34666 213 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
mgronlun@34666 214 _vtable_len(parser.vtable_size()),
mgronlun@34666 215 _itable_len(parser.itable_size()),
mgronlun@34666 216 _reference_type(parser.reference_type()) {
mgronlun@34666 217 set_kind(kind);
mgronlun@34666 218 set_access_flags(parser.access_flags());
mgronlun@34666 219 set_is_anonymous(parser.is_anonymous());
mgronlun@34666 220 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
mgronlun@34666 221 false));
mgronlun@34666 222
mgronlun@34666 223 assert(NULL == _methods, "underlying memory not zeroed?");
mgronlun@34666 224 assert(is_instance_klass(), "is layout incorrect?");
mgronlun@34666 225 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
coleenp@13728 226 }
coleenp@13728 227
coleenp@15935 228 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
coleenp@15935 229 Array<Method*>* methods) {
hseigel@20684 230 if (methods != NULL && methods != Universe::the_empty_method_array() &&
hseigel@20684 231 !methods->is_shared()) {
coleenp@15935 232 for (int i = 0; i < methods->length(); i++) {
coleenp@15935 233 Method* method = methods->at(i);
coleenp@15935 234 if (method == NULL) continue; // maybe null if error processing
coleenp@15935 235 // Only want to delete methods that are not executing for RedefineClasses.
coleenp@15935 236 // The previous version will point to them so they're not totally dangling
coleenp@15935 237 assert (!method->on_stack(), "shouldn't be called with methods on stack");
coleenp@15935 238 MetadataFactory::free_metadata(loader_data, method);
coleenp@15935 239 }
coleenp@15935 240 MetadataFactory::free_array<Method*>(loader_data, methods);
coleenp@15935 241 }
coleenp@15935 242 }
coleenp@15935 243
coleenp@15935 244 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
mgronlun@34666 245 const Klass* super_klass,
coleenp@15935 246 Array<Klass*>* local_interfaces,
coleenp@15935 247 Array<Klass*>* transitive_interfaces) {
coleenp@15935 248 // Only deallocate transitive interfaces if not empty, same as super class
coleenp@15935 249 // or same as local interfaces. See code in parseClassFile.
coleenp@15935 250 Array<Klass*>* ti = transitive_interfaces;
coleenp@15935 251 if (ti != Universe::the_empty_klass_array() && ti != local_interfaces) {
coleenp@15935 252 // check that the interfaces don't come from super class
coleenp@15935 253 Array<Klass*>* sti = (super_klass == NULL) ? NULL :
coleenp@15935 254 InstanceKlass::cast(super_klass)->transitive_interfaces();
hseigel@20684 255 if (ti != sti && ti != NULL && !ti->is_shared()) {
coleenp@15935 256 MetadataFactory::free_array<Klass*>(loader_data, ti);
coleenp@15935 257 }
coleenp@15935 258 }
coleenp@15935 259
coleenp@15935 260 // local interfaces can be empty
hseigel@20684 261 if (local_interfaces != Universe::the_empty_klass_array() &&
hseigel@20684 262 local_interfaces != NULL && !local_interfaces->is_shared()) {
coleenp@15935 263 MetadataFactory::free_array<Klass*>(loader_data, local_interfaces);
coleenp@15935 264 }
coleenp@15935 265 }
coleenp@15935 266
coleenp@13728 267 // This function deallocates the metadata and C heap pointers that the
coleenp@13728 268 // InstanceKlass points to.
coleenp@13728 269 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
coleenp@13728 270
coleenp@13728 271 // Orphan the mirror first, CMS thinks it's still live.
coleenp@15935 272 if (java_mirror() != NULL) {
coleenp@15935 273 java_lang_Class::set_klass(java_mirror(), NULL);
coleenp@15935 274 }
coleenp@13728 275
coleenp@13728 276 // Need to take this class off the class loader data list.
coleenp@13728 277 loader_data->remove_class(this);
coleenp@13728 278
coleenp@13728 279 // The array_klass for this class is created later, after error handling.
coleenp@13728 280 // For class redefinition, we keep the original class so this scratch class
coleenp@13728 281 // doesn't have an array class. Either way, assert that there is nothing
coleenp@13728 282 // to deallocate.
coleenp@13728 283 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet");
coleenp@13728 284
coleenp@13728 285 // Release C heap allocated data that this might point to, which includes
coleenp@13728 286 // reference counting symbol names.
coleenp@13728 287 release_C_heap_structures();
coleenp@13728 288
coleenp@15935 289 deallocate_methods(loader_data, methods());
coleenp@13728 290 set_methods(NULL);
coleenp@13728 291
hseigel@20684 292 if (method_ordering() != NULL &&
hseigel@20684 293 method_ordering() != Universe::the_empty_int_array() &&
hseigel@20684 294 !method_ordering()->is_shared()) {
coleenp@13728 295 MetadataFactory::free_array<int>(loader_data, method_ordering());
coleenp@13728 296 }
coleenp@13728 297 set_method_ordering(NULL);
coleenp@13728 298
acorn@20391 299 // default methods can be empty
acorn@20391 300 if (default_methods() != NULL &&
hseigel@20684 301 default_methods() != Universe::the_empty_method_array() &&
hseigel@20684 302 !default_methods()->is_shared()) {
acorn@20391 303 MetadataFactory::free_array<Method*>(loader_data, default_methods());
acorn@20391 304 }
acorn@20391 305 // Do NOT deallocate the default methods, they are owned by superinterfaces.
acorn@20391 306 set_default_methods(NULL);
acorn@20391 307
acorn@20391 308 // default methods vtable indices can be empty
hseigel@20684 309 if (default_vtable_indices() != NULL &&
hseigel@20684 310 !default_vtable_indices()->is_shared()) {
acorn@20391 311 MetadataFactory::free_array<int>(loader_data, default_vtable_indices());
acorn@20391 312 }
acorn@20391 313 set_default_vtable_indices(NULL);
acorn@20391 314
acorn@20391 315
coleenp@13728 316 // This array is in Klass, but remove it with the InstanceKlass since
coleenp@13728 317 // this place would be the only caller and it can share memory with transitive
coleenp@13728 318 // interfaces.
hseigel@20684 319 if (secondary_supers() != NULL &&
hseigel@20684 320 secondary_supers() != Universe::the_empty_klass_array() &&
hseigel@20684 321 secondary_supers() != transitive_interfaces() &&
hseigel@20684 322 !secondary_supers()->is_shared()) {
coleenp@13728 323 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
coleenp@13728 324 }
coleenp@13728 325 set_secondary_supers(NULL);
coleenp@13728 326
coleenp@15935 327 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
coleenp@13728 328 set_transitive_interfaces(NULL);
coleenp@13728 329 set_local_interfaces(NULL);
coleenp@13728 330
hseigel@20684 331 if (fields() != NULL && !fields()->is_shared()) {
hseigel@20684 332 MetadataFactory::free_array<jushort>(loader_data, fields());
hseigel@20684 333 }
coleenp@13728 334 set_fields(NULL, 0);
coleenp@13728 335
coleenp@13728 336 // If a method from a redefined class is using this constant pool, don't
coleenp@13728 337 // delete it, yet. The new class's previous version will point to this.
coleenp@15935 338 if (constants() != NULL) {
coleenp@15935 339 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
hseigel@20684 340 if (!constants()->is_shared()) {
hseigel@20684 341 MetadataFactory::free_metadata(loader_data, constants());
hseigel@20684 342 }
aeriksso@31353 343 // Delete any cached resolution errors for the constant pool
aeriksso@31353 344 SystemDictionary::delete_resolution_error(constants());
aeriksso@31353 345
coleenp@15935 346 set_constants(NULL);
coleenp@15935 347 }
coleenp@13728 348
hseigel@20684 349 if (inner_classes() != NULL &&
hseigel@20684 350 inner_classes() != Universe::the_empty_short_array() &&
hseigel@20684 351 !inner_classes()->is_shared()) {
coleenp@13728 352 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
coleenp@13728 353 }
coleenp@13728 354 set_inner_classes(NULL);
coleenp@13728 355
hseigel@20684 356 // We should deallocate the Annotations instance if it's not in shared spaces.
hseigel@20684 357 if (annotations() != NULL && !annotations()->is_shared()) {
hseigel@20684 358 MetadataFactory::free_metadata(loader_data, annotations());
hseigel@20684 359 }
coleenp@13728 360 set_annotations(NULL);
coleenp@13728 361 }
coleenp@13728 362
coleenp@13728 363 bool InstanceKlass::should_be_initialized() const {
duke@1 364 return !is_initialized();
duke@1 365 }
duke@1 366
coleenp@13728 367 klassVtable* InstanceKlass::vtable() const {
coleenp@13728 368 return new klassVtable(this, start_of_vtable(), vtable_length() / vtableEntry::size());
duke@1 369 }
duke@1 370
coleenp@13728 371 klassItable* InstanceKlass::itable() const {
coleenp@13728 372 return new klassItable(instanceKlassHandle(this));
duke@1 373 }
duke@1 374
coleenp@13728 375 void InstanceKlass::eager_initialize(Thread *thread) {
duke@1 376 if (!EagerInitialization) return;
duke@1 377
duke@1 378 if (this->is_not_initialized()) {
duke@1 379 // abort if the the class has a class initializer
duke@1 380 if (this->class_initializer() != NULL) return;
duke@1 381
duke@1 382 // abort if it is java.lang.Object (initialization is handled in genesis)
coleenp@13728 383 Klass* super = this->super();
duke@1 384 if (super == NULL) return;
duke@1 385
duke@1 386 // abort if the super class should be initialized
coleenp@13728 387 if (!InstanceKlass::cast(super)->is_initialized()) return;
duke@1 388
duke@1 389 // call body to expose the this pointer
coleenp@23515 390 instanceKlassHandle this_k(thread, this);
coleenp@23515 391 eager_initialize_impl(this_k);
duke@1 392 }
duke@1 393 }
duke@1 394
coleenp@17826 395 // JVMTI spec thinks there are signers and protection domain in the
coleenp@17826 396 // instanceKlass. These accessors pretend these fields are there.
coleenp@17826 397 // The hprof specification also thinks these fields are in InstanceKlass.
coleenp@17826 398 oop InstanceKlass::protection_domain() const {
coleenp@17826 399 // return the protection_domain from the mirror
coleenp@17826 400 return java_lang_Class::protection_domain(java_mirror());
coleenp@17826 401 }
coleenp@17826 402
coleenp@17826 403 // To remove these from requires an incompatible change and CCC request.
coleenp@17826 404 objArrayOop InstanceKlass::signers() const {
coleenp@17826 405 // return the signers from the mirror
coleenp@17826 406 return java_lang_Class::signers(java_mirror());
coleenp@17826 407 }
coleenp@17826 408
coleenp@20379 409 oop InstanceKlass::init_lock() const {
coleenp@17826 410 // return the init lock from the mirror
iklam@21079 411 oop lock = java_lang_Class::init_lock(java_mirror());
dholmes@26556 412 // Prevent reordering with any access of initialization state
dholmes@26556 413 OrderAccess::loadload();
iklam@21079 414 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
iklam@21079 415 "only fully initialized state can have a null lock");
iklam@21079 416 return lock;
iklam@21079 417 }
iklam@21079 418
iklam@21079 419 // Set the initialization lock to null so the object can be GC'ed. Any racing
iklam@21079 420 // threads to get this lock will see a null lock and will not lock.
iklam@21079 421 // That's okay because they all check for initialized state after getting
iklam@21079 422 // the lock and return.
iklam@21079 423 void InstanceKlass::fence_and_clear_init_lock() {
iklam@21079 424 // make sure previous stores are all done, notably the init_state.
iklam@21079 425 OrderAccess::storestore();
iklam@21079 426 java_lang_Class::set_init_lock(java_mirror(), NULL);
iklam@21079 427 assert(!is_not_initialized(), "class must be initialized now");
coleenp@17826 428 }
duke@1 429
coleenp@23515 430 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
duke@1 431 EXCEPTION_MARK;
coleenp@23515 432 oop init_lock = this_k->init_lock();
iklam@21079 433 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
duke@1 434
duke@1 435 // abort if someone beat us to the initialization
coleenp@23515 436 if (!this_k->is_not_initialized()) return; // note: not equivalent to is_initialized()
coleenp@23515 437
coleenp@23515 438 ClassState old_state = this_k->init_state();
coleenp@23515 439 link_class_impl(this_k, true, THREAD);
duke@1 440 if (HAS_PENDING_EXCEPTION) {
duke@1 441 CLEAR_PENDING_EXCEPTION;
duke@1 442 // Abort if linking the class throws an exception.
duke@1 443
duke@1 444 // Use a test to avoid redundantly resetting the state if there's
duke@1 445 // no change. Set_init_state() asserts that state changes make
duke@1 446 // progress, whereas here we might just be spinning in place.
coleenp@23515 447 if( old_state != this_k->_init_state )
coleenp@23515 448 this_k->set_init_state (old_state);
duke@1 449 } else {
duke@1 450 // linking successfull, mark class as initialized
coleenp@23515 451 this_k->set_init_state (fully_initialized);
coleenp@23515 452 this_k->fence_and_clear_init_lock();
duke@1 453 // trace
mockner@34628 454 if (log_is_enabled(Info, classinit)) {
duke@1 455 ResourceMark rm(THREAD);
mockner@34628 456 log_info(classinit)("[Initialized %s without side effects]", this_k->external_name());
duke@1 457 }
duke@1 458 }
duke@1 459 }
duke@1 460
duke@1 461
duke@1 462 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
duke@1 463 // process. The step comments refers to the procedure described in that section.
duke@1 464 // Note: implementation moved to static method to expose the this pointer.
coleenp@13728 465 void InstanceKlass::initialize(TRAPS) {
duke@1 466 if (this->should_be_initialized()) {
duke@1 467 HandleMark hm(THREAD);
coleenp@23515 468 instanceKlassHandle this_k(THREAD, this);
coleenp@23515 469 initialize_impl(this_k, CHECK);
duke@1 470 // Note: at this point the class may be initialized
duke@1 471 // OR it may be in the state of being initialized
duke@1 472 // in case of recursive initialization!
duke@1 473 } else {
duke@1 474 assert(is_initialized(), "sanity check");
duke@1 475 }
duke@1 476 }
duke@1 477
duke@1 478
coleenp@13728 479 bool InstanceKlass::verify_code(
coleenp@23515 480 instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) {
duke@1 481 // 1) Verify the bytecodes
duke@1 482 Verifier::Mode mode =
duke@1 483 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
stefank@27680 484 return Verifier::verify(this_k, mode, this_k->should_verify_class(), THREAD);
duke@1 485 }
duke@1 486
duke@1 487
duke@1 488 // Used exclusively by the shared spaces dump mechanism to prevent
duke@1 489 // classes mapped into the shared regions in new VMs from appearing linked.
duke@1 490
coleenp@13728 491 void InstanceKlass::unlink_class() {
duke@1 492 assert(is_linked(), "must be linked");
duke@1 493 _init_state = loaded;
duke@1 494 }
duke@1 495
coleenp@13728 496 void InstanceKlass::link_class(TRAPS) {
duke@1 497 assert(is_loaded(), "must be loaded");
duke@1 498 if (!is_linked()) {
coleenp@13728 499 HandleMark hm(THREAD);
coleenp@23515 500 instanceKlassHandle this_k(THREAD, this);
coleenp@23515 501 link_class_impl(this_k, true, CHECK);
duke@1 502 }
duke@1 503 }
duke@1 504
duke@1 505 // Called to verify that a class can link during initialization, without
duke@1 506 // throwing a VerifyError.
coleenp@13728 507 bool InstanceKlass::link_class_or_fail(TRAPS) {
duke@1 508 assert(is_loaded(), "must be loaded");
duke@1 509 if (!is_linked()) {
coleenp@13728 510 HandleMark hm(THREAD);
coleenp@23515 511 instanceKlassHandle this_k(THREAD, this);
coleenp@23515 512 link_class_impl(this_k, false, CHECK_false);
duke@1 513 }
duke@1 514 return is_linked();
duke@1 515 }
duke@1 516
coleenp@13728 517 bool InstanceKlass::link_class_impl(
coleenp@23515 518 instanceKlassHandle this_k, bool throw_verifyerror, TRAPS) {
duke@1 519 // check for error state
coleenp@23515 520 if (this_k->is_in_error_state()) {
duke@1 521 ResourceMark rm(THREAD);
duke@1 522 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
coleenp@23515 523 this_k->external_name(), false);
duke@1 524 }
duke@1 525 // return if already verified
coleenp@23515 526 if (this_k->is_linked()) {
duke@1 527 return true;
duke@1 528 }
duke@1 529
duke@1 530 // Timing
duke@1 531 // timer handles recursion
duke@1 532 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
duke@1 533 JavaThread* jt = (JavaThread*)THREAD;
duke@1 534
duke@1 535 // link super class before linking this class
coleenp@23515 536 instanceKlassHandle super(THREAD, this_k->super());
duke@1 537 if (super.not_null()) {
duke@1 538 if (super->is_interface()) { // check if super class is an interface
duke@1 539 ResourceMark rm(THREAD);
duke@1 540 Exceptions::fthrow(
duke@1 541 THREAD_AND_LOCATION,
coleenp@8076 542 vmSymbols::java_lang_IncompatibleClassChangeError(),
duke@1 543 "class %s has interface %s as super class",
coleenp@23515 544 this_k->external_name(),
duke@1 545 super->external_name()
duke@1 546 );
duke@1 547 return false;
duke@1 548 }
duke@1 549
duke@1 550 link_class_impl(super, throw_verifyerror, CHECK_false);
duke@1 551 }
duke@1 552
duke@1 553 // link all interfaces implemented by this class before linking this class
coleenp@23515 554 Array<Klass*>* interfaces = this_k->local_interfaces();
duke@1 555 int num_interfaces = interfaces->length();
duke@1 556 for (int index = 0; index < num_interfaces; index++) {
duke@1 557 HandleMark hm(THREAD);
coleenp@13728 558 instanceKlassHandle ih(THREAD, interfaces->at(index));
duke@1 559 link_class_impl(ih, throw_verifyerror, CHECK_false);
duke@1 560 }
duke@1 561
duke@1 562 // in case the class is linked in the process of linking its superclasses
coleenp@23515 563 if (this_k->is_linked()) {
duke@1 564 return true;
duke@1 565 }
duke@1 566
mchung@3575 567 // trace only the link time for this klass that includes
mchung@3575 568 // the verification time
mchung@3575 569 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
mchung@3575 570 ClassLoader::perf_class_link_selftime(),
mchung@3575 571 ClassLoader::perf_classes_linked(),
mchung@3575 572 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@3575 573 jt->get_thread_stat()->perf_timers_addr(),
mchung@3575 574 PerfClassTraceTime::CLASS_LINK);
mchung@3575 575
duke@1 576 // verification & rewriting
duke@1 577 {
coleenp@23515 578 oop init_lock = this_k->init_lock();
iklam@21079 579 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
duke@1 580 // rewritten will have been set if loader constraint error found
duke@1 581 // on an earlier link attempt
duke@1 582 // don't verify or rewrite if already rewritten
coleenp@13728 583
coleenp@23515 584 if (!this_k->is_linked()) {
coleenp@23515 585 if (!this_k->is_rewritten()) {
duke@1 586 {
coleenp@23515 587 bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
duke@1 588 if (!verify_ok) {
duke@1 589 return false;
duke@1 590 }
duke@1 591 }
duke@1 592
duke@1 593 // Just in case a side-effect of verify linked this class already
duke@1 594 // (which can sometimes happen since the verifier loads classes
duke@1 595 // using custom class loaders, which are free to initialize things)
coleenp@23515 596 if (this_k->is_linked()) {
duke@1 597 return true;
duke@1 598 }
duke@1 599
duke@1 600 // also sets rewritten
coleenp@23515 601 this_k->rewrite_class(CHECK_false);
duke@1 602 }
duke@1 603
coleenp@9971 604 // relocate jsrs and link methods after they are all rewritten
coleenp@23515 605 this_k->link_methods(CHECK_false);
coleenp@9971 606
duke@1 607 // Initialize the vtable and interface table after
duke@1 608 // methods have been rewritten since rewrite may
coleenp@13728 609 // fabricate new Method*s.
duke@1 610 // also does loader constraint checking
coleenp@23515 611 if (!this_k()->is_shared()) {
duke@1 612 ResourceMark rm(THREAD);
coleenp@23515 613 this_k->vtable()->initialize_vtable(true, CHECK_false);
coleenp@23515 614 this_k->itable()->initialize_itable(true, CHECK_false);
duke@1 615 }
duke@1 616 #ifdef ASSERT
duke@1 617 else {
duke@1 618 ResourceMark rm(THREAD);
coleenp@23515 619 this_k->vtable()->verify(tty, true);
duke@1 620 // In case itable verification is ever added.
coleenp@23515 621 // this_k->itable()->verify(tty, true);
duke@1 622 }
duke@1 623 #endif
coleenp@23515 624 this_k->set_init_state(linked);
duke@1 625 if (JvmtiExport::should_post_class_prepare()) {
duke@1 626 Thread *thread = THREAD;
duke@1 627 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
coleenp@23515 628 JvmtiExport::post_class_prepare((JavaThread *) thread, this_k());
duke@1 629 }
duke@1 630 }
duke@1 631 }
duke@1 632 return true;
duke@1 633 }
duke@1 634
duke@1 635
duke@1 636 // Rewrite the byte codes of all of the methods of a class.
duke@1 637 // The rewriter must be called exactly once. Rewriting must happen after
duke@1 638 // verification but before the first method of the class is executed.
coleenp@13728 639 void InstanceKlass::rewrite_class(TRAPS) {
duke@1 640 assert(is_loaded(), "must be loaded");
coleenp@23515 641 instanceKlassHandle this_k(THREAD, this);
coleenp@23515 642 if (this_k->is_rewritten()) {
coleenp@23515 643 assert(this_k()->is_shared(), "rewriting an unshared class?");
duke@1 644 return;
duke@1 645 }
coleenp@23515 646 Rewriter::rewrite(this_k, CHECK);
coleenp@23515 647 this_k->set_rewritten();
duke@1 648 }
duke@1 649
coleenp@9971 650 // Now relocate and link method entry points after class is rewritten.
coleenp@9971 651 // This is outside is_rewritten flag. In case of an exception, it can be
coleenp@9971 652 // executed more than once.
coleenp@15099 653 void InstanceKlass::link_methods(TRAPS) {
coleenp@15099 654 int len = methods()->length();
coleenp@15099 655 for (int i = len-1; i >= 0; i--) {
coleenp@15099 656 methodHandle m(THREAD, methods()->at(i));
coleenp@15099 657
coleenp@15099 658 // Set up method entry points for compiler and interpreter .
coleenp@15099 659 m->link_method(m, CHECK);
coleenp@15099 660 }
coleenp@9971 661 }
coleenp@9971 662
acorn@27402 663 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
acorn@27402 664 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_k, TRAPS) {
acorn@27402 665 if (this_k->has_default_methods()) {
acorn@27402 666 for (int i = 0; i < this_k->local_interfaces()->length(); ++i) {
acorn@27402 667 Klass* iface = this_k->local_interfaces()->at(i);
acorn@27402 668 InstanceKlass* ik = InstanceKlass::cast(iface);
acorn@27402 669 if (ik->should_be_initialized()) {
acorn@27402 670 if (ik->has_default_methods()) {
acorn@27402 671 ik->initialize_super_interfaces(ik, THREAD);
acorn@27402 672 }
acorn@27402 673 // Only initialize() interfaces that "declare" concrete methods.
acorn@27402 674 // has_default_methods drives searching superinterfaces since it
acorn@27402 675 // means has_default_methods in its superinterface hierarchy
acorn@27402 676 if (!HAS_PENDING_EXCEPTION && ik->declares_default_methods()) {
acorn@27402 677 ik->initialize(THREAD);
acorn@27402 678 }
acorn@27402 679 if (HAS_PENDING_EXCEPTION) {
acorn@27402 680 Handle e(THREAD, PENDING_EXCEPTION);
acorn@27402 681 CLEAR_PENDING_EXCEPTION;
acorn@27402 682 {
acorn@27402 683 EXCEPTION_MARK;
acorn@27402 684 // Locks object, set state, and notify all waiting threads
acorn@27402 685 this_k->set_initialization_state_and_notify(
acorn@27402 686 initialization_error, THREAD);
acorn@27402 687
acorn@27402 688 // ignore any exception thrown, superclass initialization error is
acorn@27402 689 // thrown below
acorn@27402 690 CLEAR_PENDING_EXCEPTION;
acorn@27402 691 }
acorn@27402 692 THROW_OOP(e());
acorn@27402 693 }
acorn@27402 694 }
acorn@27402 695 }
acorn@27402 696 }
acorn@27402 697 }
duke@1 698
coleenp@23515 699 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
duke@1 700 // Make sure klass is linked (verified) before initialization
duke@1 701 // A class could already be verified, since it has been reflected upon.
coleenp@23515 702 this_k->link_class(CHECK);
coleenp@23515 703
coleenp@33602 704 DTRACE_CLASSINIT_PROBE(required, this_k(), -1);
fparain@5089 705
fparain@5089 706 bool wait = false;
fparain@5089 707
duke@1 708 // refer to the JVM book page 47 for description of steps
duke@1 709 // Step 1
coleenp@13728 710 {
coleenp@23515 711 oop init_lock = this_k->init_lock();
iklam@21079 712 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
duke@1 713
duke@1 714 Thread *self = THREAD; // it's passed the current thread
duke@1 715
duke@1 716 // Step 2
duke@1 717 // If we were to use wait() instead of waitInterruptibly() then
duke@1 718 // we might end up throwing IE from link/symbol resolution sites
duke@1 719 // that aren't expected to throw. This would wreak havoc. See 6320309.
coleenp@23515 720 while(this_k->is_being_initialized() && !this_k->is_reentrant_initialization(self)) {
fparain@5089 721 wait = true;
duke@1 722 ol.waitUninterruptibly(CHECK);
duke@1 723 }
duke@1 724
duke@1 725 // Step 3
coleenp@23515 726 if (this_k->is_being_initialized() && this_k->is_reentrant_initialization(self)) {
coleenp@33602 727 DTRACE_CLASSINIT_PROBE_WAIT(recursive, this_k(), -1,wait);
duke@1 728 return;
fparain@5089 729 }
duke@1 730
duke@1 731 // Step 4
coleenp@23515 732 if (this_k->is_initialized()) {
coleenp@33602 733 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, this_k(), -1,wait);
duke@1 734 return;
fparain@5089 735 }
duke@1 736
duke@1 737 // Step 5
coleenp@23515 738 if (this_k->is_in_error_state()) {
coleenp@33602 739 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, this_k(), -1,wait);
duke@1 740 ResourceMark rm(THREAD);
duke@1 741 const char* desc = "Could not initialize class ";
coleenp@23515 742 const char* className = this_k->external_name();
duke@1 743 size_t msglen = strlen(desc) + strlen(className) + 1;
kamg@6421 744 char* message = NEW_RESOURCE_ARRAY(char, msglen);
duke@1 745 if (NULL == message) {
duke@1 746 // Out of memory: can't create detailed error message
duke@1 747 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
duke@1 748 } else {
duke@1 749 jio_snprintf(message, msglen, "%s%s", desc, className);
duke@1 750 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
duke@1 751 }
duke@1 752 }
duke@1 753
duke@1 754 // Step 6
coleenp@23515 755 this_k->set_init_state(being_initialized);
coleenp@23515 756 this_k->set_init_thread(self);
duke@1 757 }
duke@1 758
duke@1 759 // Step 7
coleenp@23515 760 Klass* super_klass = this_k->super();
coleenp@23515 761 if (super_klass != NULL && !this_k->is_interface() && super_klass->should_be_initialized()) {
hseigel@14488 762 super_klass->initialize(THREAD);
duke@1 763
duke@1 764 if (HAS_PENDING_EXCEPTION) {
duke@1 765 Handle e(THREAD, PENDING_EXCEPTION);
duke@1 766 CLEAR_PENDING_EXCEPTION;
duke@1 767 {
duke@1 768 EXCEPTION_MARK;
coleenp@23515 769 this_k->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads
duke@1 770 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below
duke@1 771 }
coleenp@33602 772 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, this_k(), -1,wait);
duke@1 773 THROW_OOP(e());
duke@1 774 }
duke@1 775 }
duke@1 776
acorn@34303 777 // If C is an interface that declares a non-abstract, non-static method,
acorn@34303 778 // the initialization of a class (not an interface) that implements C directly or
acorn@34303 779 // indirectly.
acorn@27402 780 // Recursively initialize any superinterfaces that declare default methods
acorn@27402 781 // Only need to recurse if has_default_methods which includes declaring and
acorn@27402 782 // inheriting default methods
acorn@34303 783 if (!this_k->is_interface() && this_k->has_default_methods()) {
acorn@27402 784 this_k->initialize_super_interfaces(this_k, CHECK);
kamg@14385 785 }
kamg@14385 786
duke@1 787 // Step 8
duke@1 788 {
duke@1 789 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
duke@1 790 JavaThread* jt = (JavaThread*)THREAD;
coleenp@33602 791 DTRACE_CLASSINIT_PROBE_WAIT(clinit, this_k(), -1,wait);
duke@1 792 // Timer includes any side effects of class initialization (resolution,
duke@1 793 // etc), but not recursive entry into call_class_initializer().
mchung@3575 794 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
mchung@3575 795 ClassLoader::perf_class_init_selftime(),
mchung@3575 796 ClassLoader::perf_classes_inited(),
mchung@3575 797 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@3575 798 jt->get_thread_stat()->perf_timers_addr(),
mchung@3575 799 PerfClassTraceTime::CLASS_CLINIT);
coleenp@23515 800 this_k->call_class_initializer(THREAD);
duke@1 801 }
duke@1 802
duke@1 803 // Step 9
duke@1 804 if (!HAS_PENDING_EXCEPTION) {
coleenp@23515 805 this_k->set_initialization_state_and_notify(fully_initialized, CHECK);
duke@1 806 { ResourceMark rm(THREAD);
coleenp@23515 807 debug_only(this_k->vtable()->verify(tty, true);)
duke@1 808 }
duke@1 809 }
duke@1 810 else {
duke@1 811 // Step 10 and 11
duke@1 812 Handle e(THREAD, PENDING_EXCEPTION);
duke@1 813 CLEAR_PENDING_EXCEPTION;
jbachorik@22897 814 // JVMTI has already reported the pending exception
jbachorik@22897 815 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@22897 816 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
duke@1 817 {
duke@1 818 EXCEPTION_MARK;
coleenp@23515 819 this_k->set_initialization_state_and_notify(initialization_error, THREAD);
duke@1 820 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
jbachorik@22897 821 // JVMTI has already reported the pending exception
jbachorik@22897 822 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@22897 823 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
duke@1 824 }
coleenp@33602 825 DTRACE_CLASSINIT_PROBE_WAIT(error, this_k(), -1,wait);
never@4571 826 if (e->is_a(SystemDictionary::Error_klass())) {
duke@1 827 THROW_OOP(e());
duke@1 828 } else {
duke@1 829 JavaCallArguments args(e);
coleenp@8076 830 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
coleenp@8076 831 vmSymbols::throwable_void_signature(),
duke@1 832 &args);
duke@1 833 }
duke@1 834 }
coleenp@33602 835 DTRACE_CLASSINIT_PROBE_WAIT(end, this_k(), -1,wait);
duke@1 836 }
duke@1 837
duke@1 838
duke@1 839 // Note: implementation moved to static method to expose the this pointer.
coleenp@13728 840 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
coleenp@13728 841 instanceKlassHandle kh(THREAD, this);
duke@1 842 set_initialization_state_and_notify_impl(kh, state, CHECK);
duke@1 843 }
duke@1 844
coleenp@23515 845 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
coleenp@23515 846 oop init_lock = this_k->init_lock();
iklam@21079 847 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
coleenp@23515 848 this_k->set_init_state(state);
coleenp@23515 849 this_k->fence_and_clear_init_lock();
duke@1 850 ol.notify_all(CHECK);
duke@1 851 }
duke@1 852
jiangli@12369 853 // The embedded _implementor field can only record one implementor.
jiangli@12369 854 // When there are more than one implementors, the _implementor field
coleenp@13728 855 // is set to the interface Klass* itself. Following are the possible
jiangli@12369 856 // values for the _implementor field:
jiangli@12369 857 // NULL - no implementor
coleenp@13728 858 // implementor Klass* - one implementor
jiangli@12369 859 // self - more than one implementor
jiangli@12369 860 //
jiangli@12369 861 // The _implementor field only exists for interfaces.
coleenp@13728 862 void InstanceKlass::add_implementor(Klass* k) {
duke@1 863 assert(Compile_lock->owned_by_self(), "");
jiangli@12369 864 assert(is_interface(), "not interface");
duke@1 865 // Filter out my subinterfaces.
duke@1 866 // (Note: Interfaces are never on the subklass list.)
coleenp@13728 867 if (InstanceKlass::cast(k)->is_interface()) return;
duke@1 868
duke@1 869 // Filter out subclasses whose supers already implement me.
duke@1 870 // (Note: CHA must walk subclasses of direct implementors
duke@1 871 // in order to locate indirect implementors.)
coleenp@33602 872 Klass* sk = k->super();
coleenp@13728 873 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this))
duke@1 874 // We only need to check one immediate superclass, since the
duke@1 875 // implements_interface query looks at transitive_interfaces.
duke@1 876 // Any supers of the super have the same (or fewer) transitive_interfaces.
duke@1 877 return;
duke@1 878
coleenp@13728 879 Klass* ik = implementor();
jiangli@12369 880 if (ik == NULL) {
jiangli@12369 881 set_implementor(k);
coleenp@13728 882 } else if (ik != this) {
jiangli@12369 883 // There is already an implementor. Use itself as an indicator of
jiangli@12369 884 // more than one implementors.
coleenp@13728 885 set_implementor(this);
duke@1 886 }
duke@1 887
duke@1 888 // The implementor also implements the transitive_interfaces
duke@1 889 for (int index = 0; index < local_interfaces()->length(); index++) {
coleenp@13728 890 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k);
duke@1 891 }
duke@1 892 }
duke@1 893
coleenp@13728 894 void InstanceKlass::init_implementor() {
jiangli@12369 895 if (is_interface()) {
jiangli@12369 896 set_implementor(NULL);
jiangli@12369 897 }
duke@1 898 }
duke@1 899
duke@1 900
coleenp@13728 901 void InstanceKlass::process_interfaces(Thread *thread) {
duke@1 902 // link this class into the implementors list of every interface it implements
duke@1 903 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
coleenp@13728 904 assert(local_interfaces()->at(i)->is_klass(), "must be a klass");
coleenp@13728 905 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i));
duke@1 906 assert(interf->is_interface(), "expected interface");
coleenp@23515 907 interf->add_implementor(this);
duke@1 908 }
duke@1 909 }
duke@1 910
coleenp@13728 911 bool InstanceKlass::can_be_primary_super_slow() const {
duke@1 912 if (is_interface())
duke@1 913 return false;
duke@1 914 else
duke@1 915 return Klass::can_be_primary_super_slow();
duke@1 916 }
duke@1 917
coleenp@13728 918 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots) {
duke@1 919 // The secondaries are the implemented interfaces.
coleenp@33602 920 Array<Klass*>* interfaces = transitive_interfaces();
duke@1 921 int num_secondaries = num_extra_slots + interfaces->length();
duke@1 922 if (num_secondaries == 0) {
coleenp@13728 923 // Must share this for correct bootstrapping!
coleenp@13728 924 set_secondary_supers(Universe::the_empty_klass_array());
coleenp@13728 925 return NULL;
duke@1 926 } else if (num_extra_slots == 0) {
coleenp@13728 927 // The secondary super list is exactly the same as the transitive interfaces.
coleenp@13728 928 // Redefine classes has to be careful not to delete this!
coleenp@13728 929 set_secondary_supers(interfaces);
coleenp@13728 930 return NULL;
duke@1 931 } else {
coleenp@13728 932 // Copy transitive interfaces to a temporary growable array to be constructed
coleenp@13728 933 // into the secondary super list with extra slots.
coleenp@13728 934 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length());
duke@1 935 for (int i = 0; i < interfaces->length(); i++) {
coleenp@13728 936 secondaries->push(interfaces->at(i));
duke@1 937 }
duke@1 938 return secondaries;
duke@1 939 }
duke@1 940 }
duke@1 941
coleenp@13728 942 bool InstanceKlass::compute_is_subtype_of(Klass* k) {
hseigel@14488 943 if (k->is_interface()) {
duke@1 944 return implements_interface(k);
duke@1 945 } else {
duke@1 946 return Klass::compute_is_subtype_of(k);
duke@1 947 }
duke@1 948 }
duke@1 949
coleenp@13728 950 bool InstanceKlass::implements_interface(Klass* k) const {
coleenp@13728 951 if (this == k) return true;
hseigel@14488 952 assert(k->is_interface(), "should be an interface class");
duke@1 953 for (int i = 0; i < transitive_interfaces()->length(); i++) {
coleenp@13728 954 if (transitive_interfaces()->at(i) == k) {
duke@1 955 return true;
duke@1 956 }
duke@1 957 }
duke@1 958 return false;
duke@1 959 }
duke@1 960
hseigel@21768 961 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
hseigel@21768 962 // Verify direct super interface
hseigel@21768 963 if (this == k) return true;
hseigel@21768 964 assert(k->is_interface(), "should be an interface class");
hseigel@21768 965 for (int i = 0; i < local_interfaces()->length(); i++) {
hseigel@21768 966 if (local_interfaces()->at(i) == k) {
hseigel@21768 967 return true;
hseigel@21768 968 }
hseigel@21768 969 }
hseigel@21768 970 return false;
hseigel@21768 971 }
hseigel@21768 972
coleenp@13728 973 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
duke@1 974 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
duke@1 975 if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
martin@3576 976 report_java_out_of_memory("Requested array size exceeds VM limit");
sspitsyn@12114 977 JvmtiExport::post_array_size_exhausted();
duke@1 978 THROW_OOP_0(Universe::out_of_memory_error_array_size());
duke@1 979 }
duke@1 980 int size = objArrayOopDesc::object_size(length);
coleenp@13728 981 Klass* ak = array_klass(n, CHECK_NULL);
duke@1 982 KlassHandle h_ak (THREAD, ak);
duke@1 983 objArrayOop o =
duke@1 984 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
duke@1 985 return o;
duke@1 986 }
duke@1 987
coleenp@13728 988 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
duke@1 989 if (TraceFinalizerRegistration) {
duke@1 990 tty->print("Registered ");
duke@1 991 i->print_value_on(tty);
david@33148 992 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", p2i(i));
duke@1 993 }
duke@1 994 instanceHandle h_i(THREAD, i);
duke@1 995 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
duke@1 996 JavaValue result(T_VOID);
duke@1 997 JavaCallArguments args(h_i);
duke@1 998 methodHandle mh (THREAD, Universe::finalizer_register_method());
duke@1 999 JavaCalls::call(&result, mh, &args, CHECK_NULL);
duke@1 1000 return h_i();
duke@1 1001 }
duke@1 1002
coleenp@13728 1003 instanceOop InstanceKlass::allocate_instance(TRAPS) {
duke@1 1004 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
duke@1 1005 int size = size_helper(); // Query before forming handle.
duke@1 1006
coleenp@13728 1007 KlassHandle h_k(THREAD, this);
duke@1 1008
duke@1 1009 instanceOop i;
duke@1 1010
duke@1 1011 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
duke@1 1012 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
duke@1 1013 i = register_finalizer(i, CHECK_NULL);
duke@1 1014 }
duke@1 1015 return i;
duke@1 1016 }
duke@1 1017
coleenp@13728 1018 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
duke@1 1019 if (is_interface() || is_abstract()) {
duke@1 1020 ResourceMark rm(THREAD);
duke@1 1021 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
duke@1 1022 : vmSymbols::java_lang_InstantiationException(), external_name());
duke@1 1023 }
coleenp@13728 1024 if (this == SystemDictionary::Class_klass()) {
duke@1 1025 ResourceMark rm(THREAD);
duke@1 1026 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
duke@1 1027 : vmSymbols::java_lang_IllegalAccessException(), external_name());
duke@1 1028 }
duke@1 1029 }
duke@1 1030
coleenp@13728 1031 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
coleenp@23515 1032 instanceKlassHandle this_k(THREAD, this);
coleenp@23515 1033 return array_klass_impl(this_k, or_null, n, THREAD);
duke@1 1034 }
duke@1 1035
coleenp@23515 1036 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_k, bool or_null, int n, TRAPS) {
coleenp@23515 1037 if (this_k->array_klasses() == NULL) {
duke@1 1038 if (or_null) return NULL;
duke@1 1039
duke@1 1040 ResourceMark rm;
duke@1 1041 JavaThread *jt = (JavaThread *)THREAD;
duke@1 1042 {
duke@1 1043 // Atomic creation of array_klasses
duke@1 1044 MutexLocker mc(Compile_lock, THREAD); // for vtables
duke@1 1045 MutexLocker ma(MultiArray_lock, THREAD);
duke@1 1046
duke@1 1047 // Check if update has already taken place
coleenp@23515 1048 if (this_k->array_klasses() == NULL) {
coleenp@23515 1049 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_k->class_loader_data(), 1, this_k, CHECK_NULL);
coleenp@23515 1050 this_k->set_array_klasses(k);
duke@1 1051 }
duke@1 1052 }
duke@1 1053 }
duke@1 1054 // _this will always be set at this point
coleenp@23515 1055 ObjArrayKlass* oak = (ObjArrayKlass*)this_k->array_klasses();
duke@1 1056 if (or_null) {
duke@1 1057 return oak->array_klass_or_null(n);
duke@1 1058 }
stefank@27680 1059 return oak->array_klass(n, THREAD);
duke@1 1060 }
duke@1 1061
coleenp@13728 1062 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
duke@1 1063 return array_klass_impl(or_null, 1, THREAD);
duke@1 1064 }
duke@1 1065
coleenp@13728 1066 void InstanceKlass::call_class_initializer(TRAPS) {
coleenp@13728 1067 instanceKlassHandle ik (THREAD, this);
duke@1 1068 call_class_initializer_impl(ik, THREAD);
duke@1 1069 }
duke@1 1070
duke@1 1071 static int call_class_initializer_impl_counter = 0; // for debugging
duke@1 1072
coleenp@13728 1073 Method* InstanceKlass::class_initializer() {
coleenp@13728 1074 Method* clinit = find_method(
kamg@8653 1075 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
kamg@8653 1076 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
kamg@8653 1077 return clinit;
kamg@8653 1078 }
kamg@8653 1079 return NULL;
duke@1 1080 }
duke@1 1081
coleenp@23515 1082 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_k, TRAPS) {
minqi@14477 1083 if (ReplayCompiles &&
minqi@14477 1084 (ReplaySuppressInitializers == 1 ||
coleenp@23515 1085 ReplaySuppressInitializers >= 2 && this_k->class_loader() != NULL)) {
minqi@14477 1086 // Hide the existence of the initializer for the purpose of replaying the compile
minqi@14477 1087 return;
minqi@14477 1088 }
minqi@14477 1089
coleenp@23515 1090 methodHandle h_method(THREAD, this_k->class_initializer());
coleenp@23515 1091 assert(!this_k->is_initialized(), "we cannot initialize twice");
mockner@34628 1092 if (log_is_enabled(Info, classinit)) {
mockner@34628 1093 ResourceMark rm;
mockner@34628 1094 outputStream* log = LogHandle(classinit)::info_stream();
mockner@34628 1095 log->print("%d Initializing ", call_class_initializer_impl_counter++);
mockner@34628 1096 this_k->name()->print_value_on(log);
mockner@34628 1097 log->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this_k()));
duke@1 1098 }
duke@1 1099 if (h_method() != NULL) {
duke@1 1100 JavaCallArguments args; // No arguments
duke@1 1101 JavaValue result(T_VOID);
duke@1 1102 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
duke@1 1103 }
duke@1 1104 }
duke@1 1105
duke@1 1106
coleenp@33593 1107 void InstanceKlass::mask_for(const methodHandle& method, int bci,
duke@1 1108 InterpreterOopMap* entry_for) {
duke@1 1109 // Dirty read, then double-check under a lock.
duke@1 1110 if (_oop_map_cache == NULL) {
duke@1 1111 // Otherwise, allocate a new one.
duke@1 1112 MutexLocker x(OopMapCacheAlloc_lock);
duke@1 1113 // First time use. Allocate a cache in C heap
duke@1 1114 if (_oop_map_cache == NULL) {
goetz@22859 1115 // Release stores from OopMapCache constructor before assignment
goetz@22859 1116 // to _oop_map_cache. C++ compilers on ppc do not emit the
goetz@22859 1117 // required memory barrier only because of the volatile
goetz@22859 1118 // qualifier of _oop_map_cache.
goetz@22859 1119 OrderAccess::release_store_ptr(&_oop_map_cache, new OopMapCache());
duke@1 1120 }
duke@1 1121 }
duke@1 1122 // _oop_map_cache is constant after init; lookup below does is own locking.
duke@1 1123 _oop_map_cache->lookup(method, bci, entry_for);
duke@1 1124 }
duke@1 1125
duke@1 1126
coleenp@13728 1127 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
coleenp@13728 1128 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1129 Symbol* f_name = fs.name();
never@10546 1130 Symbol* f_sig = fs.signature();
duke@1 1131 if (f_name == name && f_sig == sig) {
drchase@20017 1132 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
duke@1 1133 return true;
duke@1 1134 }
duke@1 1135 }
duke@1 1136 return false;
duke@1 1137 }
duke@1 1138
duke@1 1139
coleenp@13728 1140 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@1 1141 const int n = local_interfaces()->length();
duke@1 1142 for (int i = 0; i < n; i++) {
coleenp@13728 1143 Klass* intf1 = local_interfaces()->at(i);
hseigel@14488 1144 assert(intf1->is_interface(), "just checking type");
duke@1 1145 // search for field in current interface
coleenp@13728 1146 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
duke@1 1147 assert(fd->is_static(), "interface field must be static");
duke@1 1148 return intf1;
duke@1 1149 }
duke@1 1150 // search for field in direct superinterfaces
coleenp@13728 1151 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
duke@1 1152 if (intf2 != NULL) return intf2;
duke@1 1153 }
duke@1 1154 // otherwise field lookup fails
duke@1 1155 return NULL;
duke@1 1156 }
duke@1 1157
duke@1 1158
coleenp@13728 1159 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@1 1160 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@1 1161 // 1) search for field in current klass
duke@1 1162 if (find_local_field(name, sig, fd)) {
coleenp@13728 1163 return const_cast<InstanceKlass*>(this);
duke@1 1164 }
duke@1 1165 // 2) search for field recursively in direct superinterfaces
coleenp@13728 1166 { Klass* intf = find_interface_field(name, sig, fd);
duke@1 1167 if (intf != NULL) return intf;
duke@1 1168 }
duke@1 1169 // 3) apply field lookup recursively if superclass exists
coleenp@13728 1170 { Klass* supr = super();
coleenp@13728 1171 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd);
duke@1 1172 }
duke@1 1173 // 4) otherwise field lookup fails
duke@1 1174 return NULL;
duke@1 1175 }
duke@1 1176
duke@1 1177
coleenp@13728 1178 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
duke@1 1179 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@1 1180 // 1) search for field in current klass
duke@1 1181 if (find_local_field(name, sig, fd)) {
coleenp@13728 1182 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
duke@1 1183 }
duke@1 1184 // 2) search for field recursively in direct superinterfaces
duke@1 1185 if (is_static) {
coleenp@13728 1186 Klass* intf = find_interface_field(name, sig, fd);
duke@1 1187 if (intf != NULL) return intf;
duke@1 1188 }
duke@1 1189 // 3) apply field lookup recursively if superclass exists
coleenp@13728 1190 { Klass* supr = super();
coleenp@13728 1191 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
duke@1 1192 }
duke@1 1193 // 4) otherwise field lookup fails
duke@1 1194 return NULL;
duke@1 1195 }
duke@1 1196
duke@1 1197
coleenp@13728 1198 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
coleenp@13728 1199 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1200 if (fs.offset() == offset) {
drchase@20017 1201 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
duke@1 1202 if (fd->is_static() == is_static) return true;
duke@1 1203 }
duke@1 1204 }
duke@1 1205 return false;
duke@1 1206 }
duke@1 1207
duke@1 1208
coleenp@13728 1209 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
coleenp@13728 1210 Klass* klass = const_cast<InstanceKlass*>(this);
duke@1 1211 while (klass != NULL) {
coleenp@13728 1212 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
duke@1 1213 return true;
duke@1 1214 }
hseigel@14488 1215 klass = klass->super();
duke@1 1216 }
duke@1 1217 return false;
duke@1 1218 }
duke@1 1219
duke@1 1220
coleenp@13728 1221 void InstanceKlass::methods_do(void f(Method* method)) {
coleenp@24658 1222 // Methods aren't stable until they are loaded. This can be read outside
coleenp@24658 1223 // a lock through the ClassLoaderData for profiling
coleenp@24658 1224 if (!is_loaded()) {
coleenp@24658 1225 return;
coleenp@24658 1226 }
coleenp@24658 1227
duke@1 1228 int len = methods()->length();
duke@1 1229 for (int index = 0; index < len; index++) {
coleenp@13728 1230 Method* m = methods()->at(index);
duke@1 1231 assert(m->is_method(), "must be method");
duke@1 1232 f(m);
duke@1 1233 }
duke@1 1234 }
duke@1 1235
never@8725 1236
coleenp@13728 1237 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
never@10546 1238 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@10546 1239 if (fs.access_flags().is_static()) {
drchase@20017 1240 fieldDescriptor& fd = fs.field_descriptor();
never@10546 1241 cl->do_field(&fd);
never@10546 1242 }
duke@1 1243 }
duke@1 1244 }
duke@1 1245
duke@1 1246
coleenp@23872 1247 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
coleenp@13728 1248 instanceKlassHandle h_this(THREAD, this);
coleenp@23872 1249 do_local_static_fields_impl(h_this, f, mirror, CHECK);
duke@1 1250 }
duke@1 1251
duke@1 1252
coleenp@23872 1253 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_k,
coleenp@23872 1254 void f(fieldDescriptor* fd, Handle, TRAPS), Handle mirror, TRAPS) {
coleenp@23515 1255 for (JavaFieldStream fs(this_k()); !fs.done(); fs.next()) {
never@10546 1256 if (fs.access_flags().is_static()) {
drchase@20017 1257 fieldDescriptor& fd = fs.field_descriptor();
coleenp@23872 1258 f(&fd, mirror, CHECK);
never@10546 1259 }
duke@1 1260 }
duke@1 1261 }
duke@1 1262
duke@1 1263
kvn@217 1264 static int compare_fields_by_offset(int* a, int* b) {
kvn@217 1265 return a[0] - b[0];
kvn@217 1266 }
kvn@217 1267
coleenp@13728 1268 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
coleenp@13728 1269 InstanceKlass* super = superklass();
duke@1 1270 if (super != NULL) {
duke@1 1271 super->do_nonstatic_fields(cl);
duke@1 1272 }
kvn@217 1273 fieldDescriptor fd;
never@10546 1274 int length = java_fields_count();
kvn@217 1275 // In DebugInfo nonstatic fields are sorted by offset.
zgu@13195 1276 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass);
kvn@217 1277 int j = 0;
never@10546 1278 for (int i = 0; i < length; i += 1) {
drchase@20017 1279 fd.reinitialize(this, i);
kvn@217 1280 if (!fd.is_static()) {
kvn@217 1281 fields_sorted[j + 0] = fd.offset();
kvn@217 1282 fields_sorted[j + 1] = i;
kvn@217 1283 j += 2;
kvn@217 1284 }
duke@1 1285 }
kvn@217 1286 if (j > 0) {
kvn@217 1287 length = j;
kvn@217 1288 // _sort_Fn is defined in growableArray.hpp.
kvn@217 1289 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
kvn@217 1290 for (int i = 0; i < length; i += 2) {
drchase@20017 1291 fd.reinitialize(this, fields_sorted[i + 1]);
kvn@217 1292 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
kvn@217 1293 cl->do_field(&fd);
kvn@217 1294 }
kvn@217 1295 }
coleenp@27880 1296 FREE_C_HEAP_ARRAY(int, fields_sorted);
duke@1 1297 }
duke@1 1298
duke@1 1299
coleenp@13728 1300 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
coleenp@13728 1301 if (array_klasses() != NULL)
coleenp@13952 1302 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD);
coleenp@13728 1303 }
coleenp@13728 1304
coleenp@13728 1305 void InstanceKlass::array_klasses_do(void f(Klass* k)) {
duke@1 1306 if (array_klasses() != NULL)
coleenp@13952 1307 ArrayKlass::cast(array_klasses())->array_klasses_do(f);
duke@1 1308 }
duke@1 1309
duke@1 1310 #ifdef ASSERT
mgronlun@34666 1311 static int linear_search(const Array<Method*>* methods,
mgronlun@34666 1312 const Symbol* name,
mgronlun@34666 1313 const Symbol* signature) {
mgronlun@34666 1314 const int len = methods->length();
duke@1 1315 for (int index = 0; index < len; index++) {
mgronlun@34666 1316 const Method* const m = methods->at(index);
duke@1 1317 assert(m->is_method(), "must be method");
duke@1 1318 if (m->signature() == signature && m->name() == name) {
duke@1 1319 return index;
duke@1 1320 }
duke@1 1321 }
duke@1 1322 return -1;
duke@1 1323 }
duke@1 1324 #endif
duke@1 1325
mgronlun@34666 1326 static int binary_search(const Array<Method*>* methods, const Symbol* name) {
duke@1 1327 int len = methods->length();
duke@1 1328 // methods are sorted, so do binary search
duke@1 1329 int l = 0;
duke@1 1330 int h = len - 1;
duke@1 1331 while (l <= h) {
duke@1 1332 int mid = (l + h) >> 1;
coleenp@13728 1333 Method* m = methods->at(mid);
duke@1 1334 assert(m->is_method(), "must be method");
duke@1 1335 int res = m->name()->fast_compare(name);
duke@1 1336 if (res == 0) {
kamg@14385 1337 return mid;
duke@1 1338 } else if (res < 0) {
duke@1 1339 l = mid + 1;
duke@1 1340 } else {
duke@1 1341 h = mid - 1;
duke@1 1342 }
duke@1 1343 }
kamg@14385 1344 return -1;
kamg@14385 1345 }
kamg@14385 1346
acorn@20391 1347 // find_method looks up the name/signature in the local methods array
mgronlun@34666 1348 Method* InstanceKlass::find_method(const Symbol* name,
mgronlun@34666 1349 const Symbol* signature) const {
acorn@32189 1350 return find_method_impl(name, signature, find_overpass, find_static, find_private);
lfoltan@23999 1351 }
lfoltan@23999 1352
mgronlun@34666 1353 Method* InstanceKlass::find_method_impl(const Symbol* name,
mgronlun@34666 1354 const Symbol* signature,
acorn@32189 1355 OverpassLookupMode overpass_mode,
acorn@32189 1356 StaticLookupMode static_mode,
acorn@32189 1357 PrivateLookupMode private_mode) const {
mgronlun@34666 1358 return InstanceKlass::find_method_impl(methods(),
mgronlun@34666 1359 name,
mgronlun@34666 1360 signature,
mgronlun@34666 1361 overpass_mode,
mgronlun@34666 1362 static_mode,
mgronlun@34666 1363 private_mode);
kamg@14385 1364 }
kamg@14385 1365
acorn@21913 1366 // find_instance_method looks up the name/signature in the local methods array
acorn@21913 1367 // and skips over static methods
mgronlun@34666 1368 Method* InstanceKlass::find_instance_method(const Array<Method*>* methods,
mgronlun@34666 1369 const Symbol* name,
mgronlun@34666 1370 const Symbol* signature) {
mgronlun@34666 1371 Method* const meth = InstanceKlass::find_method_impl(methods,
mgronlun@34666 1372 name,
mgronlun@34666 1373 signature,
mgronlun@34666 1374 find_overpass,
mgronlun@34666 1375 skip_static,
mgronlun@34666 1376 find_private);
mgronlun@34666 1377 assert(((meth == NULL) || !meth->is_static()),
mgronlun@34666 1378 "find_instance_method should have skipped statics");
acorn@21913 1379 return meth;
acorn@21913 1380 }
acorn@21913 1381
drchase@28514 1382 // find_instance_method looks up the name/signature in the local methods array
drchase@28514 1383 // and skips over static methods
mgronlun@34666 1384 Method* InstanceKlass::find_instance_method(const Symbol* name, const Symbol* signature) const {
mgronlun@34666 1385 return InstanceKlass::find_instance_method(methods(), name, signature);
drchase@28514 1386 }
drchase@28514 1387
acorn@32189 1388 // Find looks up the name/signature in the local methods array
acorn@32189 1389 // and filters on the overpass, static and private flags
acorn@32189 1390 // This returns the first one found
acorn@32189 1391 // note that the local methods array can have up to one overpass, one static
acorn@32189 1392 // and one instance (private or not) with the same name/signature
mgronlun@34666 1393 Method* InstanceKlass::find_local_method(const Symbol* name,
mgronlun@34666 1394 const Symbol* signature,
mgronlun@34666 1395 OverpassLookupMode overpass_mode,
mgronlun@34666 1396 StaticLookupMode static_mode,
mgronlun@34666 1397 PrivateLookupMode private_mode) const {
mgronlun@34666 1398 return InstanceKlass::find_method_impl(methods(),
mgronlun@34666 1399 name,
mgronlun@34666 1400 signature,
mgronlun@34666 1401 overpass_mode,
mgronlun@34666 1402 static_mode,
mgronlun@34666 1403 private_mode);
acorn@32189 1404 }
acorn@32189 1405
acorn@32189 1406 // Find looks up the name/signature in the local methods array
acorn@32189 1407 // and filters on the overpass, static and private flags
acorn@32189 1408 // This returns the first one found
acorn@32189 1409 // note that the local methods array can have up to one overpass, one static
acorn@32189 1410 // and one instance (private or not) with the same name/signature
mgronlun@34666 1411 Method* InstanceKlass::find_local_method(const Array<Method*>* methods,
mgronlun@34666 1412 const Symbol* name,
mgronlun@34666 1413 const Symbol* signature,
mgronlun@34666 1414 OverpassLookupMode overpass_mode,
mgronlun@34666 1415 StaticLookupMode static_mode,
mgronlun@34666 1416 PrivateLookupMode private_mode) {
mgronlun@34666 1417 return InstanceKlass::find_method_impl(methods,
mgronlun@34666 1418 name,
mgronlun@34666 1419 signature,
mgronlun@34666 1420 overpass_mode,
mgronlun@34666 1421 static_mode,
mgronlun@34666 1422 private_mode);
mgronlun@34666 1423 }
mgronlun@34666 1424
mgronlun@34666 1425 Method* InstanceKlass::find_method(const Array<Method*>* methods,
mgronlun@34666 1426 const Symbol* name,
mgronlun@34666 1427 const Symbol* signature) {
mgronlun@34666 1428 return InstanceKlass::find_method_impl(methods,
mgronlun@34666 1429 name,
mgronlun@34666 1430 signature,
mgronlun@34666 1431 find_overpass,
mgronlun@34666 1432 find_static,
mgronlun@34666 1433 find_private);
mgronlun@34666 1434 }
mgronlun@34666 1435
mgronlun@34666 1436 Method* InstanceKlass::find_method_impl(const Array<Method*>* methods,
mgronlun@34666 1437 const Symbol* name,
mgronlun@34666 1438 const Symbol* signature,
acorn@32189 1439 OverpassLookupMode overpass_mode,
acorn@32189 1440 StaticLookupMode static_mode,
acorn@32189 1441 PrivateLookupMode private_mode) {
acorn@32189 1442 int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode);
acorn@20391 1443 return hit >= 0 ? methods->at(hit): NULL;
acorn@20391 1444 }
acorn@20391 1445
mgronlun@34666 1446 // true if method matches signature and conforms to skipping_X conditions.
mgronlun@34666 1447 static bool method_matches(const Method* m,
mgronlun@34666 1448 const Symbol* signature,
mgronlun@34666 1449 bool skipping_overpass,
mgronlun@34666 1450 bool skipping_static,
mgronlun@34666 1451 bool skipping_private) {
mgronlun@34666 1452 return ((m->signature() == signature) &&
mgronlun@34666 1453 (!skipping_overpass || !m->is_overpass()) &&
mgronlun@34666 1454 (!skipping_static || !m->is_static()) &&
mgronlun@34666 1455 (!skipping_private || !m->is_private()));
drchase@28514 1456 }
drchase@28514 1457
acorn@20391 1458 // Used directly for default_methods to find the index into the
acorn@20391 1459 // default_vtable_indices, and indirectly by find_method
acorn@20391 1460 // find_method_index looks in the local methods array to return the index
lfoltan@23999 1461 // of the matching name/signature. If, overpass methods are being ignored,
lfoltan@23999 1462 // the search continues to find a potential non-overpass match. This capability
lfoltan@23999 1463 // is important during method resolution to prefer a static method, for example,
lfoltan@23999 1464 // over an overpass method.
acorn@32189 1465 // There is the possibility in any _method's array to have the same name/signature
acorn@32189 1466 // for a static method, an overpass method and a local instance method
acorn@32189 1467 // To correctly catch a given method, the search criteria may need
acorn@32189 1468 // to explicitly skip the other two. For local instance methods, it
acorn@32189 1469 // is often necessary to skip private methods
mgronlun@34666 1470 int InstanceKlass::find_method_index(const Array<Method*>* methods,
mgronlun@34666 1471 const Symbol* name,
mgronlun@34666 1472 const Symbol* signature,
mgronlun@34666 1473 OverpassLookupMode overpass_mode,
mgronlun@34666 1474 StaticLookupMode static_mode,
mgronlun@34666 1475 PrivateLookupMode private_mode) {
mgronlun@34666 1476 const bool skipping_overpass = (overpass_mode == skip_overpass);
mgronlun@34666 1477 const bool skipping_static = (static_mode == skip_static);
mgronlun@34666 1478 const bool skipping_private = (private_mode == skip_private);
mgronlun@34666 1479 const int hit = binary_search(methods, name);
kamg@14385 1480 if (hit != -1) {
mgronlun@34666 1481 const Method* const m = methods->at(hit);
drchase@28514 1482
kamg@14385 1483 // Do linear search to find matching signature. First, quick check
lfoltan@23999 1484 // for common case, ignoring overpasses if requested.
mgronlun@34666 1485 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
mgronlun@34666 1486 return hit;
mgronlun@34666 1487 }
lfoltan@23999 1488
kamg@14385 1489 // search downwards through overloaded methods
kamg@14385 1490 int i;
kamg@14385 1491 for (i = hit - 1; i >= 0; --i) {
mgronlun@34666 1492 const Method* const m = methods->at(i);
kamg@14385 1493 assert(m->is_method(), "must be method");
mgronlun@34666 1494 if (m->name() != name) {
mgronlun@34666 1495 break;
mgronlun@34666 1496 }
mgronlun@34666 1497 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
mgronlun@34666 1498 return i;
mgronlun@34666 1499 }
kamg@14385 1500 }
kamg@14385 1501 // search upwards
kamg@14385 1502 for (i = hit + 1; i < methods->length(); ++i) {
mgronlun@34666 1503 const Method* const m = methods->at(i);
kamg@14385 1504 assert(m->is_method(), "must be method");
mgronlun@34666 1505 if (m->name() != name) {
mgronlun@34666 1506 break;
mgronlun@34666 1507 }
mgronlun@34666 1508 if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) {
mgronlun@34666 1509 return i;
mgronlun@34666 1510 }
kamg@14385 1511 }
kamg@14385 1512 // not found
duke@1 1513 #ifdef ASSERT
mgronlun@34666 1514 const int index = (skipping_overpass || skipping_static || skipping_private) ? -1 :
mgronlun@34666 1515 linear_search(methods, name, signature);
mgronlun@34666 1516 assert(-1 == index, "binary search should have found entry %d", index);
duke@1 1517 #endif
kamg@14385 1518 }
acorn@20391 1519 return -1;
duke@1 1520 }
mgronlun@34666 1521
mgronlun@34666 1522 int InstanceKlass::find_method_by_name(const Symbol* name, int* end) const {
kamg@14385 1523 return find_method_by_name(methods(), name, end);
kamg@14385 1524 }
kamg@14385 1525
mgronlun@34666 1526 int InstanceKlass::find_method_by_name(const Array<Method*>* methods,
mgronlun@34666 1527 const Symbol* name,
mgronlun@34666 1528 int* end_ptr) {
kamg@14385 1529 assert(end_ptr != NULL, "just checking");
kamg@14385 1530 int start = binary_search(methods, name);
kamg@14385 1531 int end = start + 1;
kamg@14385 1532 if (start != -1) {
kamg@14385 1533 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
kamg@14385 1534 while (end < methods->length() && (methods->at(end))->name() == name) ++end;
kamg@14385 1535 *end_ptr = end;
kamg@14385 1536 return start;
kamg@14385 1537 }
kamg@14385 1538 return -1;
kamg@14385 1539 }
kamg@14385 1540
hseigel@22232 1541 // uncached_lookup_method searches both the local class methods array and all
hseigel@22232 1542 // superclasses methods arrays, skipping any overpass methods in superclasses.
mgronlun@34666 1543 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
mgronlun@34666 1544 const Symbol* signature,
mgronlun@34666 1545 OverpassLookupMode overpass_mode) const {
lfoltan@28731 1546 OverpassLookupMode overpass_local_mode = overpass_mode;
mgronlun@34666 1547 const Klass* klass = this;
duke@1 1548 while (klass != NULL) {
mgronlun@34666 1549 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
mgronlun@34666 1550 signature,
mgronlun@34666 1551 overpass_local_mode,
mgronlun@34666 1552 find_static,
mgronlun@34666 1553 find_private);
lfoltan@23999 1554 if (method != NULL) {
hseigel@22232 1555 return method;
hseigel@22232 1556 }
coleenp@33602 1557 klass = klass->super();
lfoltan@28731 1558 overpass_local_mode = skip_overpass; // Always ignore overpass methods in superclasses
duke@1 1559 }
duke@1 1560 return NULL;
duke@1 1561 }
duke@1 1562
dsamersoff@24828 1563 #ifdef ASSERT
dsamersoff@24828 1564 // search through class hierarchy and return true if this class or
dsamersoff@24828 1565 // one of the superclasses was redefined
mgronlun@34666 1566 bool InstanceKlass::has_redefined_this_or_super() const {
mgronlun@34666 1567 const Klass* klass = this;
dsamersoff@24828 1568 while (klass != NULL) {
coleenp@33602 1569 if (InstanceKlass::cast(klass)->has_been_redefined()) {
dsamersoff@24828 1570 return true;
dsamersoff@24828 1571 }
coleenp@33602 1572 klass = klass->super();
dsamersoff@24828 1573 }
dsamersoff@24828 1574 return false;
dsamersoff@24828 1575 }
dsamersoff@24828 1576 #endif
dsamersoff@24828 1577
acorn@20391 1578 // lookup a method in the default methods list then in all transitive interfaces
acorn@20391 1579 // Do NOT return private or static methods
acorn@20391 1580 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name,
acorn@20391 1581 Symbol* signature) const {
acorn@20396 1582 Method* m = NULL;
acorn@20391 1583 if (default_methods() != NULL) {
acorn@20391 1584 m = find_method(default_methods(), name, signature);
acorn@20391 1585 }
acorn@20391 1586 // Look up interfaces
acorn@20391 1587 if (m == NULL) {
lfoltan@28731 1588 m = lookup_method_in_all_interfaces(name, signature, find_defaults);
acorn@20391 1589 }
acorn@20391 1590 return m;
acorn@20391 1591 }
acorn@20391 1592
duke@1 1593 // lookup a method in all the interfaces that this class implements
acorn@20284 1594 // Do NOT return private or static methods, new in JDK8 which are not externally visible
acorn@20284 1595 // They should only be found in the initial InterfaceMethodRef
coleenp@13728 1596 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
hseigel@22232 1597 Symbol* signature,
lfoltan@28731 1598 DefaultsLookupMode defaults_mode) const {
coleenp@13728 1599 Array<Klass*>* all_ifs = transitive_interfaces();
duke@1 1600 int num_ifs = all_ifs->length();
coleenp@13728 1601 InstanceKlass *ik = NULL;
duke@1 1602 for (int i = 0; i < num_ifs; i++) {
coleenp@13728 1603 ik = InstanceKlass::cast(all_ifs->at(i));
coleenp@13728 1604 Method* m = ik->lookup_method(name, signature);
hseigel@22232 1605 if (m != NULL && m->is_public() && !m->is_static() &&
lfoltan@28731 1606 ((defaults_mode != skip_defaults) || !m->is_default_method())) {
duke@1 1607 return m;
duke@1 1608 }
duke@1 1609 }
duke@1 1610 return NULL;
duke@1 1611 }
duke@1 1612
duke@1 1613 /* jni_id_for_impl for jfieldIds only */
coleenp@23515 1614 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_k, int offset) {
duke@1 1615 MutexLocker ml(JfieldIdCreation_lock);
duke@1 1616 // Retry lookup after we got the lock
coleenp@23515 1617 JNIid* probe = this_k->jni_ids() == NULL ? NULL : this_k->jni_ids()->find(offset);
duke@1 1618 if (probe == NULL) {
duke@1 1619 // Slow case, allocate new static field identifier
coleenp@23515 1620 probe = new JNIid(this_k(), offset, this_k->jni_ids());
coleenp@23515 1621 this_k->set_jni_ids(probe);
duke@1 1622 }
duke@1 1623 return probe;
duke@1 1624 }
duke@1 1625
duke@1 1626
duke@1 1627 /* jni_id_for for jfieldIds only */
coleenp@13728 1628 JNIid* InstanceKlass::jni_id_for(int offset) {
duke@1 1629 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
duke@1 1630 if (probe == NULL) {
coleenp@13728 1631 probe = jni_id_for_impl(this, offset);
duke@1 1632 }
duke@1 1633 return probe;
duke@1 1634 }
duke@1 1635
mgronlun@34666 1636 u2 InstanceKlass::enclosing_method_data(int offset) const {
mgronlun@34666 1637 const Array<jushort>* const inner_class_list = inner_classes();
jiangli@12231 1638 if (inner_class_list == NULL) {
jiangli@12231 1639 return 0;
jiangli@12231 1640 }
mgronlun@34666 1641 const int length = inner_class_list->length();
jiangli@12231 1642 if (length % inner_class_next_offset == 0) {
jiangli@12231 1643 return 0;
jiangli@12231 1644 }
mgronlun@34666 1645 const int index = length - enclosing_method_attribute_size;
mgronlun@34666 1646 assert(offset < enclosing_method_attribute_size, "invalid offset");
mgronlun@34666 1647 return inner_class_list->at(index + offset);
jiangli@12231 1648 }
jiangli@12231 1649
coleenp@13728 1650 void InstanceKlass::set_enclosing_method_indices(u2 class_index,
jiangli@12231 1651 u2 method_index) {
coleenp@13728 1652 Array<jushort>* inner_class_list = inner_classes();
jiangli@12231 1653 assert (inner_class_list != NULL, "_inner_classes list is not set up");
jiangli@12231 1654 int length = inner_class_list->length();
jiangli@12231 1655 if (length % inner_class_next_offset == enclosing_method_attribute_size) {
jiangli@12231 1656 int index = length - enclosing_method_attribute_size;
coleenp@13728 1657 inner_class_list->at_put(
jiangli@12231 1658 index + enclosing_method_class_index_offset, class_index);
coleenp@13728 1659 inner_class_list->at_put(
jiangli@12231 1660 index + enclosing_method_method_index_offset, method_index);
jiangli@12231 1661 }
jiangli@12231 1662 }
duke@1 1663
duke@1 1664 // Lookup or create a jmethodID.
dcubed@3824 1665 // This code is called by the VMThread and JavaThreads so the
dcubed@3824 1666 // locking has to be done very carefully to avoid deadlocks
dcubed@3824 1667 // and/or other cache consistency problems.
dcubed@3824 1668 //
coleenp@33593 1669 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, const methodHandle& method_h) {
duke@1 1670 size_t idnum = (size_t)method_h->method_idnum();
duke@1 1671 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
duke@1 1672 size_t length = 0;
duke@1 1673 jmethodID id = NULL;
dcubed@3824 1674
dcubed@3824 1675 // We use a double-check locking idiom here because this cache is
dcubed@3824 1676 // performance sensitive. In the normal system, this cache only
dcubed@3824 1677 // transitions from NULL to non-NULL which is safe because we use
dcubed@3824 1678 // release_set_methods_jmethod_ids() to advertise the new cache.
dcubed@3824 1679 // A partially constructed cache should never be seen by a racing
dcubed@3824 1680 // thread. We also use release_store_ptr() to save a new jmethodID
dcubed@3824 1681 // in the cache so a partially constructed jmethodID should never be
dcubed@3824 1682 // seen either. Cache reads of existing jmethodIDs proceed without a
dcubed@3824 1683 // lock, but cache writes of a new jmethodID requires uniqueness and
dcubed@3824 1684 // creation of the cache itself requires no leaks so a lock is
dcubed@3824 1685 // generally acquired in those two cases.
dcubed@3824 1686 //
dcubed@3824 1687 // If the RedefineClasses() API has been used, then this cache can
dcubed@3824 1688 // grow and we'll have transitions from non-NULL to bigger non-NULL.
dcubed@3824 1689 // Cache creation requires no leaks and we require safety between all
dcubed@3824 1690 // cache accesses and freeing of the old cache so a lock is generally
dcubed@3824 1691 // acquired when the RedefineClasses() API has been used.
duke@1 1692
dcubed@3824 1693 if (jmeths != NULL) {
dcubed@3824 1694 // the cache already exists
dcubed@3824 1695 if (!ik_h->idnum_can_increment()) {
dcubed@3824 1696 // the cache can't grow so we can just get the current values
dcubed@3824 1697 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1698 } else {
dcubed@3824 1699 // cache can grow so we have to be more careful
dcubed@3824 1700 if (Threads::number_of_threads() == 0 ||
dcubed@3824 1701 SafepointSynchronize::is_at_safepoint()) {
dcubed@3824 1702 // we're single threaded or at a safepoint - no locking needed
dcubed@3824 1703 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1704 } else {
dcubed@3824 1705 MutexLocker ml(JmethodIdCreation_lock);
dcubed@3824 1706 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@3824 1707 }
dcubed@3824 1708 }
dcubed@3824 1709 }
dcubed@3824 1710 // implied else:
dcubed@3824 1711 // we need to allocate a cache so default length and id values are good
duke@1 1712
dcubed@3824 1713 if (jmeths == NULL || // no cache yet
dcubed@3824 1714 length <= idnum || // cache is too short
dcubed@3824 1715 id == NULL) { // cache doesn't contain entry
dcubed@3824 1716
dcubed@3824 1717 // This function can be called by the VMThread so we have to do all
dcubed@3824 1718 // things that might block on a safepoint before grabbing the lock.
dcubed@3824 1719 // Otherwise, we can deadlock with the VMThread or have a cache
dcubed@3824 1720 // consistency issue. These vars keep track of what we might have
dcubed@3824 1721 // to free after the lock is dropped.
dcubed@3824 1722 jmethodID to_dealloc_id = NULL;
dcubed@3824 1723 jmethodID* to_dealloc_jmeths = NULL;
dcubed@3824 1724
dcubed@3824 1725 // may not allocate new_jmeths or use it if we allocate it
duke@1 1726 jmethodID* new_jmeths = NULL;
duke@1 1727 if (length <= idnum) {
dcubed@3824 1728 // allocate a new cache that might be used
duke@1 1729 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count());
zgu@13195 1730 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
duke@1 1731 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
dcubed@3824 1732 // cache size is stored in element[0], other elements offset by one
dcubed@3824 1733 new_jmeths[0] = (jmethodID)size;
duke@1 1734 }
duke@1 1735
dcubed@3824 1736 // allocate a new jmethodID that might be used
duke@1 1737 jmethodID new_id = NULL;
duke@1 1738 if (method_h->is_old() && !method_h->is_obsolete()) {
duke@1 1739 // The method passed in is old (but not obsolete), we need to use the current version
coleenp@13728 1740 Method* current_method = ik_h->method_with_idnum((int)idnum);
duke@1 1741 assert(current_method != NULL, "old and but not obsolete, so should exist");
coleenp@13728 1742 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method);
duke@1 1743 } else {
duke@1 1744 // It is the current version of the method or an obsolete method,
duke@1 1745 // use the version passed in
coleenp@13728 1746 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h());
duke@1 1747 }
duke@1 1748
dcubed@3824 1749 if (Threads::number_of_threads() == 0 ||
dcubed@3824 1750 SafepointSynchronize::is_at_safepoint()) {
dcubed@3824 1751 // we're single threaded or at a safepoint - no locking needed
dcubed@3824 1752 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
dcubed@3824 1753 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@222 1754 } else {
duke@1 1755 MutexLocker ml(JmethodIdCreation_lock);
dcubed@3824 1756 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
dcubed@3824 1757 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@3824 1758 }
dcubed@3824 1759
dcubed@3824 1760 // The lock has been dropped so we can free resources.
dcubed@3824 1761 // Free up either the old cache or the new cache if we allocated one.
dcubed@3824 1762 if (to_dealloc_jmeths != NULL) {
dcubed@3824 1763 FreeHeap(to_dealloc_jmeths);
dcubed@3824 1764 }
dcubed@3824 1765 // free up the new ID since it wasn't needed
dcubed@3824 1766 if (to_dealloc_id != NULL) {
coleenp@13728 1767 Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id);
dcubed@222 1768 }
dcubed@222 1769 }
dcubed@222 1770 return id;
dcubed@222 1771 }
duke@1 1772
jmanson@27478 1773 // Figure out how many jmethodIDs haven't been allocated, and make
jmanson@27478 1774 // sure space for them is pre-allocated. This makes getting all
jmanson@27478 1775 // method ids much, much faster with classes with more than 8
jmanson@27478 1776 // methods, and has a *substantial* effect on performance with jvmti
jmanson@27478 1777 // code that loads all jmethodIDs for all classes.
jmanson@27478 1778 void InstanceKlass::ensure_space_for_methodids(int start_offset) {
jmanson@27478 1779 int new_jmeths = 0;
jmanson@27478 1780 int length = methods()->length();
jmanson@27478 1781 for (int index = start_offset; index < length; index++) {
jmanson@27478 1782 Method* m = methods()->at(index);
jmanson@27478 1783 jmethodID id = m->find_jmethod_id_or_null();
jmanson@27478 1784 if (id == NULL) {
jmanson@27478 1785 new_jmeths++;
jmanson@27478 1786 }
jmanson@27478 1787 }
jmanson@27478 1788 if (new_jmeths != 0) {
jmanson@27478 1789 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
jmanson@27478 1790 }
jmanson@27478 1791 }
dcubed@222 1792
dcubed@3824 1793 // Common code to fetch the jmethodID from the cache or update the
dcubed@3824 1794 // cache with the new jmethodID. This function should never do anything
dcubed@3824 1795 // that causes the caller to go to a safepoint or we can deadlock with
dcubed@3824 1796 // the VMThread or have cache consistency issues.
dcubed@3824 1797 //
coleenp@13728 1798 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
dcubed@3824 1799 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
dcubed@3824 1800 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
dcubed@3824 1801 jmethodID** to_dealloc_jmeths_p) {
dcubed@3824 1802 assert(new_id != NULL, "sanity check");
dcubed@3824 1803 assert(to_dealloc_id_p != NULL, "sanity check");
dcubed@3824 1804 assert(to_dealloc_jmeths_p != NULL, "sanity check");
dcubed@3824 1805 assert(Threads::number_of_threads() == 0 ||
dcubed@3824 1806 SafepointSynchronize::is_at_safepoint() ||
dcubed@3824 1807 JmethodIdCreation_lock->owned_by_self(), "sanity check");
dcubed@3824 1808
dcubed@3824 1809 // reacquire the cache - we are locked, single threaded or at a safepoint
dcubed@222 1810 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
dcubed@3824 1811 jmethodID id = NULL;
dcubed@3824 1812 size_t length = 0;
duke@1 1813
dcubed@3824 1814 if (jmeths == NULL || // no cache yet
dcubed@3824 1815 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short
dcubed@222 1816 if (jmeths != NULL) {
dcubed@3824 1817 // copy any existing entries from the old cache
dcubed@222 1818 for (size_t index = 0; index < length; index++) {
dcubed@222 1819 new_jmeths[index+1] = jmeths[index+1];
duke@1 1820 }
dcubed@3824 1821 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete
duke@1 1822 }
dcubed@222 1823 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths);
dcubed@222 1824 } else {
dcubed@3824 1825 // fetch jmethodID (if any) from the existing cache
dcubed@222 1826 id = jmeths[idnum+1];
dcubed@3824 1827 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete
dcubed@222 1828 }
dcubed@222 1829 if (id == NULL) {
dcubed@3824 1830 // No matching jmethodID in the existing cache or we have a new
dcubed@3824 1831 // cache or we just grew the cache. This cache write is done here
dcubed@3824 1832 // by the first thread to win the foot race because a jmethodID
dcubed@3824 1833 // needs to be unique once it is generally available.
dcubed@222 1834 id = new_id;
dcubed@3824 1835
dcubed@3824 1836 // The jmethodID cache can be read while unlocked so we have to
dcubed@3824 1837 // make sure the new jmethodID is complete before installing it
dcubed@3824 1838 // in the cache.
dcubed@3824 1839 OrderAccess::release_store_ptr(&jmeths[idnum+1], id);
dcubed@222 1840 } else {
dcubed@3824 1841 *to_dealloc_id_p = new_id; // save new id for later delete
duke@1 1842 }
duke@1 1843 return id;
duke@1 1844 }
duke@1 1845
duke@1 1846
dcubed@3824 1847 // Common code to get the jmethodID cache length and the jmethodID
dcubed@3824 1848 // value at index idnum if there is one.
dcubed@3824 1849 //
coleenp@13728 1850 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache,
dcubed@3824 1851 size_t idnum, size_t *length_p, jmethodID* id_p) {
dcubed@3824 1852 assert(cache != NULL, "sanity check");
dcubed@3824 1853 assert(length_p != NULL, "sanity check");
dcubed@3824 1854 assert(id_p != NULL, "sanity check");
dcubed@3824 1855
dcubed@3824 1856 // cache size is stored in element[0], other elements offset by one
dcubed@3824 1857 *length_p = (size_t)cache[0];
dcubed@3824 1858 if (*length_p <= idnum) { // cache is too short
dcubed@3824 1859 *id_p = NULL;
dcubed@3824 1860 } else {
dcubed@3824 1861 *id_p = cache[idnum+1]; // fetch jmethodID (if any)
dcubed@3824 1862 }
dcubed@3824 1863 }
dcubed@3824 1864
dcubed@3824 1865
duke@1 1866 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
coleenp@13728 1867 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
duke@1 1868 size_t idnum = (size_t)method->method_idnum();
duke@1 1869 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@1 1870 size_t length; // length assigned as debugging crumb
duke@1 1871 jmethodID id = NULL;
dcubed@3824 1872 if (jmeths != NULL && // If there is a cache
duke@1 1873 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
duke@1 1874 id = jmeths[idnum+1]; // Look up the id (may be NULL)
duke@1 1875 }
duke@1 1876 return id;
duke@1 1877 }
duke@1 1878
vlivanov@34195 1879 inline DependencyContext InstanceKlass::dependencies() {
vlivanov@34195 1880 DependencyContext dep_context(&_dep_context);
vlivanov@34195 1881 return dep_context;
vlivanov@31037 1882 }
vlivanov@31037 1883
vlivanov@31037 1884 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
vlivanov@34195 1885 return dependencies().mark_dependent_nmethods(changes);
stefank@25492 1886 }
duke@1 1887
coleenp@13728 1888 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
vlivanov@34195 1889 dependencies().add_dependent_nmethod(nm);
duke@1 1890 }
duke@1 1891
stefank@33576 1892 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
vlivanov@34195 1893 dependencies().remove_dependent_nmethod(nm, delete_immediately);
duke@1 1894 }
duke@1 1895
duke@1 1896 #ifndef PRODUCT
coleenp@13728 1897 void InstanceKlass::print_dependent_nmethods(bool verbose) {
vlivanov@34195 1898 dependencies().print_dependent_nmethods(verbose);
duke@1 1899 }
duke@1 1900
coleenp@13728 1901 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
vlivanov@34195 1902 return dependencies().is_dependent_nmethod(nm);
duke@1 1903 }
duke@1 1904 #endif //PRODUCT
duke@1 1905
stefank@33576 1906 void InstanceKlass::clean_weak_instanceklass_links(BoolObjectClosure* is_alive) {
stefank@33576 1907 clean_implementors_list(is_alive);
stefank@33576 1908 clean_method_data(is_alive);
stefank@33576 1909
vlivanov@34195 1910 // Since GC iterates InstanceKlasses sequentially, it is safe to remove stale entries here.
vlivanov@34195 1911 DependencyContext dep_context(&_dep_context);
vlivanov@34195 1912 dep_context.expunge_stale_entries();
stefank@33576 1913 }
stefank@33576 1914
coleenp@13728 1915 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
stefank@25492 1916 assert(class_loader_data()->is_alive(is_alive), "this klass should be live");
jiangli@12369 1917 if (is_interface()) {
jiangli@12369 1918 if (ClassUnloading) {
coleenp@13728 1919 Klass* impl = implementor();
jiangli@12369 1920 if (impl != NULL) {
coleenp@13728 1921 if (!impl->is_loader_alive(is_alive)) {
jiangli@12369 1922 // remove this guy
morris@15873 1923 Klass** klass = adr_implementor();
morris@15873 1924 assert(klass != NULL, "null klass");
morris@15873 1925 if (klass != NULL) {
morris@15873 1926 *klass = NULL;
morris@15873 1927 }
jiangli@12369 1928 }
duke@1 1929 }
coleenp@13728 1930 }
coleenp@13728 1931 }
coleenp@13728 1932 }
coleenp@13728 1933
coleenp@13728 1934 void InstanceKlass::clean_method_data(BoolObjectClosure* is_alive) {
coleenp@13728 1935 for (int m = 0; m < methods()->length(); m++) {
coleenp@13728 1936 MethodData* mdo = methods()->at(m)->method_data();
coleenp@13728 1937 if (mdo != NULL) {
roland@22916 1938 mdo->clean_method_data(is_alive);
duke@1 1939 }
duke@1 1940 }
coleenp@13728 1941 }
coleenp@13728 1942
coleenp@13728 1943
coleenp@13728 1944 static void remove_unshareable_in_class(Klass* k) {
coleenp@13728 1945 // remove klass's unshareable info
coleenp@13728 1946 k->remove_unshareable_info();
coleenp@13728 1947 }
coleenp@13728 1948
coleenp@13728 1949 void InstanceKlass::remove_unshareable_info() {
coleenp@13728 1950 Klass::remove_unshareable_info();
coleenp@13728 1951 // Unlink the class
coleenp@13728 1952 if (is_linked()) {
coleenp@13728 1953 unlink_class();
coleenp@13728 1954 }
coleenp@13728 1955 init_implementor();
coleenp@13728 1956
coleenp@13728 1957 constants()->remove_unshareable_info();
coleenp@13728 1958
vlivanov@34195 1959 assert(_dep_context == DependencyContext::EMPTY, "dependency context is not shareable");
vlivanov@34195 1960
coleenp@13728 1961 for (int i = 0; i < methods()->length(); i++) {
coleenp@13728 1962 Method* m = methods()->at(i);
coleenp@13728 1963 m->remove_unshareable_info();
coleenp@13728 1964 }
coleenp@13728 1965
coleenp@13728 1966 // do array classes also.
coleenp@13728 1967 array_klasses_do(remove_unshareable_in_class);
duke@1 1968 }
duke@1 1969
iklam@26135 1970 static void restore_unshareable_in_class(Klass* k, TRAPS) {
iklam@26135 1971 // Array classes have null protection domain.
iklam@26135 1972 // --> see ArrayKlass::complete_create_array_klass()
iklam@26135 1973 k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
duke@1 1974 }
duke@1 1975
iklam@26135 1976 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
iklam@26135 1977 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
coleenp@13728 1978 instanceKlassHandle ik(THREAD, this);
coleenp@13728 1979
coleenp@13728 1980 Array<Method*>* methods = ik->methods();
coleenp@13728 1981 int num_methods = methods->length();
coleenp@13728 1982 for (int index2 = 0; index2 < num_methods; ++index2) {
coleenp@13728 1983 methodHandle m(THREAD, methods->at(index2));
coleenp@23872 1984 m->restore_unshareable_info(CHECK);
coleenp@13728 1985 }
coleenp@13728 1986 if (JvmtiExport::has_redefined_a_class()) {
coleenp@13728 1987 // Reinitialize vtable because RedefineClasses may have changed some
coleenp@13728 1988 // entries in this vtable for super classes so the CDS vtable might
coleenp@13728 1989 // point to old or obsolete entries. RedefineClasses doesn't fix up
coleenp@13728 1990 // vtables in the shared system dictionary, only the main one.
coleenp@13728 1991 // It also redefines the itable too so fix that too.
coleenp@13728 1992 ResourceMark rm(THREAD);
coleenp@13728 1993 ik->vtable()->initialize_vtable(false, CHECK);
coleenp@13728 1994 ik->itable()->initialize_itable(false, CHECK);
coleenp@13728 1995 }
coleenp@13728 1996
coleenp@13728 1997 // restore constant pool resolved references
coleenp@13728 1998 ik->constants()->restore_unshareable_info(CHECK);
coleenp@13728 1999
coleenp@13728 2000 ik->array_klasses_do(restore_unshareable_in_class, CHECK);
coleenp@13728 2001 }
coleenp@13728 2002
iklam@26135 2003 // returns true IFF is_in_error_state() has been changed as a result of this call.
iklam@26135 2004 bool InstanceKlass::check_sharing_error_state() {
iklam@26135 2005 assert(DumpSharedSpaces, "should only be called during dumping");
iklam@26135 2006 bool old_state = is_in_error_state();
iklam@26135 2007
iklam@26135 2008 if (!is_in_error_state()) {
iklam@26135 2009 bool bad = false;
iklam@26135 2010 for (InstanceKlass* sup = java_super(); sup; sup = sup->java_super()) {
iklam@26135 2011 if (sup->is_in_error_state()) {
iklam@26135 2012 bad = true;
iklam@26135 2013 break;
iklam@26135 2014 }
iklam@26135 2015 }
iklam@26135 2016 if (!bad) {
iklam@26135 2017 Array<Klass*>* interfaces = transitive_interfaces();
iklam@26135 2018 for (int i = 0; i < interfaces->length(); i++) {
iklam@26135 2019 Klass* iface = interfaces->at(i);
iklam@26135 2020 if (InstanceKlass::cast(iface)->is_in_error_state()) {
iklam@26135 2021 bad = true;
iklam@26135 2022 break;
iklam@26135 2023 }
iklam@26135 2024 }
iklam@26135 2025 }
iklam@26135 2026
iklam@26135 2027 if (bad) {
iklam@26135 2028 set_in_error_state();
iklam@26135 2029 }
iklam@26135 2030 }
iklam@26135 2031
iklam@26135 2032 return (old_state != is_in_error_state());
iklam@26135 2033 }
iklam@26135 2034
coleenp@13728 2035 static void clear_all_breakpoints(Method* m) {
duke@1 2036 m->clear_all_breakpoints();
duke@1 2037 }
duke@1 2038
coleenp@17075 2039
coleenp@17075 2040 void InstanceKlass::notify_unload_class(InstanceKlass* ik) {
coleenp@17075 2041 // notify the debugger
coleenp@17075 2042 if (JvmtiExport::should_post_class_unload()) {
coleenp@17075 2043 JvmtiExport::post_class_unload(ik);
coleenp@17075 2044 }
coleenp@17075 2045
coleenp@17075 2046 // notify ClassLoadingService of class unload
coleenp@17075 2047 ClassLoadingService::notify_class_unloaded(ik);
coleenp@17075 2048 }
coleenp@17075 2049
coleenp@17075 2050 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
coleenp@17075 2051 // Clean up C heap
coleenp@17075 2052 ik->release_C_heap_structures();
coleenp@17075 2053 ik->constants()->release_C_heap_structures();
coleenp@17075 2054 }
coleenp@17075 2055
coleenp@13728 2056 void InstanceKlass::release_C_heap_structures() {
coleenp@17075 2057
coleenp@17075 2058 // Can't release the constant pool here because the constant pool can be
coleenp@17075 2059 // deallocated separately from the InstanceKlass for default methods and
coleenp@17075 2060 // redefine classes.
coleenp@17075 2061
duke@1 2062 // Deallocate oop map cache
duke@1 2063 if (_oop_map_cache != NULL) {
duke@1 2064 delete _oop_map_cache;
duke@1 2065 _oop_map_cache = NULL;
duke@1 2066 }
duke@1 2067
duke@1 2068 // Deallocate JNI identifiers for jfieldIDs
duke@1 2069 JNIid::deallocate(jni_ids());
duke@1 2070 set_jni_ids(NULL);
duke@1 2071
duke@1 2072 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@1 2073 if (jmeths != (jmethodID*)NULL) {
duke@1 2074 release_set_methods_jmethod_ids(NULL);
duke@1 2075 FreeHeap(jmeths);
duke@1 2076 }
duke@1 2077
sspitsyn@17830 2078 // Deallocate MemberNameTable
sspitsyn@17830 2079 {
sspitsyn@17830 2080 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock;
sspitsyn@17830 2081 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag);
sspitsyn@17830 2082 MemberNameTable* mnt = member_names();
sspitsyn@17830 2083 if (mnt != NULL) {
sspitsyn@17830 2084 delete mnt;
sspitsyn@17830 2085 set_member_names(NULL);
sspitsyn@17830 2086 }
sspitsyn@17029 2087 }
sspitsyn@17029 2088
vlivanov@34221 2089 // Release dependencies.
vlivanov@34221 2090 // It is desirable to use DC::remove_all_dependents() here, but, unfortunately,
vlivanov@34221 2091 // it is not safe (see JDK-8143408). The problem is that the klass dependency
vlivanov@34221 2092 // context can contain live dependencies, since there's a race between nmethod &
vlivanov@34221 2093 // klass unloading. If the klass is dead when nmethod unloading happens, relevant
vlivanov@34221 2094 // dependencies aren't removed from the context associated with the class (see
vlivanov@34221 2095 // nmethod::flush_dependencies). It ends up during klass unloading as seemingly
vlivanov@34221 2096 // live dependencies pointing to unloaded nmethods and causes a crash in
vlivanov@34221 2097 // DC::remove_all_dependents() when it touches unloaded nmethod.
vlivanov@34221 2098 dependencies().wipe();
duke@1 2099
duke@1 2100 // Deallocate breakpoint records
duke@1 2101 if (breakpoints() != 0x0) {
duke@1 2102 methods_do(clear_all_breakpoints);
duke@1 2103 assert(breakpoints() == 0x0, "should have cleared breakpoints");
duke@1 2104 }
duke@1 2105
duke@1 2106 // deallocate the cached class file
jiangli@18940 2107 if (_cached_class_file != NULL) {
coleenp@27880 2108 os::free(_cached_class_file);
jiangli@18940 2109 _cached_class_file = NULL;
duke@1 2110 }
coleenp@8076 2111
coleenp@8076 2112 // Decrement symbol reference counts associated with the unloaded class.
coleenp@8076 2113 if (_name != NULL) _name->decrement_refcount();
coleenp@8076 2114 // unreference array name derived from this class name (arrays of an unloaded
coleenp@8076 2115 // class can't be referenced anymore).
coleenp@8076 2116 if (_array_name != NULL) _array_name->decrement_refcount();
coleenp@27880 2117 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
zgu@15104 2118
zgu@15104 2119 assert(_total_instanceKlass_count >= 1, "Sanity check");
zgu@15104 2120 Atomic::dec(&_total_instanceKlass_count);
coleenp@8076 2121 }
coleenp@8076 2122
mgronlun@34666 2123 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
fparain@13201 2124 if (array == NULL) {
fparain@13201 2125 _source_debug_extension = NULL;
fparain@13201 2126 } else {
fparain@13201 2127 // Adding one to the attribute length in order to store a null terminator
fparain@13201 2128 // character could cause an overflow because the attribute length is
fparain@13201 2129 // already coded with an u4 in the classfile, but in practice, it's
fparain@13201 2130 // unlikely to happen.
fparain@13201 2131 assert((length+1) > length, "Overflow checking");
fparain@13201 2132 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
fparain@13201 2133 for (int i = 0; i < length; i++) {
fparain@13201 2134 sde[i] = array[i];
fparain@13201 2135 }
fparain@13201 2136 sde[length] = '\0';
fparain@13201 2137 _source_debug_extension = sde;
fparain@13201 2138 }
duke@1 2139 }
duke@1 2140
coleenp@13728 2141 address InstanceKlass::static_field_addr(int offset) {
hseigel@20282 2142 return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(java_mirror()));
never@8725 2143 }
never@8725 2144
never@8725 2145
coleenp@13728 2146 const char* InstanceKlass::signature_name() const {
farvidsson@21183 2147 int hash_len = 0;
farvidsson@21183 2148 char hash_buf[40];
farvidsson@21183 2149
farvidsson@21183 2150 // If this is an anonymous class, append a hash to make the name unique
farvidsson@21183 2151 if (is_anonymous()) {
farvidsson@21183 2152 intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
goetz@27471 2153 jio_snprintf(hash_buf, sizeof(hash_buf), "/" UINTX_FORMAT, (uintx)hash);
farvidsson@21183 2154 hash_len = (int)strlen(hash_buf);
farvidsson@21183 2155 }
farvidsson@21183 2156
farvidsson@21183 2157 // Get the internal name as a c string
duke@1 2158 const char* src = (const char*) (name()->as_C_string());
duke@1 2159 const int src_length = (int)strlen(src);
farvidsson@21183 2160
farvidsson@21183 2161 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
farvidsson@21183 2162
farvidsson@21183 2163 // Add L as type indicator
duke@1 2164 int dest_index = 0;
duke@1 2165 dest[dest_index++] = 'L';
farvidsson@21183 2166
farvidsson@21183 2167 // Add the actual class name
farvidsson@21183 2168 for (int src_index = 0; src_index < src_length; ) {
duke@1 2169 dest[dest_index++] = src[src_index++];
duke@1 2170 }
farvidsson@21183 2171
farvidsson@21183 2172 // If we have a hash, append it
farvidsson@21183 2173 for (int hash_index = 0; hash_index < hash_len; ) {
farvidsson@21183 2174 dest[dest_index++] = hash_buf[hash_index++];
farvidsson@21183 2175 }
farvidsson@21183 2176
farvidsson@21183 2177 // Add the semicolon and the NULL
duke@1 2178 dest[dest_index++] = ';';
duke@1 2179 dest[dest_index] = '\0';
duke@1 2180 return dest;
duke@1 2181 }
duke@1 2182
duke@1 2183 // different verisons of is_same_class_package
mgronlun@34666 2184 bool InstanceKlass::is_same_class_package(const Klass* class2) const {
mgronlun@34666 2185 const Klass* const class1 = (const Klass* const)this;
mgronlun@34666 2186 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
mgronlun@34666 2187 const Symbol* const classname1 = class1->name();
mgronlun@34666 2188
coleenp@33611 2189 if (class2->is_objArray_klass()) {
coleenp@13952 2190 class2 = ObjArrayKlass::cast(class2)->bottom_klass();
duke@1 2191 }
mgronlun@34666 2192 oop classloader2;
mgronlun@34666 2193 if (class2->is_instance_klass()) {
mgronlun@34666 2194 classloader2 = InstanceKlass::cast(class2)->class_loader();
mgronlun@34666 2195 } else {
mgronlun@34666 2196 assert(class2->is_typeArray_klass(), "should be type array");
mgronlun@34666 2197 classloader2 = NULL;
mgronlun@34666 2198 }
mgronlun@34666 2199 const Symbol* classname2 = class2->name();
mgronlun@34666 2200
mgronlun@34666 2201 return InstanceKlass::is_same_class_package(classloader1, classname1,
duke@1 2202 classloader2, classname2);
duke@1 2203 }
duke@1 2204
mgronlun@34666 2205 bool InstanceKlass::is_same_class_package(oop other_class_loader,
mgronlun@34666 2206 const Symbol* other_class_name) const {
mgronlun@34666 2207 oop this_class_loader = class_loader();
mgronlun@34666 2208 const Symbol* const this_class_name = name();
mgronlun@34666 2209
mgronlun@34666 2210 return InstanceKlass::is_same_class_package(this_class_loader,
mgronlun@34666 2211 this_class_name,
mgronlun@34666 2212 other_class_loader,
mgronlun@34666 2213 other_class_name);
duke@1 2214 }
duke@1 2215
duke@1 2216 // return true if two classes are in the same package, classloader
duke@1 2217 // and classname information is enough to determine a class's package
mgronlun@34666 2218 bool InstanceKlass::is_same_class_package(oop class_loader1, const Symbol* class_name1,
mgronlun@34666 2219 oop class_loader2, const Symbol* class_name2) {
duke@1 2220 if (class_loader1 != class_loader2) {
duke@1 2221 return false;
jrose@2332 2222 } else if (class_name1 == class_name2) {
jrose@2332 2223 return true; // skip painful bytewise comparison
duke@1 2224 } else {
duke@1 2225 ResourceMark rm;
duke@1 2226
coleenp@8076 2227 // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
duke@1 2228 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
duke@1 2229 // Otherwise, we just compare jbyte values between the strings.
coleenp@8076 2230 const jbyte *name1 = class_name1->base();
coleenp@8076 2231 const jbyte *name2 = class_name2->base();
coleenp@8076 2232
coleenp@8076 2233 const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/');
coleenp@8076 2234 const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/');
duke@1 2235
duke@1 2236 if ((last_slash1 == NULL) || (last_slash2 == NULL)) {
duke@1 2237 // One of the two doesn't have a package. Only return true
duke@1 2238 // if the other one also doesn't have a package.
duke@1 2239 return last_slash1 == last_slash2;
duke@1 2240 } else {
duke@1 2241 // Skip over '['s
duke@1 2242 if (*name1 == '[') {
duke@1 2243 do {
duke@1 2244 name1++;
duke@1 2245 } while (*name1 == '[');
duke@1 2246 if (*name1 != 'L') {
duke@1 2247 // Something is terribly wrong. Shouldn't be here.
duke@1 2248 return false;
duke@1 2249 }
duke@1 2250 }
duke@1 2251 if (*name2 == '[') {
duke@1 2252 do {
duke@1 2253 name2++;
duke@1 2254 } while (*name2 == '[');
duke@1 2255 if (*name2 != 'L') {
duke@1 2256 // Something is terribly wrong. Shouldn't be here.
duke@1 2257 return false;
duke@1 2258 }
duke@1 2259 }
duke@1 2260
duke@1 2261 // Check that package part is identical
duke@1 2262 int length1 = last_slash1 - name1;
duke@1 2263 int length2 = last_slash2 - name2;
duke@1 2264
duke@1 2265 return UTF8::equal(name1, length1, name2, length2);
duke@1 2266 }
duke@1 2267 }
duke@1 2268 }
duke@1 2269
acorn@2264 2270 // Returns true iff super_method can be overridden by a method in targetclassname
acorn@2264 2271 // See JSL 3rd edition 8.4.6.1
acorn@2264 2272 // Assumes name-signature match
coleenp@13728 2273 // "this" is InstanceKlass of super_method which must exist
coleenp@13728 2274 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
coleenp@33593 2275 bool InstanceKlass::is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
acorn@2264 2276 // Private methods can not be overridden
acorn@2264 2277 if (super_method->is_private()) {
acorn@2264 2278 return false;
acorn@2264 2279 }
acorn@2264 2280 // If super method is accessible, then override
acorn@2264 2281 if ((super_method->is_protected()) ||
acorn@2264 2282 (super_method->is_public())) {
acorn@2264 2283 return true;
acorn@2264 2284 }
acorn@2264 2285 // Package-private methods are not inherited outside of package
acorn@2264 2286 assert(super_method->is_package_private(), "must be package private");
coleenp@8076 2287 return(is_same_class_package(targetclassloader(), targetclassname));
acorn@2264 2288 }
duke@1 2289
jrose@2332 2290 /* defined for now in jvm.cpp, for historical reasons *--
coleenp@13728 2291 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
coleenp@8076 2292 Symbol*& simple_name_result, TRAPS) {
jrose@2332 2293 ...
jrose@2332 2294 }
jrose@2332 2295 */
jrose@2332 2296
jrose@2332 2297 // tell if two classes have the same enclosing class (at package level)
mgronlun@34666 2298 bool InstanceKlass::is_same_package_member_impl(const InstanceKlass* class1,
mgronlun@34666 2299 const Klass* class2,
mgronlun@34666 2300 TRAPS) {
mgronlun@34666 2301 if (class2 == class1) return true;
mgronlun@34666 2302 if (!class2->is_instance_klass()) return false;
jrose@2332 2303
jrose@2332 2304 // must be in same package before we try anything else
jrose@2332 2305 if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
jrose@2332 2306 return false;
jrose@2332 2307
jrose@2332 2308 // As long as there is an outer1.getEnclosingClass,
jrose@2332 2309 // shift the search outward.
mgronlun@34666 2310 const InstanceKlass* outer1 = class1;
jrose@2332 2311 for (;;) {
jrose@2332 2312 // As we walk along, look for equalities between outer1 and class2.
jrose@2332 2313 // Eventually, the walks will terminate as outer1 stops
jrose@2332 2314 // at the top-level class around the original class.
xlu@4496 2315 bool ignore_inner_is_member;
mgronlun@34666 2316 const Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member,
mgronlun@34666 2317 CHECK_false);
jrose@2332 2318 if (next == NULL) break;
mgronlun@34666 2319 if (next == class2) return true;
mgronlun@34666 2320 outer1 = InstanceKlass::cast(next);
jrose@2332 2321 }
jrose@2332 2322
jrose@2332 2323 // Now do the same for class2.
mgronlun@34666 2324 const InstanceKlass* outer2 = InstanceKlass::cast(class2);
jrose@2332 2325 for (;;) {
xlu@4496 2326 bool ignore_inner_is_member;
coleenp@13728 2327 Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
xlu@4496 2328 CHECK_false);
jrose@2332 2329 if (next == NULL) break;
jrose@2332 2330 // Might as well check the new outer against all available values.
mgronlun@34666 2331 if (next == class1) return true;
mgronlun@34666 2332 if (next == outer1) return true;
mgronlun@34666 2333 outer2 = InstanceKlass::cast(next);
jrose@2332 2334 }
jrose@2332 2335
jrose@2332 2336 // If by this point we have not found an equality between the
jrose@2332 2337 // two classes, we know they are in separate package members.
jrose@2332 2338 return false;
jrose@2332 2339 }
jrose@2332 2340
vlivanov@30222 2341 bool InstanceKlass::find_inner_classes_attr(instanceKlassHandle k, int* ooff, int* noff, TRAPS) {
vlivanov@30222 2342 constantPoolHandle i_cp(THREAD, k->constants());
vlivanov@30222 2343 for (InnerClassesIterator iter(k); !iter.done(); iter.next()) {
vlivanov@30222 2344 int ioff = iter.inner_class_info_index();
vlivanov@30222 2345 if (ioff != 0) {
vlivanov@30222 2346 // Check to see if the name matches the class we're looking for
vlivanov@30222 2347 // before attempting to find the class.
vlivanov@30222 2348 if (i_cp->klass_name_at_matches(k, ioff)) {
vlivanov@30222 2349 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
vlivanov@30222 2350 if (k() == inner_klass) {
vlivanov@30222 2351 *ooff = iter.outer_class_info_index();
vlivanov@30222 2352 *noff = iter.inner_name_index();
vlivanov@30222 2353 return true;
vlivanov@30222 2354 }
vlivanov@30222 2355 }
vlivanov@30222 2356 }
vlivanov@30222 2357 }
vlivanov@30222 2358 return false;
vlivanov@30222 2359 }
vlivanov@30222 2360
mgronlun@34666 2361 InstanceKlass* InstanceKlass::compute_enclosing_class_impl(const InstanceKlass* k,
mgronlun@34666 2362 bool* inner_is_member,
mgronlun@34666 2363 TRAPS) {
mgronlun@34666 2364 InstanceKlass* outer_klass = NULL;
vlivanov@30222 2365 *inner_is_member = false;
vlivanov@30222 2366 int ooff = 0, noff = 0;
vlivanov@30222 2367 if (find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
vlivanov@30222 2368 constantPoolHandle i_cp(THREAD, k->constants());
vlivanov@30222 2369 if (ooff != 0) {
vlivanov@30222 2370 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
mgronlun@34666 2371 outer_klass = InstanceKlass::cast(ok);
vlivanov@30222 2372 *inner_is_member = true;
vlivanov@30222 2373 }
mgronlun@34666 2374 if (NULL == outer_klass) {
vlivanov@30222 2375 // It may be anonymous; try for that.
vlivanov@30222 2376 int encl_method_class_idx = k->enclosing_method_class_index();
vlivanov@30222 2377 if (encl_method_class_idx != 0) {
vlivanov@30222 2378 Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
mgronlun@34666 2379 outer_klass = InstanceKlass::cast(ok);
vlivanov@30222 2380 *inner_is_member = false;
vlivanov@30222 2381 }
vlivanov@30222 2382 }
vlivanov@30222 2383 }
vlivanov@30222 2384
vlivanov@30222 2385 // If no inner class attribute found for this class.
mgronlun@34666 2386 if (NULL == outer_klass) return NULL;
vlivanov@30222 2387
vlivanov@30222 2388 // Throws an exception if outer klass has not declared k as an inner klass
vlivanov@30222 2389 // We need evidence that each klass knows about the other, or else
vlivanov@30222 2390 // the system could allow a spoof of an inner class to gain access rights.
vlivanov@30222 2391 Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL);
mgronlun@34666 2392 return outer_klass;
vlivanov@30222 2393 }
duke@1 2394
coleenp@13728 2395 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
duke@1 2396 jint access = access_flags().as_int();
duke@1 2397
duke@1 2398 // But check if it happens to be member class.
coleenp@13728 2399 instanceKlassHandle ik(THREAD, this);
jiangli@12231 2400 InnerClassesIterator iter(ik);
jiangli@12231 2401 for (; !iter.done(); iter.next()) {
jiangli@12231 2402 int ioff = iter.inner_class_info_index();
jiangli@12231 2403 // Inner class attribute can be zero, skip it.
jiangli@12231 2404 // Strange but true: JVM spec. allows null inner class refs.
jiangli@12231 2405 if (ioff == 0) continue;
jiangli@12231 2406
jiangli@12231 2407 // only look at classes that are already loaded
jiangli@12231 2408 // since we are looking for the flags for our self.
jiangli@12231 2409 Symbol* inner_name = ik->constants()->klass_name_at(ioff);
jiangli@12231 2410 if ((ik->name() == inner_name)) {
jiangli@12231 2411 // This is really a member class.
jiangli@12231 2412 access = iter.inner_access_flags();
jiangli@12231 2413 break;
duke@1 2414 }
duke@1 2415 }
duke@1 2416 // Remember to strip ACC_SUPER bit
duke@1 2417 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
duke@1 2418 }
duke@1 2419
coleenp@13728 2420 jint InstanceKlass::jvmti_class_status() const {
duke@1 2421 jint result = 0;
duke@1 2422
duke@1 2423 if (is_linked()) {
duke@1 2424 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
duke@1 2425 }
duke@1 2426
duke@1 2427 if (is_initialized()) {
duke@1 2428 assert(is_linked(), "Class status is not consistent");
duke@1 2429 result |= JVMTI_CLASS_STATUS_INITIALIZED;
duke@1 2430 }
duke@1 2431 if (is_in_error_state()) {
duke@1 2432 result |= JVMTI_CLASS_STATUS_ERROR;
duke@1 2433 }
duke@1 2434 return result;
duke@1 2435 }
duke@1 2436
coleenp@13728 2437 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) {
duke@1 2438 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
duke@1 2439 int method_table_offset_in_words = ioe->offset()/wordSize;
duke@1 2440 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
duke@1 2441 / itableOffsetEntry::size();
duke@1 2442
duke@1 2443 for (int cnt = 0 ; ; cnt ++, ioe ++) {
twisti@2131 2444 // If the interface isn't implemented by the receiver class,
duke@1 2445 // the VM should throw IncompatibleClassChangeError.
duke@1 2446 if (cnt >= nof_interfaces) {
coleenp@13728 2447 THROW_NULL(vmSymbols::java_lang_IncompatibleClassChangeError());
duke@1 2448 }
duke@1 2449
coleenp@13728 2450 Klass* ik = ioe->interface_klass();
duke@1 2451 if (ik == holder) break;
duke@1 2452 }
duke@1 2453
coleenp@13728 2454 itableMethodEntry* ime = ioe->first_method_entry(this);
coleenp@13728 2455 Method* m = ime[index].method();
duke@1 2456 if (m == NULL) {
coleenp@13728 2457 THROW_NULL(vmSymbols::java_lang_AbstractMethodError());
duke@1 2458 }
duke@1 2459 return m;
duke@1 2460 }
duke@1 2461
acorn@20391 2462
acorn@20391 2463 #if INCLUDE_JVMTI
acorn@20391 2464 // update default_methods for redefineclasses for methods that are
acorn@20391 2465 // not yet in the vtable due to concurrent subclass define and superinterface
acorn@20391 2466 // redefinition
acorn@20391 2467 // Note: those in the vtable, should have been updated via adjust_method_entries
sspitsyn@29316 2468 void InstanceKlass::adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed) {
acorn@20391 2469 // search the default_methods for uses of either obsolete or EMCP methods
acorn@20391 2470 if (default_methods() != NULL) {
sspitsyn@29316 2471 for (int index = 0; index < default_methods()->length(); index ++) {
sspitsyn@29316 2472 Method* old_method = default_methods()->at(index);
sspitsyn@29316 2473 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
sspitsyn@29316 2474 continue; // skip uninteresting entries
sspitsyn@29316 2475 }
sspitsyn@29316 2476 assert(!old_method->is_deleted(), "default methods may not be deleted");
sspitsyn@29316 2477
sspitsyn@29316 2478 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
sspitsyn@29316 2479
sspitsyn@29316 2480 assert(new_method != NULL, "method_with_idnum() should not be NULL");
sspitsyn@29316 2481 assert(old_method != new_method, "sanity check");
sspitsyn@29316 2482
sspitsyn@29316 2483 default_methods()->at_put(index, new_method);
sspitsyn@29316 2484 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
sspitsyn@29316 2485 if (!(*trace_name_printed)) {
sspitsyn@29316 2486 // RC_TRACE_MESG macro has an embedded ResourceMark
sspitsyn@29316 2487 RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s",
sspitsyn@29316 2488 external_name(),
sspitsyn@29316 2489 old_method->method_holder()->external_name()));
sspitsyn@29316 2490 *trace_name_printed = true;
acorn@20391 2491 }
sspitsyn@29316 2492 RC_TRACE(0x00100000, ("default method update: %s(%s) ",
sspitsyn@29316 2493 new_method->name()->as_C_string(),
sspitsyn@29316 2494 new_method->signature()->as_C_string()));
acorn@20391 2495 }
acorn@20391 2496 }
acorn@20391 2497 }
acorn@20391 2498 }
acorn@20391 2499 #endif // INCLUDE_JVMTI
acorn@20391 2500
duke@1 2501 // On-stack replacement stuff
coleenp@13728 2502 void InstanceKlass::add_osr_nmethod(nmethod* n) {
duke@1 2503 // only one compilation can be active
thartmann@27409 2504 {
thartmann@27409 2505 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 2506 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@27409 2507 assert(n->is_osr_method(), "wrong kind of nmethod");
thartmann@27409 2508 n->set_osr_link(osr_nmethods_head());
thartmann@27409 2509 set_osr_nmethods_head(n);
thartmann@27409 2510 // Raise the highest osr level if necessary
thartmann@27409 2511 if (TieredCompilation) {
thartmann@27409 2512 Method* m = n->method();
thartmann@27409 2513 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
thartmann@27409 2514 }
iveresov@6453 2515 }
iveresov@6453 2516
iveresov@6453 2517 // Get rid of the osr methods for the same bci that have lower levels.
iveresov@6453 2518 if (TieredCompilation) {
iveresov@6453 2519 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
iveresov@6453 2520 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
iveresov@6453 2521 if (inv != NULL && inv->is_in_use()) {
iveresov@6453 2522 inv->make_not_entrant();
iveresov@6453 2523 }
iveresov@6453 2524 }
iveresov@6453 2525 }
duke@1 2526 }
duke@1 2527
duke@1 2528
coleenp@13728 2529 void InstanceKlass::remove_osr_nmethod(nmethod* n) {
duke@1 2530 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 2531 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
duke@1 2532 assert(n->is_osr_method(), "wrong kind of nmethod");
duke@1 2533 nmethod* last = NULL;
duke@1 2534 nmethod* cur = osr_nmethods_head();
iveresov@6453 2535 int max_level = CompLevel_none; // Find the max comp level excluding n
coleenp@13728 2536 Method* m = n->method();
duke@1 2537 // Search for match
duke@1 2538 while(cur != NULL && cur != n) {
neliasso@22920 2539 if (TieredCompilation && m == cur->method()) {
iveresov@6453 2540 // Find max level before n
iveresov@6453 2541 max_level = MAX2(max_level, cur->comp_level());
iveresov@6453 2542 }
duke@1 2543 last = cur;
jrose@3908 2544 cur = cur->osr_link();
duke@1 2545 }
iveresov@6453 2546 nmethod* next = NULL;
duke@1 2547 if (cur == n) {
iveresov@6453 2548 next = cur->osr_link();
duke@1 2549 if (last == NULL) {
duke@1 2550 // Remove first element
iveresov@6453 2551 set_osr_nmethods_head(next);
duke@1 2552 } else {
iveresov@6453 2553 last->set_osr_link(next);
duke@1 2554 }
duke@1 2555 }
jrose@3908 2556 n->set_osr_link(NULL);
iveresov@6453 2557 if (TieredCompilation) {
iveresov@6453 2558 cur = next;
iveresov@6453 2559 while (cur != NULL) {
iveresov@6453 2560 // Find max level after n
neliasso@22920 2561 if (m == cur->method()) {
neliasso@22920 2562 max_level = MAX2(max_level, cur->comp_level());
neliasso@22920 2563 }
iveresov@6453 2564 cur = cur->osr_link();
iveresov@6453 2565 }
iveresov@6453 2566 m->set_highest_osr_comp_level(max_level);
iveresov@6453 2567 }
duke@1 2568 }
duke@1 2569
thartmann@27434 2570 int InstanceKlass::mark_osr_nmethods(const Method* m) {
thartmann@27434 2571 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27434 2572 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@27434 2573 nmethod* osr = osr_nmethods_head();
thartmann@27434 2574 int found = 0;
thartmann@27434 2575 while (osr != NULL) {
thartmann@27434 2576 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
thartmann@27434 2577 if (osr->method() == m) {
thartmann@27434 2578 osr->mark_for_deoptimization();
thartmann@27434 2579 found++;
thartmann@27434 2580 }
thartmann@27434 2581 osr = osr->osr_link();
thartmann@27434 2582 }
thartmann@27434 2583 return found;
thartmann@27434 2584 }
thartmann@27434 2585
minqi@17370 2586 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
duke@1 2587 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@27409 2588 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
duke@1 2589 nmethod* osr = osr_nmethods_head();
iveresov@6453 2590 nmethod* best = NULL;
duke@1 2591 while (osr != NULL) {
duke@1 2592 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
iveresov@6453 2593 // There can be a time when a c1 osr method exists but we are waiting
iveresov@6453 2594 // for a c2 version. When c2 completes its osr nmethod we will trash
iveresov@6453 2595 // the c1 version and only be able to find the c2 version. However
iveresov@6453 2596 // while we overflow in the c1 code at back branches we don't want to
iveresov@6453 2597 // try and switch to the same code as we are already running
iveresov@6453 2598
duke@1 2599 if (osr->method() == m &&
duke@1 2600 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
iveresov@6453 2601 if (match_level) {
iveresov@6453 2602 if (osr->comp_level() == comp_level) {
iveresov@6453 2603 // Found a match - return it.
iveresov@6453 2604 return osr;
iveresov@6453 2605 }
iveresov@6453 2606 } else {
iveresov@6453 2607 if (best == NULL || (osr->comp_level() > best->comp_level())) {
iveresov@6453 2608 if (osr->comp_level() == CompLevel_highest_tier) {
iveresov@6453 2609 // Found the best possible - return it.
iveresov@6453 2610 return osr;
iveresov@6453 2611 }
iveresov@6453 2612 best = osr;
iveresov@6453 2613 }
iveresov@6453 2614 }
duke@1 2615 }
jrose@3908 2616 osr = osr->osr_link();
duke@1 2617 }
iveresov@6453 2618 if (best != NULL && best->comp_level() >= comp_level && match_level == false) {
iveresov@6453 2619 return best;
iveresov@6453 2620 }
duke@1 2621 return NULL;
duke@1 2622 }
duke@1 2623
coleenp@27674 2624 bool InstanceKlass::add_member_name(Handle mem_name) {
sspitsyn@17029 2625 jweak mem_name_wref = JNIHandles::make_weak_global(mem_name);
sspitsyn@17029 2626 MutexLocker ml(MemberNameTable_lock);
sspitsyn@17029 2627 DEBUG_ONLY(No_Safepoint_Verifier nsv);
sspitsyn@17029 2628
coleenp@27674 2629 // Check if method has been redefined while taking out MemberNameTable_lock, if so
coleenp@27674 2630 // return false. We cannot cache obsolete methods. They will crash when the function
coleenp@27674 2631 // is called!
coleenp@27674 2632 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name());
coleenp@27674 2633 if (method->is_obsolete()) {
coleenp@27674 2634 return false;
coleenp@27674 2635 } else if (method->is_old()) {
coleenp@27674 2636 // Replace method with redefined version
coleenp@27674 2637 java_lang_invoke_MemberName::set_vmtarget(mem_name(), method_with_idnum(method->method_idnum()));
coleenp@27674 2638 }
coleenp@27674 2639
sspitsyn@17029 2640 if (_member_names == NULL) {
sspitsyn@17828 2641 _member_names = new (ResourceObj::C_HEAP, mtClass) MemberNameTable(idnum_allocated_count());
sspitsyn@17029 2642 }
coleenp@27674 2643 _member_names->add_member_name(mem_name_wref);
coleenp@27674 2644 return true;
sspitsyn@17029 2645 }
sspitsyn@17029 2646
duke@1 2647 // -----------------------------------------------------------------------------------------------------
coleenp@13728 2648 // Printing
coleenp@13728 2649
duke@1 2650 #ifndef PRODUCT
duke@1 2651
jrose@2332 2652 #define BULLET " - "
jrose@2332 2653
coleenp@13728 2654 static const char* state_names[] = {
coleenp@13728 2655 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
coleenp@13728 2656 };
coleenp@13728 2657
drchase@20017 2658 static void print_vtable(intptr_t* start, int len, outputStream* st) {
drchase@20017 2659 for (int i = 0; i < len; i++) {
drchase@20017 2660 intptr_t e = start[i];
drchase@20017 2661 st->print("%d : " INTPTR_FORMAT, i, e);
drchase@20017 2662 if (e != 0 && ((Metadata*)e)->is_metaspace_object()) {
drchase@20017 2663 st->print(" ");
drchase@20017 2664 ((Metadata*)e)->print_value_on(st);
drchase@20017 2665 }
drchase@20017 2666 st->cr();
drchase@20017 2667 }
drchase@20017 2668 }
drchase@20017 2669
coleenp@13728 2670 void InstanceKlass::print_on(outputStream* st) const {
coleenp@13728 2671 assert(is_klass(), "must be klass");
coleenp@13728 2672 Klass::print_on(st);
coleenp@13728 2673
coleenp@13728 2674 st->print(BULLET"instance size: %d", size_helper()); st->cr();
coleenp@13728 2675 st->print(BULLET"klass size: %d", size()); st->cr();
coleenp@13728 2676 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
drchase@24424 2677 st->print(BULLET"state: "); st->print_cr("%s", state_names[_init_state]);
coleenp@13728 2678 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
coleenp@13728 2679 st->print(BULLET"super: "); super()->print_value_on_maybe_null(st); st->cr();
coleenp@13728 2680 st->print(BULLET"sub: ");
coleenp@13728 2681 Klass* sub = subklass();
coleenp@13728 2682 int n;
coleenp@13728 2683 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
coleenp@13728 2684 if (n < MaxSubklassPrintSize) {
coleenp@13728 2685 sub->print_value_on(st);
coleenp@13728 2686 st->print(" ");
coleenp@13728 2687 }
coleenp@13728 2688 }
david@33148 2689 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
coleenp@13728 2690 st->cr();
coleenp@13728 2691
coleenp@13728 2692 if (is_interface()) {
coleenp@13728 2693 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
coleenp@13728 2694 if (nof_implementors() == 1) {
coleenp@13728 2695 st->print_cr(BULLET"implementor: ");
coleenp@13728 2696 st->print(" ");
coleenp@13728 2697 implementor()->print_value_on(st);
coleenp@13728 2698 st->cr();
coleenp@13728 2699 }
coleenp@13728 2700 }
coleenp@13728 2701
coleenp@13728 2702 st->print(BULLET"arrays: "); array_klasses()->print_value_on_maybe_null(st); st->cr();
coleenp@13728 2703 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
drchase@20017 2704 if (Verbose || WizardMode) {
coleenp@13728 2705 Array<Method*>* method_array = methods();
acorn@20391 2706 for (int i = 0; i < method_array->length(); i++) {
coleenp@13728 2707 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
coleenp@13728 2708 }
coleenp@13728 2709 }
acorn@20391 2710 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
acorn@20391 2711 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
acorn@20391 2712 if (Verbose && default_methods() != NULL) {
acorn@20391 2713 Array<Method*>* method_array = default_methods();
acorn@20391 2714 for (int i = 0; i < method_array->length(); i++) {
acorn@20391 2715 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
acorn@20391 2716 }
acorn@20391 2717 }
acorn@20391 2718 if (default_vtable_indices() != NULL) {
acorn@20391 2719 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
acorn@20391 2720 }
coleenp@13728 2721 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
coleenp@13728 2722 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
coleenp@13728 2723 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
coleenp@13728 2724 if (class_loader_data() != NULL) {
coleenp@13728 2725 st->print(BULLET"class loader data: ");
coleenp@13728 2726 class_loader_data()->print_value_on(st);
coleenp@13728 2727 st->cr();
coleenp@13728 2728 }
coleenp@13728 2729 st->print(BULLET"host class: "); host_klass()->print_value_on_maybe_null(st); st->cr();
coleenp@13728 2730 if (source_file_name() != NULL) {
coleenp@13728 2731 st->print(BULLET"source file: ");
coleenp@13728 2732 source_file_name()->print_value_on(st);
coleenp@13728 2733 st->cr();
coleenp@13728 2734 }
coleenp@13728 2735 if (source_debug_extension() != NULL) {
coleenp@13728 2736 st->print(BULLET"source debug extension: ");
coleenp@13728 2737 st->print("%s", source_debug_extension());
coleenp@13728 2738 st->cr();
coleenp@13728 2739 }
coleenp@15601 2740 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
coleenp@15601 2741 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
coleenp@15601 2742 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
coleenp@15601 2743 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
coleenp@13728 2744 {
coleenp@20059 2745 bool have_pv = false;
coleenp@26558 2746 // previous versions are linked together through the InstanceKlass
coleenp@26558 2747 for (InstanceKlass* pv_node = _previous_versions;
coleenp@26558 2748 pv_node != NULL;
coleenp@26558 2749 pv_node = pv_node->previous_versions()) {
coleenp@20059 2750 if (!have_pv)
coleenp@20059 2751 st->print(BULLET"previous version: ");
coleenp@20059 2752 have_pv = true;
coleenp@26558 2753 pv_node->constants()->print_value_on(st);
coleenp@20059 2754 }
coleenp@20059 2755 if (have_pv) st->cr();
coleenp@26558 2756 }
coleenp@13728 2757
coleenp@13728 2758 if (generic_signature() != NULL) {
coleenp@13728 2759 st->print(BULLET"generic signature: ");
coleenp@13728 2760 generic_signature()->print_value_on(st);
coleenp@13728 2761 st->cr();
coleenp@13728 2762 }
coleenp@13728 2763 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
coleenp@13728 2764 st->print(BULLET"java mirror: "); java_mirror()->print_value_on(st); st->cr();
david@33148 2765 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
drchase@20017 2766 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
david@33148 2767 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
drchase@20017 2768 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
coleenp@13728 2769 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
coleenp@13728 2770 FieldPrinter print_static_field(st);
coleenp@13728 2771 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
coleenp@13728 2772 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
coleenp@13728 2773 FieldPrinter print_nonstatic_field(st);
coleenp@33602 2774 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
coleenp@33602 2775 ik->do_nonstatic_fields(&print_nonstatic_field);
coleenp@13728 2776
coleenp@13728 2777 st->print(BULLET"non-static oop maps: ");
coleenp@13728 2778 OopMapBlock* map = start_of_nonstatic_oop_maps();
coleenp@13728 2779 OopMapBlock* end_map = map + nonstatic_oop_map_count();
coleenp@13728 2780 while (map < end_map) {
coleenp@13728 2781 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
coleenp@13728 2782 map++;
coleenp@13728 2783 }
coleenp@13728 2784 st->cr();
coleenp@13728 2785 }
coleenp@13728 2786
coleenp@13728 2787 #endif //PRODUCT
coleenp@13728 2788
coleenp@13728 2789 void InstanceKlass::print_value_on(outputStream* st) const {
coleenp@13728 2790 assert(is_klass(), "must be klass");
drchase@20017 2791 if (Verbose || WizardMode) access_flags().print_on(st);
coleenp@13728 2792 name()->print_value_on(st);
coleenp@13728 2793 }
coleenp@13728 2794
coleenp@13728 2795 #ifndef PRODUCT
coleenp@13728 2796
duke@1 2797 void FieldPrinter::do_field(fieldDescriptor* fd) {
jrose@2332 2798 _st->print(BULLET);
never@8725 2799 if (_obj == NULL) {
duke@1 2800 fd->print_on(_st);
duke@1 2801 _st->cr();
duke@1 2802 } else {
duke@1 2803 fd->print_on_for(_st, _obj);
duke@1 2804 _st->cr();
duke@1 2805 }
duke@1 2806 }
duke@1 2807
duke@1 2808
coleenp@13728 2809 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
duke@1 2810 Klass::oop_print_on(obj, st);
duke@1 2811
coleenp@13728 2812 if (this == SystemDictionary::String_klass()) {
duke@1 2813 typeArrayOop value = java_lang_String::value(obj);
duke@1 2814 juint length = java_lang_String::length(obj);
duke@1 2815 if (value != NULL &&
duke@1 2816 value->is_typeArray() &&
thartmann@33628 2817 length <= (juint) value->length()) {
jrose@2332 2818 st->print(BULLET"string: ");
stefank@22907 2819 java_lang_String::print(obj, st);
duke@1 2820 st->cr();
duke@1 2821 if (!WizardMode) return; // that is enough
duke@1 2822 }
duke@1 2823 }
duke@1 2824
jrose@2332 2825 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj));
never@8725 2826 FieldPrinter print_field(st, obj);
never@8725 2827 do_nonstatic_fields(&print_field);
duke@1 2828
coleenp@13728 2829 if (this == SystemDictionary::Class_klass()) {
jrose@2332 2830 st->print(BULLET"signature: ");
jrose@2332 2831 java_lang_Class::print_signature(obj, st);
jrose@2332 2832 st->cr();
coleenp@13728 2833 Klass* mirrored_klass = java_lang_Class::as_Klass(obj);
jrose@2332 2834 st->print(BULLET"fake entry for mirror: ");
coleenp@13728 2835 mirrored_klass->print_value_on_maybe_null(st);
duke@1 2836 st->cr();
coleenp@13728 2837 Klass* array_klass = java_lang_Class::array_klass(obj);
jrose@2332 2838 st->print(BULLET"fake entry for array: ");
coleenp@13728 2839 array_klass->print_value_on_maybe_null(st);
duke@1 2840 st->cr();
never@8725 2841 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj));
never@8725 2842 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj));
coleenp@13728 2843 Klass* real_klass = java_lang_Class::as_Klass(obj);
coleenp@33611 2844 if (real_klass != NULL && real_klass->is_instance_klass()) {
coleenp@13728 2845 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
never@8725 2846 }
coleenp@13728 2847 } else if (this == SystemDictionary::MethodType_klass()) {
jrose@4094 2848 st->print(BULLET"signature: ");
jrose@8676 2849 java_lang_invoke_MethodType::print_signature(obj, st);
jrose@4094 2850 st->cr();
duke@1 2851 }
duke@1 2852 }
duke@1 2853
jrose@4584 2854 #endif //PRODUCT
jrose@4584 2855
coleenp@13728 2856 void InstanceKlass::oop_print_value_on(oop obj, outputStream* st) {
duke@1 2857 st->print("a ");
duke@1 2858 name()->print_value_on(st);
duke@1 2859 obj->print_address_on(st);
coleenp@13728 2860 if (this == SystemDictionary::String_klass()
jrose@2332 2861 && java_lang_String::value(obj) != NULL) {
jrose@2332 2862 ResourceMark rm;
jrose@2332 2863 int len = java_lang_String::length(obj);
jrose@2332 2864 int plen = (len < 24 ? len : 12);
jrose@2332 2865 char* str = java_lang_String::as_utf8_string(obj, 0, plen);
jrose@2332 2866 st->print(" = \"%s\"", str);
jrose@2332 2867 if (len > plen)
jrose@2332 2868 st->print("...[%d]", len);
coleenp@13728 2869 } else if (this == SystemDictionary::Class_klass()) {
coleenp@13728 2870 Klass* k = java_lang_Class::as_Klass(obj);
jrose@2332 2871 st->print(" = ");
jrose@2332 2872 if (k != NULL) {
jrose@2332 2873 k->print_value_on(st);
jrose@2332 2874 } else {
jrose@2332 2875 const char* tname = type2name(java_lang_Class::primitive_type(obj));
jrose@2332 2876 st->print("%s", tname ? tname : "type?");
jrose@2332 2877 }
coleenp@13728 2878 } else if (this == SystemDictionary::MethodType_klass()) {
jrose@4094 2879 st->print(" = ");
jrose@8676 2880 java_lang_invoke_MethodType::print_signature(obj, st);
jrose@2332 2881 } else if (java_lang_boxing_object::is_instance(obj)) {
jrose@2332 2882 st->print(" = ");
jrose@2332 2883 java_lang_boxing_object::print(obj, st);
coleenp@13728 2884 } else if (this == SystemDictionary::LambdaForm_klass()) {
twisti@13391 2885 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj);
twisti@13391 2886 if (vmentry != NULL) {
twisti@13391 2887 st->print(" => ");
twisti@13391 2888 vmentry->print_value_on(st);
twisti@13391 2889 }
coleenp@13728 2890 } else if (this == SystemDictionary::MemberName_klass()) {
coleenp@13728 2891 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
twisti@13391 2892 if (vmtarget != NULL) {
twisti@13391 2893 st->print(" = ");
twisti@13391 2894 vmtarget->print_value_on(st);
twisti@13391 2895 } else {
twisti@13391 2896 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st);
twisti@13391 2897 st->print(".");
twisti@13391 2898 java_lang_invoke_MemberName::name(obj)->print_value_on(st);
twisti@13391 2899 }
jrose@2332 2900 }
duke@1 2901 }
duke@1 2902
coleenp@13728 2903 const char* InstanceKlass::internal_name() const {
duke@1 2904 return external_name();
duke@1 2905 }
duke@1 2906
acorn@15437 2907 #if INCLUDE_SERVICES
acorn@15437 2908 // Size Statistics
acorn@15437 2909 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const {
acorn@15437 2910 Klass::collect_statistics(sz);
acorn@15437 2911
acorn@15437 2912 sz->_inst_size = HeapWordSize * size_helper();
acorn@15437 2913 sz->_vtab_bytes = HeapWordSize * align_object_offset(vtable_length());
acorn@15437 2914 sz->_itab_bytes = HeapWordSize * align_object_offset(itable_length());
acorn@15437 2915 sz->_nonstatic_oopmap_bytes = HeapWordSize *
acorn@15437 2916 ((is_interface() || is_anonymous()) ?
acorn@15437 2917 align_object_offset(nonstatic_oop_map_size()) :
acorn@15437 2918 nonstatic_oop_map_size());
acorn@15437 2919
acorn@15437 2920 int n = 0;
acorn@15437 2921 n += (sz->_methods_array_bytes = sz->count_array(methods()));
acorn@15437 2922 n += (sz->_method_ordering_bytes = sz->count_array(method_ordering()));
acorn@15437 2923 n += (sz->_local_interfaces_bytes = sz->count_array(local_interfaces()));
acorn@15437 2924 n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces()));
acorn@15437 2925 n += (sz->_fields_bytes = sz->count_array(fields()));
acorn@15437 2926 n += (sz->_inner_classes_bytes = sz->count_array(inner_classes()));
acorn@15437 2927 sz->_ro_bytes += n;
acorn@15437 2928
acorn@15437 2929 const ConstantPool* cp = constants();
acorn@15437 2930 if (cp) {
acorn@15437 2931 cp->collect_statistics(sz);
acorn@15437 2932 }
acorn@15437 2933
acorn@15437 2934 const Annotations* anno = annotations();
acorn@15437 2935 if (anno) {
acorn@15437 2936 anno->collect_statistics(sz);
acorn@15437 2937 }
acorn@15437 2938
acorn@15437 2939 const Array<Method*>* methods_array = methods();
acorn@15437 2940 if (methods()) {
acorn@15437 2941 for (int i = 0; i < methods_array->length(); i++) {
acorn@15437 2942 Method* method = methods_array->at(i);
acorn@15437 2943 if (method) {
acorn@15437 2944 sz->_method_count ++;
acorn@15437 2945 method->collect_statistics(sz);
acorn@15437 2946 }
acorn@15437 2947 }
acorn@15437 2948 }
acorn@15437 2949 }
acorn@15437 2950 #endif // INCLUDE_SERVICES
acorn@15437 2951
duke@1 2952 // Verification
duke@1 2953
duke@1 2954 class VerifyFieldClosure: public OopClosure {
coleenp@360 2955 protected:
coleenp@360 2956 template <class T> void do_oop_work(T* p) {
coleenp@360 2957 oop obj = oopDesc::load_decode_heap_oop(p);
coleenp@360 2958 if (!obj->is_oop_or_null()) {
david@33148 2959 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p2i(p), p2i(obj));
duke@1 2960 Universe::print();
duke@1 2961 guarantee(false, "boom");
duke@1 2962 }
duke@1 2963 }
coleenp@360 2964 public:
coleenp@360 2965 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); }
coleenp@360 2966 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
duke@1 2967 };
duke@1 2968
coleenp@22794 2969 void InstanceKlass::verify_on(outputStream* st) {
coleenp@13728 2970 #ifndef PRODUCT
coleenp@18439 2971 // Avoid redundant verifies, this really should be in product.
coleenp@13728 2972 if (_verify_count == Universe::verify_count()) return;
coleenp@13728 2973 _verify_count = Universe::verify_count();
coleenp@13728 2974 #endif
coleenp@18439 2975
coleenp@18439 2976 // Verify Klass
coleenp@22794 2977 Klass::verify_on(st);
coleenp@22794 2978
coleenp@22794 2979 // Verify that klass is present in ClassLoaderData
coleenp@22794 2980 guarantee(class_loader_data()->contains_klass(this),
coleenp@22794 2981 "this class isn't found in class loader data");
coleenp@13728 2982
coleenp@13728 2983 // Verify vtables
coleenp@13728 2984 if (is_linked()) {
coleenp@18439 2985 ResourceMark rm;
coleenp@13728 2986 // $$$ This used to be done only for m/s collections. Doing it
coleenp@13728 2987 // always seemed a valid generalization. (DLD -- 6/00)
coleenp@13728 2988 vtable()->verify(st);
coleenp@13728 2989 }
coleenp@13728 2990
coleenp@13728 2991 // Verify first subklass
coleenp@23515 2992 if (subklass() != NULL) {
coleenp@23515 2993 guarantee(subklass()->is_klass(), "should be klass");
coleenp@13728 2994 }
coleenp@13728 2995
coleenp@13728 2996 // Verify siblings
coleenp@13728 2997 Klass* super = this->super();
coleenp@13728 2998 Klass* sib = next_sibling();
coleenp@13728 2999 if (sib != NULL) {
coleenp@13728 3000 if (sib == this) {
david@33148 3001 fatal("subclass points to itself " PTR_FORMAT, p2i(sib));
coleenp@13728 3002 }
coleenp@13728 3003
coleenp@13728 3004 guarantee(sib->is_klass(), "should be klass");
coleenp@13728 3005 guarantee(sib->super() == super, "siblings should have same superklass");
coleenp@13728 3006 }
coleenp@13728 3007
coleenp@13728 3008 // Verify implementor fields
coleenp@13728 3009 Klass* im = implementor();
coleenp@13728 3010 if (im != NULL) {
coleenp@13728 3011 guarantee(is_interface(), "only interfaces should have implementor set");
coleenp@13728 3012 guarantee(im->is_klass(), "should be klass");
hseigel@14488 3013 guarantee(!im->is_interface() || im == this,
coleenp@13728 3014 "implementors cannot be interfaces");
coleenp@13728 3015 }
coleenp@13728 3016
coleenp@13728 3017 // Verify local interfaces
coleenp@13728 3018 if (local_interfaces()) {
coleenp@13728 3019 Array<Klass*>* local_interfaces = this->local_interfaces();
coleenp@13728 3020 for (int j = 0; j < local_interfaces->length(); j++) {
coleenp@13728 3021 Klass* e = local_interfaces->at(j);
hseigel@14488 3022 guarantee(e->is_klass() && e->is_interface(), "invalid local interface");
coleenp@13728 3023 }
coleenp@13728 3024 }
coleenp@13728 3025
coleenp@13728 3026 // Verify transitive interfaces
coleenp@13728 3027 if (transitive_interfaces() != NULL) {
coleenp@13728 3028 Array<Klass*>* transitive_interfaces = this->transitive_interfaces();
coleenp@13728 3029 for (int j = 0; j < transitive_interfaces->length(); j++) {
coleenp@13728 3030 Klass* e = transitive_interfaces->at(j);
hseigel@14488 3031 guarantee(e->is_klass() && e->is_interface(), "invalid transitive interface");
coleenp@13728 3032 }
coleenp@13728 3033 }
coleenp@13728 3034
coleenp@13728 3035 // Verify methods
coleenp@13728 3036 if (methods() != NULL) {
coleenp@13728 3037 Array<Method*>* methods = this->methods();
coleenp@13728 3038 for (int j = 0; j < methods->length(); j++) {
coleenp@13728 3039 guarantee(methods->at(j)->is_method(), "non-method in methods array");
coleenp@13728 3040 }
coleenp@13728 3041 for (int j = 0; j < methods->length() - 1; j++) {
coleenp@13728 3042 Method* m1 = methods->at(j);
coleenp@13728 3043 Method* m2 = methods->at(j + 1);
coleenp@13728 3044 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
coleenp@13728 3045 }
coleenp@13728 3046 }
coleenp@13728 3047
coleenp@13728 3048 // Verify method ordering
coleenp@13728 3049 if (method_ordering() != NULL) {
coleenp@13728 3050 Array<int>* method_ordering = this->method_ordering();
coleenp@13728 3051 int length = method_ordering->length();
coleenp@13728 3052 if (JvmtiExport::can_maintain_original_method_order() ||
hseigel@16667 3053 ((UseSharedSpaces || DumpSharedSpaces) && length != 0)) {
coleenp@13728 3054 guarantee(length == methods()->length(), "invalid method ordering length");
coleenp@13728 3055 jlong sum = 0;
coleenp@13728 3056 for (int j = 0; j < length; j++) {
coleenp@13728 3057 int original_index = method_ordering->at(j);
coleenp@13728 3058 guarantee(original_index >= 0, "invalid method ordering index");
coleenp@13728 3059 guarantee(original_index < length, "invalid method ordering index");
coleenp@13728 3060 sum += original_index;
coleenp@13728 3061 }
coleenp@13728 3062 // Verify sum of indices 0,1,...,length-1
coleenp@13728 3063 guarantee(sum == ((jlong)length*(length-1))/2, "invalid method ordering sum");
coleenp@13728 3064 } else {
coleenp@13728 3065 guarantee(length == 0, "invalid method ordering length");
coleenp@13728 3066 }
coleenp@13728 3067 }
coleenp@13728 3068
acorn@20391 3069 // Verify default methods
acorn@20391 3070 if (default_methods() != NULL) {
acorn@20391 3071 Array<Method*>* methods = this->default_methods();
acorn@20391 3072 for (int j = 0; j < methods->length(); j++) {
acorn@20391 3073 guarantee(methods->at(j)->is_method(), "non-method in methods array");
acorn@20391 3074 }
acorn@20391 3075 for (int j = 0; j < methods->length() - 1; j++) {
acorn@20391 3076 Method* m1 = methods->at(j);
acorn@20391 3077 Method* m2 = methods->at(j + 1);
acorn@20391 3078 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
acorn@20391 3079 }
acorn@20391 3080 }
acorn@20391 3081
coleenp@13728 3082 // Verify JNI static field identifiers
coleenp@13728 3083 if (jni_ids() != NULL) {
coleenp@13728 3084 jni_ids()->verify(this);
coleenp@13728 3085 }
coleenp@13728 3086
coleenp@13728 3087 // Verify other fields
coleenp@13728 3088 if (array_klasses() != NULL) {
coleenp@13728 3089 guarantee(array_klasses()->is_klass(), "should be klass");
coleenp@13728 3090 }
coleenp@13728 3091 if (constants() != NULL) {
coleenp@13728 3092 guarantee(constants()->is_constantPool(), "should be constant pool");
coleenp@13728 3093 }
morris@15873 3094 const Klass* host = host_klass();
morris@15873 3095 if (host != NULL) {
morris@15873 3096 guarantee(host->is_klass(), "should be klass");
coleenp@13728 3097 }
coleenp@13728 3098 }
coleenp@13728 3099
coleenp@13728 3100 void InstanceKlass::oop_verify_on(oop obj, outputStream* st) {
duke@1 3101 Klass::oop_verify_on(obj, st);
duke@1 3102 VerifyFieldClosure blk;
coleenp@13728 3103 obj->oop_iterate_no_header(&blk);
duke@1 3104 }
duke@1 3105
coleenp@13728 3106
coleenp@360 3107 // JNIid class for jfieldIDs only
coleenp@360 3108 // Note to reviewers:
coleenp@360 3109 // These JNI functions are just moved over to column 1 and not changed
coleenp@360 3110 // in the compressed oops workspace.
coleenp@13728 3111 JNIid::JNIid(Klass* holder, int offset, JNIid* next) {
coleenp@360 3112 _holder = holder;
coleenp@360 3113 _offset = offset;
coleenp@360 3114 _next = next;
coleenp@360 3115 debug_only(_is_static_field_id = false;)
coleenp@360 3116 }
duke@1 3117
duke@1 3118
coleenp@360 3119 JNIid* JNIid::find(int offset) {
coleenp@360 3120 JNIid* current = this;
coleenp@360 3121 while (current != NULL) {
coleenp@360 3122 if (current->offset() == offset) return current;
coleenp@360 3123 current = current->next();
coleenp@360 3124 }
coleenp@360 3125 return NULL;
coleenp@360 3126 }
duke@1 3127
duke@1 3128 void JNIid::deallocate(JNIid* current) {
coleenp@360 3129 while (current != NULL) {
coleenp@360 3130 JNIid* next = current->next();
coleenp@360 3131 delete current;
coleenp@360 3132 current = next;
coleenp@360 3133 }
coleenp@360 3134 }
duke@1 3135
duke@1 3136
coleenp@13728 3137 void JNIid::verify(Klass* holder) {
coleenp@13738 3138 int first_field_offset = InstanceMirrorKlass::offset_of_static_fields();
coleenp@360 3139 int end_field_offset;
coleenp@13728 3140 end_field_offset = first_field_offset + (InstanceKlass::cast(holder)->static_field_size() * wordSize);
duke@1 3141
coleenp@360 3142 JNIid* current = this;
coleenp@360 3143 while (current != NULL) {
coleenp@360 3144 guarantee(current->holder() == holder, "Invalid klass in JNIid");
coleenp@360 3145 #ifdef ASSERT
coleenp@360 3146 int o = current->offset();
coleenp@360 3147 if (current->is_static_field_id()) {
coleenp@360 3148 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid");
coleenp@360 3149 }
coleenp@360 3150 #endif
coleenp@360 3151 current = current->next();
coleenp@360 3152 }
coleenp@360 3153 }
duke@1 3154
duke@1 3155
duke@1 3156 #ifdef ASSERT
coleenp@13728 3157 void InstanceKlass::set_init_state(ClassState state) {
coleenp@13728 3158 bool good_state = is_shared() ? (_init_state <= state)
coleenp@360 3159 : (_init_state < state);
coleenp@360 3160 assert(good_state || state == allocated, "illegal state transition");
coleenp@11407 3161 _init_state = (u1)state;
coleenp@360 3162 }
duke@1 3163 #endif
duke@1 3164
duke@1 3165
coleenp@29576 3166
duke@1 3167 // RedefineClasses() support for previous versions:
coleenp@29576 3168 int InstanceKlass::_previous_version_count = 0;
coleenp@29576 3169
coleenp@29576 3170 // Purge previous versions before adding new previous versions of the class.
coleenp@26558 3171 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) {
coleenp@13728 3172 if (ik->previous_versions() != NULL) {
coleenp@13728 3173 // This klass has previous versions so see what we can cleanup
coleenp@13728 3174 // while it is safe to do so.
coleenp@13728 3175
coleenp@13728 3176 int deleted_count = 0; // leave debugging breadcrumbs
coleenp@13728 3177 int live_count = 0;
coleenp@26558 3178 ClassLoaderData* loader_data = ik->class_loader_data();
coleenp@26558 3179 assert(loader_data != NULL, "should never be null");
coleenp@13728 3180
coleenp@13728 3181 // RC_TRACE macro has an embedded ResourceMark
coleenp@26558 3182 RC_TRACE(0x00000200, ("purge: %s: previous versions", ik->external_name()));
coleenp@26558 3183
coleenp@26558 3184 // previous versions are linked together through the InstanceKlass
coleenp@26558 3185 InstanceKlass* pv_node = ik->previous_versions();
coleenp@26558 3186 InstanceKlass* last = ik;
coleenp@26558 3187 int version = 0;
coleenp@26558 3188
coleenp@26558 3189 // check the previous versions list
coleenp@26558 3190 for (; pv_node != NULL; ) {
coleenp@26558 3191
coleenp@26558 3192 ConstantPool* pvcp = pv_node->constants();
coleenp@26558 3193 assert(pvcp != NULL, "cp ref was unexpectedly cleared");
coleenp@26558 3194
coleenp@13728 3195 if (!pvcp->on_stack()) {
coleenp@13728 3196 // If the constant pool isn't on stack, none of the methods
coleenp@26558 3197 // are executing. Unlink this previous_version.
coleenp@26558 3198 // The previous version InstanceKlass is on the ClassLoaderData deallocate list
coleenp@26558 3199 // so will be deallocated during the next phase of class unloading.
coleenp@29576 3200 RC_TRACE(0x00000200, ("purge: previous version " INTPTR_FORMAT " is dead",
david@33148 3201 p2i(pv_node)));
coleenp@29576 3202 // For debugging purposes.
coleenp@29576 3203 pv_node->set_is_scratch_class();
coleenp@29576 3204 pv_node->class_loader_data()->add_to_deallocate_list(pv_node);
coleenp@26558 3205 pv_node = pv_node->previous_versions();
coleenp@26558 3206 last->link_previous_versions(pv_node);
coleenp@13728 3207 deleted_count++;
coleenp@26558 3208 version++;
coleenp@13728 3209 continue;
coleenp@13728 3210 } else {
coleenp@26558 3211 RC_TRACE(0x00000200, ("purge: previous version " INTPTR_FORMAT " is alive",
david@33148 3212 p2i(pv_node)));
coleenp@13728 3213 assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
coleenp@13728 3214 guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
coleenp@13728 3215 live_count++;
coleenp@13728 3216 }
coleenp@13728 3217
coleenp@29576 3218 // At least one method is live in this previous version.
coleenp@26558 3219 // Reset dead EMCP methods not to get breakpoints.
coleenp@26558 3220 // All methods are deallocated when all of the methods for this class are no
coleenp@26558 3221 // longer running.
coleenp@26558 3222 Array<Method*>* method_refs = pv_node->methods();
coleenp@13728 3223 if (method_refs != NULL) {
coleenp@13728 3224 RC_TRACE(0x00000200, ("purge: previous methods length=%d",
coleenp@13728 3225 method_refs->length()));
coleenp@26558 3226 for (int j = 0; j < method_refs->length(); j++) {
coleenp@13728 3227 Method* method = method_refs->at(j);
coleenp@26558 3228
coleenp@13728 3229 if (!method->on_stack()) {
coleenp@26558 3230 // no breakpoints for non-running methods
coleenp@26558 3231 if (method->is_running_emcp()) {
coleenp@26558 3232 method->set_running_emcp(false);
coleenp@26558 3233 }
coleenp@13728 3234 } else {
coleenp@26558 3235 assert (method->is_obsolete() || method->is_running_emcp(),
coleenp@26558 3236 "emcp method cannot run after emcp bit is cleared");
coleenp@13728 3237 // RC_TRACE macro has an embedded ResourceMark
coleenp@13728 3238 RC_TRACE(0x00000200,
coleenp@13728 3239 ("purge: %s(%s): prev method @%d in version @%d is alive",
coleenp@13728 3240 method->name()->as_C_string(),
coleenp@26558 3241 method->signature()->as_C_string(), j, version));
coleenp@13728 3242 }
coleenp@13728 3243 }
coleenp@13728 3244 }
coleenp@26558 3245 // next previous version
coleenp@26558 3246 last = pv_node;
coleenp@26558 3247 pv_node = pv_node->previous_versions();
coleenp@26558 3248 version++;
coleenp@13728 3249 }
coleenp@13728 3250 RC_TRACE(0x00000200,
coleenp@13728 3251 ("purge: previous version stats: live=%d, deleted=%d", live_count,
coleenp@13728 3252 deleted_count));
coleenp@13728 3253 }
coleenp@13728 3254 }
coleenp@13728 3255
coleenp@26558 3256 void InstanceKlass::mark_newly_obsolete_methods(Array<Method*>* old_methods,
coleenp@26558 3257 int emcp_method_count) {
duke@1 3258 int obsolete_method_count = old_methods->length() - emcp_method_count;
duke@1 3259
duke@1 3260 if (emcp_method_count != 0 && obsolete_method_count != 0 &&
coleenp@26558 3261 _previous_versions != NULL) {
coleenp@13728 3262 // We have a mix of obsolete and EMCP methods so we have to
duke@1 3263 // clear out any matching EMCP method entries the hard way.
duke@1 3264 int local_count = 0;
duke@1 3265 for (int i = 0; i < old_methods->length(); i++) {
coleenp@26558 3266 Method* old_method = old_methods->at(i);
coleenp@26558