Thu, 11 Oct 2012 12:25:42 -0400
7200776: Implement default methods in interfaces
Summary: Add generic type analysis and default method selection algorithms
Reviewed-by: coleenp, acorn
duke@435 | 1 | /* |
brutisso@3675 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
coleenp@4037 | 26 | #include "classfile/javaClasses.hpp" |
coleenp@4037 | 27 | #include "classfile/dictionary.hpp" |
stefank@2314 | 28 | #include "classfile/systemDictionary.hpp" |
stefank@2314 | 29 | #include "classfile/vmSymbols.hpp" |
coleenp@4037 | 30 | #include "gc_implementation/shared/markSweep.inline.hpp" |
stefank@2314 | 31 | #include "gc_interface/collectedHeap.inline.hpp" |
coleenp@4037 | 32 | #include "memory/metadataFactory.hpp" |
stefank@2314 | 33 | #include "memory/oopFactory.hpp" |
stefank@2314 | 34 | #include "memory/resourceArea.hpp" |
stefank@2314 | 35 | #include "oops/instanceKlass.hpp" |
stefank@2314 | 36 | #include "oops/klass.inline.hpp" |
stefank@2314 | 37 | #include "oops/oop.inline2.hpp" |
stefank@2314 | 38 | #include "runtime/atomic.hpp" |
coleenp@4037 | 39 | #include "utilities/stack.hpp" |
coleenp@4037 | 40 | #ifndef SERIALGC |
coleenp@4037 | 41 | #include "gc_implementation/parallelScavenge/psParallelCompact.hpp" |
coleenp@4037 | 42 | #include "gc_implementation/parallelScavenge/psPromotionManager.hpp" |
coleenp@4037 | 43 | #include "gc_implementation/parallelScavenge/psScavenge.hpp" |
coleenp@4037 | 44 | #endif |
duke@435 | 45 | |
coleenp@2497 | 46 | void Klass::set_name(Symbol* n) { |
coleenp@2497 | 47 | _name = n; |
coleenp@2497 | 48 | if (_name != NULL) _name->increment_refcount(); |
coleenp@2497 | 49 | } |
duke@435 | 50 | |
coleenp@4037 | 51 | bool Klass::is_subclass_of(Klass* k) const { |
duke@435 | 52 | // Run up the super chain and check |
coleenp@4037 | 53 | if (this == k) return true; |
duke@435 | 54 | |
coleenp@4037 | 55 | Klass* t = const_cast<Klass*>(this)->super(); |
duke@435 | 56 | |
duke@435 | 57 | while (t != NULL) { |
duke@435 | 58 | if (t == k) return true; |
duke@435 | 59 | t = Klass::cast(t)->super(); |
duke@435 | 60 | } |
duke@435 | 61 | return false; |
duke@435 | 62 | } |
duke@435 | 63 | |
coleenp@4037 | 64 | bool Klass::search_secondary_supers(Klass* k) const { |
duke@435 | 65 | // Put some extra logic here out-of-line, before the search proper. |
duke@435 | 66 | // This cuts down the size of the inline method. |
duke@435 | 67 | |
duke@435 | 68 | // This is necessary, since I am never in my own secondary_super list. |
coleenp@4037 | 69 | if (this == k) |
duke@435 | 70 | return true; |
duke@435 | 71 | // Scan the array-of-objects for a match |
duke@435 | 72 | int cnt = secondary_supers()->length(); |
duke@435 | 73 | for (int i = 0; i < cnt; i++) { |
coleenp@4037 | 74 | if (secondary_supers()->at(i) == k) { |
duke@435 | 75 | ((Klass*)this)->set_secondary_super_cache(k); |
duke@435 | 76 | return true; |
duke@435 | 77 | } |
duke@435 | 78 | } |
duke@435 | 79 | return false; |
duke@435 | 80 | } |
duke@435 | 81 | |
duke@435 | 82 | // Return self, except for abstract classes with exactly 1 |
duke@435 | 83 | // implementor. Then return the 1 concrete implementation. |
duke@435 | 84 | Klass *Klass::up_cast_abstract() { |
duke@435 | 85 | Klass *r = this; |
duke@435 | 86 | while( r->is_abstract() ) { // Receiver is abstract? |
duke@435 | 87 | Klass *s = r->subklass(); // Check for exactly 1 subklass |
duke@435 | 88 | if( !s || s->next_sibling() ) // Oops; wrong count; give up |
duke@435 | 89 | return this; // Return 'this' as a no-progress flag |
duke@435 | 90 | r = s; // Loop till find concrete class |
duke@435 | 91 | } |
duke@435 | 92 | return r; // Return the 1 concrete class |
duke@435 | 93 | } |
duke@435 | 94 | |
twisti@1040 | 95 | // Find LCA in class hierarchy |
duke@435 | 96 | Klass *Klass::LCA( Klass *k2 ) { |
duke@435 | 97 | Klass *k1 = this; |
duke@435 | 98 | while( 1 ) { |
coleenp@4037 | 99 | if( k1->is_subtype_of(k2) ) return k2; |
coleenp@4037 | 100 | if( k2->is_subtype_of(k1) ) return k1; |
coleenp@4037 | 101 | k1 = k1->super(); |
coleenp@4037 | 102 | k2 = k2->super(); |
duke@435 | 103 | } |
duke@435 | 104 | } |
duke@435 | 105 | |
duke@435 | 106 | |
duke@435 | 107 | void Klass::check_valid_for_instantiation(bool throwError, TRAPS) { |
duke@435 | 108 | ResourceMark rm(THREAD); |
duke@435 | 109 | THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() |
duke@435 | 110 | : vmSymbols::java_lang_InstantiationException(), external_name()); |
duke@435 | 111 | } |
duke@435 | 112 | |
duke@435 | 113 | |
duke@435 | 114 | void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { |
duke@435 | 115 | THROW(vmSymbols::java_lang_ArrayStoreException()); |
duke@435 | 116 | } |
duke@435 | 117 | |
duke@435 | 118 | |
duke@435 | 119 | void Klass::initialize(TRAPS) { |
duke@435 | 120 | ShouldNotReachHere(); |
duke@435 | 121 | } |
duke@435 | 122 | |
coleenp@4037 | 123 | bool Klass::compute_is_subtype_of(Klass* k) { |
duke@435 | 124 | assert(k->is_klass(), "argument must be a class"); |
duke@435 | 125 | return is_subclass_of(k); |
duke@435 | 126 | } |
duke@435 | 127 | |
duke@435 | 128 | |
coleenp@4037 | 129 | Method* Klass::uncached_lookup_method(Symbol* name, Symbol* signature) const { |
duke@435 | 130 | #ifdef ASSERT |
duke@435 | 131 | tty->print_cr("Error: uncached_lookup_method called on a klass oop." |
duke@435 | 132 | " Likely error: reflection method does not correctly" |
duke@435 | 133 | " wrap return value in a mirror object."); |
duke@435 | 134 | #endif |
duke@435 | 135 | ShouldNotReachHere(); |
duke@435 | 136 | return NULL; |
duke@435 | 137 | } |
duke@435 | 138 | |
coleenp@4037 | 139 | void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) { |
coleenp@4037 | 140 | return Metaspace::allocate(loader_data, word_size, /*read_only*/false, |
coleenp@4037 | 141 | Metaspace::ClassType, CHECK_NULL); |
coleenp@4037 | 142 | } |
coleenp@4037 | 143 | |
coleenp@4037 | 144 | Klass::Klass() { |
coleenp@4037 | 145 | Klass* k = this; |
duke@435 | 146 | |
duke@435 | 147 | { // Preinitialize supertype information. |
duke@435 | 148 | // A later call to initialize_supers() may update these settings: |
coleenp@4037 | 149 | set_super(NULL); |
duke@435 | 150 | for (juint i = 0; i < Klass::primary_super_limit(); i++) { |
coleenp@4037 | 151 | _primary_supers[i] = NULL; |
duke@435 | 152 | } |
coleenp@4037 | 153 | set_secondary_supers(NULL); |
coleenp@4037 | 154 | _primary_supers[0] = k; |
coleenp@4037 | 155 | set_super_check_offset(in_bytes(primary_supers_offset())); |
duke@435 | 156 | } |
duke@435 | 157 | |
coleenp@4037 | 158 | set_java_mirror(NULL); |
coleenp@4037 | 159 | set_modifier_flags(0); |
coleenp@4037 | 160 | set_layout_helper(Klass::_lh_neutral_value); |
coleenp@4037 | 161 | set_name(NULL); |
duke@435 | 162 | AccessFlags af; |
duke@435 | 163 | af.set_flags(0); |
coleenp@4037 | 164 | set_access_flags(af); |
coleenp@4037 | 165 | set_subklass(NULL); |
coleenp@4037 | 166 | set_next_sibling(NULL); |
coleenp@4037 | 167 | set_next_link(NULL); |
coleenp@4037 | 168 | set_alloc_count(0); |
coleenp@4037 | 169 | TRACE_SET_KLASS_TRACE_ID(this, 0); |
duke@435 | 170 | |
coleenp@4037 | 171 | set_prototype_header(markOopDesc::prototype()); |
coleenp@4037 | 172 | set_biased_lock_revocation_count(0); |
coleenp@4037 | 173 | set_last_biased_lock_bulk_revocation_time(0); |
duke@435 | 174 | |
coleenp@4037 | 175 | // The klass doesn't have any references at this point. |
coleenp@4037 | 176 | clear_modified_oops(); |
coleenp@4037 | 177 | clear_accumulated_modified_oops(); |
duke@435 | 178 | } |
duke@435 | 179 | |
duke@435 | 180 | jint Klass::array_layout_helper(BasicType etype) { |
duke@435 | 181 | assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); |
duke@435 | 182 | // Note that T_ARRAY is not allowed here. |
duke@435 | 183 | int hsize = arrayOopDesc::base_offset_in_bytes(etype); |
kvn@464 | 184 | int esize = type2aelembytes(etype); |
duke@435 | 185 | bool isobj = (etype == T_OBJECT); |
duke@435 | 186 | int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value; |
duke@435 | 187 | int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize)); |
duke@435 | 188 | |
duke@435 | 189 | assert(lh < (int)_lh_neutral_value, "must look like an array layout"); |
coleenp@4037 | 190 | assert(layout_helper_is_array(lh), "correct kind"); |
duke@435 | 191 | assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); |
duke@435 | 192 | assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); |
duke@435 | 193 | assert(layout_helper_header_size(lh) == hsize, "correct decode"); |
duke@435 | 194 | assert(layout_helper_element_type(lh) == etype, "correct decode"); |
duke@435 | 195 | assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); |
duke@435 | 196 | |
duke@435 | 197 | return lh; |
duke@435 | 198 | } |
duke@435 | 199 | |
duke@435 | 200 | bool Klass::can_be_primary_super_slow() const { |
duke@435 | 201 | if (super() == NULL) |
duke@435 | 202 | return true; |
coleenp@4037 | 203 | else if (super()->super_depth() >= primary_super_limit()-1) |
duke@435 | 204 | return false; |
duke@435 | 205 | else |
duke@435 | 206 | return true; |
duke@435 | 207 | } |
duke@435 | 208 | |
coleenp@4037 | 209 | void Klass::initialize_supers(Klass* k, TRAPS) { |
duke@435 | 210 | if (FastSuperclassLimit == 0) { |
duke@435 | 211 | // None of the other machinery matters. |
duke@435 | 212 | set_super(k); |
duke@435 | 213 | return; |
duke@435 | 214 | } |
duke@435 | 215 | if (k == NULL) { |
duke@435 | 216 | set_super(NULL); |
coleenp@4037 | 217 | _primary_supers[0] = this; |
duke@435 | 218 | assert(super_depth() == 0, "Object must already be initialized properly"); |
never@1577 | 219 | } else if (k != super() || k == SystemDictionary::Object_klass()) { |
never@1577 | 220 | assert(super() == NULL || super() == SystemDictionary::Object_klass(), |
duke@435 | 221 | "initialize this only once to a non-trivial value"); |
duke@435 | 222 | set_super(k); |
coleenp@4037 | 223 | Klass* sup = k; |
duke@435 | 224 | int sup_depth = sup->super_depth(); |
duke@435 | 225 | juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); |
duke@435 | 226 | if (!can_be_primary_super_slow()) |
duke@435 | 227 | my_depth = primary_super_limit(); |
duke@435 | 228 | for (juint i = 0; i < my_depth; i++) { |
coleenp@4037 | 229 | _primary_supers[i] = sup->_primary_supers[i]; |
duke@435 | 230 | } |
coleenp@4037 | 231 | Klass* *super_check_cell; |
duke@435 | 232 | if (my_depth < primary_super_limit()) { |
coleenp@4037 | 233 | _primary_supers[my_depth] = this; |
duke@435 | 234 | super_check_cell = &_primary_supers[my_depth]; |
duke@435 | 235 | } else { |
duke@435 | 236 | // Overflow of the primary_supers array forces me to be secondary. |
duke@435 | 237 | super_check_cell = &_secondary_super_cache; |
duke@435 | 238 | } |
coleenp@4037 | 239 | set_super_check_offset((address)super_check_cell - (address) this); |
duke@435 | 240 | |
duke@435 | 241 | #ifdef ASSERT |
duke@435 | 242 | { |
duke@435 | 243 | juint j = super_depth(); |
duke@435 | 244 | assert(j == my_depth, "computed accessor gets right answer"); |
coleenp@4037 | 245 | Klass* t = this; |
duke@435 | 246 | while (!Klass::cast(t)->can_be_primary_super()) { |
duke@435 | 247 | t = Klass::cast(t)->super(); |
duke@435 | 248 | j = Klass::cast(t)->super_depth(); |
duke@435 | 249 | } |
duke@435 | 250 | for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { |
duke@435 | 251 | assert(primary_super_of_depth(j1) == NULL, "super list padding"); |
duke@435 | 252 | } |
duke@435 | 253 | while (t != NULL) { |
duke@435 | 254 | assert(primary_super_of_depth(j) == t, "super list initialization"); |
duke@435 | 255 | t = Klass::cast(t)->super(); |
duke@435 | 256 | --j; |
duke@435 | 257 | } |
duke@435 | 258 | assert(j == (juint)-1, "correct depth count"); |
duke@435 | 259 | } |
duke@435 | 260 | #endif |
duke@435 | 261 | } |
duke@435 | 262 | |
duke@435 | 263 | if (secondary_supers() == NULL) { |
duke@435 | 264 | KlassHandle this_kh (THREAD, this); |
duke@435 | 265 | |
duke@435 | 266 | // Now compute the list of secondary supertypes. |
duke@435 | 267 | // Secondaries can occasionally be on the super chain, |
duke@435 | 268 | // if the inline "_primary_supers" array overflows. |
duke@435 | 269 | int extras = 0; |
coleenp@4037 | 270 | Klass* p; |
coleenp@4037 | 271 | for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) { |
duke@435 | 272 | ++extras; |
duke@435 | 273 | } |
duke@435 | 274 | |
coleenp@4037 | 275 | ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below |
coleenp@4037 | 276 | |
duke@435 | 277 | // Compute the "real" non-extra secondaries. |
coleenp@4037 | 278 | GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras); |
coleenp@4037 | 279 | if (secondaries == NULL) { |
coleenp@4037 | 280 | // secondary_supers set by compute_secondary_supers |
coleenp@4037 | 281 | return; |
coleenp@4037 | 282 | } |
duke@435 | 283 | |
coleenp@4037 | 284 | GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras); |
coleenp@4037 | 285 | |
coleenp@4037 | 286 | for (p = this_kh->super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) { |
duke@435 | 287 | int i; // Scan for overflow primaries being duplicates of 2nd'arys |
duke@435 | 288 | |
duke@435 | 289 | // This happens frequently for very deeply nested arrays: the |
duke@435 | 290 | // primary superclass chain overflows into the secondary. The |
duke@435 | 291 | // secondary list contains the element_klass's secondaries with |
duke@435 | 292 | // an extra array dimension added. If the element_klass's |
duke@435 | 293 | // secondary list already contains some primary overflows, they |
duke@435 | 294 | // (with the extra level of array-ness) will collide with the |
duke@435 | 295 | // normal primary superclass overflows. |
coleenp@4037 | 296 | for( i = 0; i < secondaries->length(); i++ ) { |
coleenp@4037 | 297 | if( secondaries->at(i) == p ) |
duke@435 | 298 | break; |
coleenp@4037 | 299 | } |
duke@435 | 300 | if( i < secondaries->length() ) |
duke@435 | 301 | continue; // It's a dup, don't put it in |
coleenp@4037 | 302 | primaries->push(p); |
duke@435 | 303 | } |
coleenp@4037 | 304 | // Combine the two arrays into a metadata object to pack the array. |
coleenp@4037 | 305 | // The primaries are added in the reverse order, then the secondaries. |
coleenp@4037 | 306 | int new_length = primaries->length() + secondaries->length(); |
coleenp@4037 | 307 | Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>( |
coleenp@4037 | 308 | class_loader_data(), new_length, CHECK); |
coleenp@4037 | 309 | int fill_p = primaries->length(); |
coleenp@4037 | 310 | for (int j = 0; j < fill_p; j++) { |
coleenp@4037 | 311 | s2->at_put(j, primaries->pop()); // add primaries in reverse order. |
coleenp@4037 | 312 | } |
coleenp@4037 | 313 | for( int j = 0; j < secondaries->length(); j++ ) { |
coleenp@4037 | 314 | s2->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end. |
duke@435 | 315 | } |
duke@435 | 316 | |
duke@435 | 317 | #ifdef ASSERT |
duke@435 | 318 | // We must not copy any NULL placeholders left over from bootstrap. |
coleenp@4037 | 319 | for (int j = 0; j < s2->length(); j++) { |
coleenp@4037 | 320 | assert(s2->at(j) != NULL, "correct bootstrapping order"); |
duke@435 | 321 | } |
duke@435 | 322 | #endif |
duke@435 | 323 | |
coleenp@4037 | 324 | this_kh->set_secondary_supers(s2); |
duke@435 | 325 | } |
duke@435 | 326 | } |
duke@435 | 327 | |
coleenp@4037 | 328 | GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) { |
duke@435 | 329 | assert(num_extra_slots == 0, "override for complex klasses"); |
coleenp@4037 | 330 | set_secondary_supers(Universe::the_empty_klass_array()); |
coleenp@4037 | 331 | return NULL; |
duke@435 | 332 | } |
duke@435 | 333 | |
duke@435 | 334 | |
duke@435 | 335 | Klass* Klass::subklass() const { |
duke@435 | 336 | return _subklass == NULL ? NULL : Klass::cast(_subklass); |
duke@435 | 337 | } |
duke@435 | 338 | |
coleenp@4037 | 339 | InstanceKlass* Klass::superklass() const { |
coleenp@4037 | 340 | assert(super() == NULL || super()->oop_is_instance(), "must be instance klass"); |
coleenp@4037 | 341 | return _super == NULL ? NULL : InstanceKlass::cast(_super); |
duke@435 | 342 | } |
duke@435 | 343 | |
duke@435 | 344 | Klass* Klass::next_sibling() const { |
duke@435 | 345 | return _next_sibling == NULL ? NULL : Klass::cast(_next_sibling); |
duke@435 | 346 | } |
duke@435 | 347 | |
coleenp@4037 | 348 | void Klass::set_subklass(Klass* s) { |
coleenp@4037 | 349 | assert(s != this, "sanity check"); |
coleenp@4037 | 350 | _subklass = s; |
duke@435 | 351 | } |
duke@435 | 352 | |
coleenp@4037 | 353 | void Klass::set_next_sibling(Klass* s) { |
coleenp@4037 | 354 | assert(s != this, "sanity check"); |
coleenp@4037 | 355 | _next_sibling = s; |
duke@435 | 356 | } |
duke@435 | 357 | |
duke@435 | 358 | void Klass::append_to_sibling_list() { |
coleenp@4178 | 359 | debug_only(verify();) |
duke@435 | 360 | // add ourselves to superklass' subklass list |
coleenp@4037 | 361 | InstanceKlass* super = superklass(); |
duke@435 | 362 | if (super == NULL) return; // special case: class Object |
coleenp@4178 | 363 | assert((!super->is_interface() // interfaces cannot be supers |
duke@435 | 364 | && (super->superklass() == NULL || !is_interface())), |
duke@435 | 365 | "an interface can only be a subklass of Object"); |
coleenp@4037 | 366 | Klass* prev_first_subklass = super->subklass_oop(); |
duke@435 | 367 | if (prev_first_subklass != NULL) { |
duke@435 | 368 | // set our sibling to be the superklass' previous first subklass |
duke@435 | 369 | set_next_sibling(prev_first_subklass); |
duke@435 | 370 | } |
duke@435 | 371 | // make ourselves the superklass' first subklass |
coleenp@4037 | 372 | super->set_subklass(this); |
coleenp@4178 | 373 | debug_only(verify();) |
duke@435 | 374 | } |
duke@435 | 375 | |
duke@435 | 376 | void Klass::remove_from_sibling_list() { |
duke@435 | 377 | // remove receiver from sibling list |
coleenp@4037 | 378 | InstanceKlass* super = superklass(); |
coleenp@4037 | 379 | assert(super != NULL || this == SystemDictionary::Object_klass(), "should have super"); |
duke@435 | 380 | if (super == NULL) return; // special case: class Object |
duke@435 | 381 | if (super->subklass() == this) { |
duke@435 | 382 | // first subklass |
duke@435 | 383 | super->set_subklass(_next_sibling); |
duke@435 | 384 | } else { |
duke@435 | 385 | Klass* sib = super->subklass(); |
duke@435 | 386 | while (sib->next_sibling() != this) { |
duke@435 | 387 | sib = sib->next_sibling(); |
duke@435 | 388 | }; |
duke@435 | 389 | sib->set_next_sibling(_next_sibling); |
duke@435 | 390 | } |
duke@435 | 391 | } |
duke@435 | 392 | |
coleenp@4037 | 393 | bool Klass::is_loader_alive(BoolObjectClosure* is_alive) { |
coleenp@4037 | 394 | assert(is_metadata(), "p is not meta-data"); |
coleenp@4037 | 395 | assert(ClassLoaderDataGraph::contains((address)this), "is in the metaspace"); |
coleenp@4037 | 396 | // The class is alive iff the class loader is alive. |
coleenp@4037 | 397 | oop loader = class_loader(); |
coleenp@4037 | 398 | return (loader == NULL) || is_alive->do_object_b(loader); |
coleenp@4037 | 399 | } |
coleenp@4037 | 400 | |
coleenp@4037 | 401 | void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive) { |
coleenp@4037 | 402 | if (!ClassUnloading) { |
coleenp@4037 | 403 | return; |
coleenp@4037 | 404 | } |
coleenp@4037 | 405 | |
coleenp@4037 | 406 | Klass* root = SystemDictionary::Object_klass(); |
coleenp@4037 | 407 | Stack<Klass*, mtGC> stack; |
coleenp@4037 | 408 | |
coleenp@4037 | 409 | stack.push(root); |
coleenp@4037 | 410 | while (!stack.is_empty()) { |
coleenp@4037 | 411 | Klass* current = stack.pop(); |
coleenp@4037 | 412 | |
coleenp@4037 | 413 | assert(current->is_loader_alive(is_alive), "just checking, this should be live"); |
coleenp@4037 | 414 | |
coleenp@4037 | 415 | // Find and set the first alive subklass |
coleenp@4037 | 416 | Klass* sub = current->subklass_oop(); |
coleenp@4037 | 417 | while (sub != NULL && !sub->is_loader_alive(is_alive)) { |
duke@435 | 418 | #ifndef PRODUCT |
duke@435 | 419 | if (TraceClassUnloading && WizardMode) { |
duke@435 | 420 | ResourceMark rm; |
coleenp@4037 | 421 | tty->print_cr("[Unlinking class (subclass) %s]", sub->external_name()); |
duke@435 | 422 | } |
duke@435 | 423 | #endif |
coleenp@4037 | 424 | sub = sub->next_sibling_oop(); |
duke@435 | 425 | } |
coleenp@4037 | 426 | current->set_subklass(sub); |
coleenp@4037 | 427 | if (sub != NULL) { |
coleenp@4037 | 428 | stack.push(sub); |
coleenp@4037 | 429 | } |
coleenp@4037 | 430 | |
coleenp@4037 | 431 | // Find and set the first alive sibling |
coleenp@4037 | 432 | Klass* sibling = current->next_sibling_oop(); |
coleenp@4037 | 433 | while (sibling != NULL && !sibling->is_loader_alive(is_alive)) { |
duke@435 | 434 | if (TraceClassUnloading && WizardMode) { |
duke@435 | 435 | ResourceMark rm; |
coleenp@4037 | 436 | tty->print_cr("[Unlinking class (sibling) %s]", sibling->external_name()); |
duke@435 | 437 | } |
coleenp@4037 | 438 | sibling = sibling->next_sibling_oop(); |
duke@435 | 439 | } |
coleenp@4037 | 440 | current->set_next_sibling(sibling); |
coleenp@4037 | 441 | if (sibling != NULL) { |
coleenp@4037 | 442 | stack.push(sibling); |
coleenp@4037 | 443 | } |
coleenp@4037 | 444 | |
coleenp@4037 | 445 | // Clean the implementors list and method data. |
coleenp@4037 | 446 | if (current->oop_is_instance()) { |
coleenp@4037 | 447 | InstanceKlass* ik = InstanceKlass::cast(current); |
coleenp@4037 | 448 | ik->clean_implementors_list(is_alive); |
coleenp@4037 | 449 | ik->clean_method_data(is_alive); |
duke@435 | 450 | } |
duke@435 | 451 | } |
duke@435 | 452 | } |
duke@435 | 453 | |
coleenp@4037 | 454 | void Klass::klass_update_barrier_set(oop v) { |
coleenp@4037 | 455 | record_modified_oops(); |
coleenp@4037 | 456 | } |
coleenp@4037 | 457 | |
coleenp@4037 | 458 | void Klass::klass_update_barrier_set_pre(void* p, oop v) { |
coleenp@4037 | 459 | // This barrier used by G1, where it's used remember the old oop values, |
coleenp@4037 | 460 | // so that we don't forget any objects that were live at the snapshot at |
coleenp@4037 | 461 | // the beginning. This function is only used when we write oops into |
coleenp@4037 | 462 | // Klasses. Since the Klasses are used as roots in G1, we don't have to |
coleenp@4037 | 463 | // do anything here. |
coleenp@4037 | 464 | } |
coleenp@4037 | 465 | |
coleenp@4037 | 466 | void Klass::klass_oop_store(oop* p, oop v) { |
coleenp@4037 | 467 | assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata"); |
coleenp@4037 | 468 | assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object"); |
coleenp@4037 | 469 | |
coleenp@4037 | 470 | // do the store |
coleenp@4037 | 471 | if (always_do_update_barrier) { |
coleenp@4037 | 472 | klass_oop_store((volatile oop*)p, v); |
coleenp@4037 | 473 | } else { |
coleenp@4037 | 474 | klass_update_barrier_set_pre((void*)p, v); |
coleenp@4037 | 475 | *p = v; |
coleenp@4037 | 476 | klass_update_barrier_set(v); |
coleenp@4037 | 477 | } |
coleenp@4037 | 478 | } |
coleenp@4037 | 479 | |
coleenp@4037 | 480 | void Klass::klass_oop_store(volatile oop* p, oop v) { |
coleenp@4037 | 481 | assert(!Universe::heap()->is_in_reserved((void*)p), "Should store pointer into metadata"); |
coleenp@4037 | 482 | assert(v == NULL || Universe::heap()->is_in_reserved((void*)v), "Should store pointer to an object"); |
coleenp@4037 | 483 | |
coleenp@4037 | 484 | klass_update_barrier_set_pre((void*)p, v); |
coleenp@4037 | 485 | OrderAccess::release_store_ptr(p, v); |
coleenp@4037 | 486 | klass_update_barrier_set(v); |
coleenp@4037 | 487 | } |
coleenp@4037 | 488 | |
coleenp@4037 | 489 | void Klass::oops_do(OopClosure* cl) { |
coleenp@4037 | 490 | cl->do_oop(&_java_mirror); |
coleenp@4037 | 491 | } |
duke@435 | 492 | |
duke@435 | 493 | void Klass::remove_unshareable_info() { |
duke@435 | 494 | set_subklass(NULL); |
duke@435 | 495 | set_next_sibling(NULL); |
coleenp@4037 | 496 | // Clear the java mirror |
coleenp@4037 | 497 | set_java_mirror(NULL); |
coleenp@4037 | 498 | set_next_link(NULL); |
coleenp@4037 | 499 | |
coleenp@4037 | 500 | // Null out class_loader_data because we don't share that yet. |
coleenp@4037 | 501 | set_class_loader_data(NULL); |
duke@435 | 502 | } |
duke@435 | 503 | |
coleenp@4037 | 504 | void Klass::restore_unshareable_info(TRAPS) { |
coleenp@4037 | 505 | ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); |
coleenp@4037 | 506 | // Restore class_loader_data to the null class loader data |
coleenp@4037 | 507 | set_class_loader_data(loader_data); |
duke@435 | 508 | |
coleenp@4037 | 509 | // Add to null class loader list first before creating the mirror |
coleenp@4037 | 510 | // (same order as class file parsing) |
coleenp@4037 | 511 | loader_data->add_class(this); |
coleenp@4037 | 512 | |
coleenp@4037 | 513 | // Recreate the class mirror |
coleenp@4037 | 514 | java_lang_Class::create_mirror(this, CHECK); |
coleenp@2497 | 515 | } |
coleenp@2497 | 516 | |
coleenp@4037 | 517 | Klass* Klass::array_klass_or_null(int rank) { |
duke@435 | 518 | EXCEPTION_MARK; |
duke@435 | 519 | // No exception can be thrown by array_klass_impl when called with or_null == true. |
duke@435 | 520 | // (In anycase, the execption mark will fail if it do so) |
duke@435 | 521 | return array_klass_impl(true, rank, THREAD); |
duke@435 | 522 | } |
duke@435 | 523 | |
duke@435 | 524 | |
coleenp@4037 | 525 | Klass* Klass::array_klass_or_null() { |
duke@435 | 526 | EXCEPTION_MARK; |
duke@435 | 527 | // No exception can be thrown by array_klass_impl when called with or_null == true. |
duke@435 | 528 | // (In anycase, the execption mark will fail if it do so) |
duke@435 | 529 | return array_klass_impl(true, THREAD); |
duke@435 | 530 | } |
duke@435 | 531 | |
duke@435 | 532 | |
coleenp@4037 | 533 | Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) { |
coleenp@4142 | 534 | fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); |
duke@435 | 535 | return NULL; |
duke@435 | 536 | } |
duke@435 | 537 | |
duke@435 | 538 | |
coleenp@4037 | 539 | Klass* Klass::array_klass_impl(bool or_null, TRAPS) { |
coleenp@4142 | 540 | fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); |
duke@435 | 541 | return NULL; |
duke@435 | 542 | } |
duke@435 | 543 | |
duke@435 | 544 | |
coleenp@4037 | 545 | void Klass::with_array_klasses_do(void f(Klass* k)) { |
coleenp@4037 | 546 | f(this); |
duke@435 | 547 | } |
duke@435 | 548 | |
duke@435 | 549 | |
coleenp@4037 | 550 | oop Klass::class_loader() const { return class_loader_data()->class_loader(); } |
coleenp@4037 | 551 | |
duke@435 | 552 | const char* Klass::external_name() const { |
jrose@866 | 553 | if (oop_is_instance()) { |
coleenp@4037 | 554 | InstanceKlass* ik = (InstanceKlass*) this; |
jrose@866 | 555 | if (ik->is_anonymous()) { |
twisti@2698 | 556 | assert(EnableInvokeDynamic, ""); |
jrose@866 | 557 | intptr_t hash = ik->java_mirror()->identity_hash(); |
jrose@866 | 558 | char hash_buf[40]; |
jrose@866 | 559 | sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash); |
jrose@866 | 560 | size_t hash_len = strlen(hash_buf); |
jrose@866 | 561 | |
jrose@866 | 562 | size_t result_len = name()->utf8_length(); |
jrose@866 | 563 | char* result = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1); |
jrose@866 | 564 | name()->as_klass_external_name(result, (int) result_len + 1); |
jrose@866 | 565 | assert(strlen(result) == result_len, ""); |
jrose@866 | 566 | strcpy(result + result_len, hash_buf); |
jrose@866 | 567 | assert(strlen(result) == result_len + hash_len, ""); |
jrose@866 | 568 | return result; |
jrose@866 | 569 | } |
jrose@866 | 570 | } |
jrose@1474 | 571 | if (name() == NULL) return "<unknown>"; |
duke@435 | 572 | return name()->as_klass_external_name(); |
duke@435 | 573 | } |
duke@435 | 574 | |
duke@435 | 575 | |
jrose@1474 | 576 | const char* Klass::signature_name() const { |
jrose@1474 | 577 | if (name() == NULL) return "<unknown>"; |
duke@435 | 578 | return name()->as_C_string(); |
duke@435 | 579 | } |
duke@435 | 580 | |
duke@435 | 581 | // Unless overridden, modifier_flags is 0. |
duke@435 | 582 | jint Klass::compute_modifier_flags(TRAPS) const { |
duke@435 | 583 | return 0; |
duke@435 | 584 | } |
duke@435 | 585 | |
duke@435 | 586 | int Klass::atomic_incr_biased_lock_revocation_count() { |
duke@435 | 587 | return (int) Atomic::add(1, &_biased_lock_revocation_count); |
duke@435 | 588 | } |
duke@435 | 589 | |
duke@435 | 590 | // Unless overridden, jvmti_class_status has no flags set. |
duke@435 | 591 | jint Klass::jvmti_class_status() const { |
duke@435 | 592 | return 0; |
duke@435 | 593 | } |
duke@435 | 594 | |
coleenp@4037 | 595 | |
duke@435 | 596 | // Printing |
duke@435 | 597 | |
coleenp@4037 | 598 | void Klass::print_on(outputStream* st) const { |
coleenp@4037 | 599 | ResourceMark rm; |
coleenp@4037 | 600 | // print title |
coleenp@4037 | 601 | st->print("%s", internal_name()); |
coleenp@4037 | 602 | print_address_on(st); |
coleenp@4037 | 603 | st->cr(); |
coleenp@4037 | 604 | } |
coleenp@4037 | 605 | |
duke@435 | 606 | void Klass::oop_print_on(oop obj, outputStream* st) { |
duke@435 | 607 | ResourceMark rm; |
duke@435 | 608 | // print title |
duke@435 | 609 | st->print_cr("%s ", internal_name()); |
duke@435 | 610 | obj->print_address_on(st); |
duke@435 | 611 | |
duke@435 | 612 | if (WizardMode) { |
duke@435 | 613 | // print header |
duke@435 | 614 | obj->mark()->print_on(st); |
duke@435 | 615 | } |
duke@435 | 616 | |
duke@435 | 617 | // print class |
duke@435 | 618 | st->print(" - klass: "); |
duke@435 | 619 | obj->klass()->print_value_on(st); |
duke@435 | 620 | st->cr(); |
duke@435 | 621 | } |
duke@435 | 622 | |
duke@435 | 623 | void Klass::oop_print_value_on(oop obj, outputStream* st) { |
duke@435 | 624 | // print title |
duke@435 | 625 | ResourceMark rm; // Cannot print in debug mode without this |
duke@435 | 626 | st->print("%s", internal_name()); |
duke@435 | 627 | obj->print_address_on(st); |
duke@435 | 628 | } |
duke@435 | 629 | |
coleenp@4037 | 630 | |
duke@435 | 631 | // Verification |
duke@435 | 632 | |
coleenp@4037 | 633 | void Klass::verify_on(outputStream* st) { |
coleenp@4037 | 634 | guarantee(!Universe::heap()->is_in_reserved(this), "Shouldn't be"); |
coleenp@4037 | 635 | guarantee(this->is_metadata(), "should be in metaspace"); |
coleenp@4037 | 636 | |
coleenp@4037 | 637 | assert(ClassLoaderDataGraph::contains((address)this), "Should be"); |
coleenp@4037 | 638 | |
coleenp@4037 | 639 | guarantee(this->is_klass(),"should be klass"); |
coleenp@4037 | 640 | |
coleenp@4037 | 641 | if (super() != NULL) { |
coleenp@4037 | 642 | guarantee(super()->is_metadata(), "should be in metaspace"); |
coleenp@4037 | 643 | guarantee(super()->is_klass(), "should be klass"); |
coleenp@4037 | 644 | } |
coleenp@4037 | 645 | if (secondary_super_cache() != NULL) { |
coleenp@4037 | 646 | Klass* ko = secondary_super_cache(); |
coleenp@4037 | 647 | guarantee(ko->is_metadata(), "should be in metaspace"); |
coleenp@4037 | 648 | guarantee(ko->is_klass(), "should be klass"); |
coleenp@4037 | 649 | } |
coleenp@4037 | 650 | for ( uint i = 0; i < primary_super_limit(); i++ ) { |
coleenp@4037 | 651 | Klass* ko = _primary_supers[i]; |
coleenp@4037 | 652 | if (ko != NULL) { |
coleenp@4037 | 653 | guarantee(ko->is_metadata(), "should be in metaspace"); |
coleenp@4037 | 654 | guarantee(ko->is_klass(), "should be klass"); |
coleenp@4037 | 655 | } |
coleenp@4037 | 656 | } |
coleenp@4037 | 657 | |
coleenp@4037 | 658 | if (java_mirror() != NULL) { |
coleenp@4037 | 659 | guarantee(java_mirror()->is_oop(), "should be instance"); |
coleenp@4037 | 660 | } |
coleenp@4037 | 661 | } |
coleenp@4037 | 662 | |
duke@435 | 663 | void Klass::oop_verify_on(oop obj, outputStream* st) { |
duke@435 | 664 | guarantee(obj->is_oop(), "should be oop"); |
coleenp@4037 | 665 | guarantee(obj->klass()->is_metadata(), "should not be in Java heap"); |
duke@435 | 666 | guarantee(obj->klass()->is_klass(), "klass field is not a klass"); |
duke@435 | 667 | } |
duke@435 | 668 | |
duke@435 | 669 | #ifndef PRODUCT |
duke@435 | 670 | |
duke@435 | 671 | void Klass::verify_vtable_index(int i) { |
duke@435 | 672 | if (oop_is_instance()) { |
coleenp@4037 | 673 | assert(i>=0 && i<((InstanceKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); |
duke@435 | 674 | } else { |
coleenp@4037 | 675 | assert(oop_is_array(), "Must be"); |
coleenp@4142 | 676 | assert(i>=0 && i<((ArrayKlass*)this)->vtable_length()/vtableEntry::size(), "index out of bounds"); |
duke@435 | 677 | } |
duke@435 | 678 | } |
duke@435 | 679 | |
duke@435 | 680 | #endif |