| 1 | /* |
| 2 | * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "precompiled.hpp" |
| 26 | #include "classfile/bytecodeAssembler.hpp" |
| 27 | #include "classfile/defaultMethods.hpp" |
| 28 | #include "classfile/symbolTable.hpp" |
| 29 | #include "classfile/systemDictionary.hpp" |
| 30 | #include "logging/log.hpp" |
| 31 | #include "logging/logStream.hpp" |
| 32 | #include "memory/allocation.hpp" |
| 33 | #include "memory/metadataFactory.hpp" |
| 34 | #include "memory/resourceArea.hpp" |
| 35 | #include "memory/universe.hpp" |
| 36 | #include "runtime/handles.inline.hpp" |
| 37 | #include "runtime/signature.hpp" |
| 38 | #include "runtime/thread.hpp" |
| 39 | #include "oops/instanceKlass.hpp" |
| 40 | #include "oops/klass.hpp" |
| 41 | #include "oops/method.hpp" |
| 42 | #include "utilities/accessFlags.hpp" |
| 43 | #include "utilities/exceptions.hpp" |
| 44 | #include "utilities/ostream.hpp" |
| 45 | #include "utilities/pair.hpp" |
| 46 | #include "utilities/resourceHash.hpp" |
| 47 | |
| 48 | typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState; |
| 49 | |
| 50 | // Because we use an iterative algorithm when iterating over the type |
| 51 | // hierarchy, we can't use traditional scoped objects which automatically do |
| 52 | // cleanup in the destructor when the scope is exited. PseudoScope (and |
| 53 | // PseudoScopeMark) provides a similar functionality, but for when you want a |
| 54 | // scoped object in non-stack memory (such as in resource memory, as we do |
| 55 | // here). You've just got to remember to call 'destroy()' on the scope when |
| 56 | // leaving it (and marks have to be explicitly added). |
| 57 | class PseudoScopeMark : public ResourceObj { |
| 58 | public: |
| 59 | virtual void destroy() = 0; |
| 60 | }; |
| 61 | |
| 62 | class PseudoScope : public ResourceObj { |
| 63 | private: |
| 64 | GrowableArray<PseudoScopeMark*> _marks; |
| 65 | public: |
| 66 | |
| 67 | static PseudoScope* cast(void* data) { |
| 68 | return static_cast<PseudoScope*>(data); |
| 69 | } |
| 70 | |
| 71 | void add_mark(PseudoScopeMark* psm) { |
| 72 | _marks.append(psm); |
| 73 | } |
| 74 | |
| 75 | void destroy() { |
| 76 | for (int i = 0; i < _marks.length(); ++i) { |
| 77 | _marks.at(i)->destroy(); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | static void print_slot(outputStream* str, Symbol* name, Symbol* signature) { |
| 83 | str->print("%s%s" , name->as_C_string(), signature->as_C_string()); |
| 84 | } |
| 85 | |
| 86 | static void print_method(outputStream* str, Method* mo, bool with_class=true) { |
| 87 | if (with_class) { |
| 88 | str->print("%s." , mo->klass_name()->as_C_string()); |
| 89 | } |
| 90 | print_slot(str, mo->name(), mo->signature()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Perform a depth-first iteration over the class hierarchy, applying |
| 95 | * algorithmic logic as it goes. |
| 96 | * |
| 97 | * This class is one half of the inheritance hierarchy analysis mechanism. |
| 98 | * It is meant to be used in conjunction with another class, the algorithm, |
| 99 | * which is indicated by the ALGO template parameter. This class can be |
| 100 | * paired with any algorithm class that provides the required methods. |
| 101 | * |
| 102 | * This class contains all the mechanics for iterating over the class hierarchy |
| 103 | * starting at a particular root, without recursing (thus limiting stack growth |
| 104 | * from this point). It visits each superclass (if present) and superinterface |
| 105 | * in a depth-first manner, with callbacks to the ALGO class as each class is |
| 106 | * encountered (visit()), The algorithm can cut-off further exploration of a |
| 107 | * particular branch by returning 'false' from a visit() call. |
| 108 | * |
| 109 | * The ALGO class, must provide a visit() method, which each of which will be |
| 110 | * called once for each node in the inheritance tree during the iteration. In |
| 111 | * addition, it can provide a memory block via new_node_data(InstanceKlass*), |
| 112 | * which it can use for node-specific storage (and access via the |
| 113 | * current_data() and data_at_depth(int) methods). |
| 114 | * |
| 115 | * Bare minimum needed to be an ALGO class: |
| 116 | * class Algo : public HierarchyVisitor<Algo> { |
| 117 | * void* new_node_data(InstanceKlass* cls) { return NULL; } |
| 118 | * void free_node_data(void* data) { return; } |
| 119 | * bool visit() { return true; } |
| 120 | * }; |
| 121 | */ |
| 122 | template <class ALGO> |
| 123 | class HierarchyVisitor : StackObj { |
| 124 | private: |
| 125 | |
| 126 | class Node : public ResourceObj { |
| 127 | public: |
| 128 | InstanceKlass* _class; |
| 129 | bool _super_was_visited; |
| 130 | int _interface_index; |
| 131 | void* _algorithm_data; |
| 132 | |
| 133 | Node(InstanceKlass* cls, void* data, bool visit_super) |
| 134 | : _class(cls), _super_was_visited(!visit_super), |
| 135 | _interface_index(0), _algorithm_data(data) {} |
| 136 | |
| 137 | int number_of_interfaces() { return _class->local_interfaces()->length(); } |
| 138 | int interface_index() { return _interface_index; } |
| 139 | void set_super_visited() { _super_was_visited = true; } |
| 140 | void increment_visited_interface() { ++_interface_index; } |
| 141 | void set_all_interfaces_visited() { |
| 142 | _interface_index = number_of_interfaces(); |
| 143 | } |
| 144 | bool has_visited_super() { return _super_was_visited; } |
| 145 | bool has_visited_all_interfaces() { |
| 146 | return interface_index() >= number_of_interfaces(); |
| 147 | } |
| 148 | InstanceKlass* interface_at(int index) { |
| 149 | return InstanceKlass::cast(_class->local_interfaces()->at(index)); |
| 150 | } |
| 151 | InstanceKlass* next_super() { return _class->java_super(); } |
| 152 | InstanceKlass* next_interface() { |
| 153 | return interface_at(interface_index()); |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | bool _visited_Object; |
| 158 | GrowableArray<Node*> _path; |
| 159 | |
| 160 | Node* current_top() const { return _path.top(); } |
| 161 | bool has_more_nodes() const { return !_path.is_empty(); } |
| 162 | void push(InstanceKlass* cls, void* data) { |
| 163 | assert(cls != NULL, "Requires a valid instance class" ); |
| 164 | Node* node = new Node(cls, data, has_super(cls)); |
| 165 | if (cls == SystemDictionary::Object_klass()) { |
| 166 | _visited_Object = true; |
| 167 | } |
| 168 | _path.push(node); |
| 169 | } |
| 170 | void pop() { _path.pop(); } |
| 171 | |
| 172 | // Since the starting point can be an interface, we must ensure we catch |
| 173 | // j.l.Object as the super once in those cases. The _visited_Object flag |
| 174 | // only ensures we don't then repeatedly enqueue Object for each interface |
| 175 | // in the class hierarchy. |
| 176 | bool has_super(InstanceKlass* cls) { |
| 177 | return cls->super() != NULL && (!_visited_Object || !cls->is_interface()); |
| 178 | } |
| 179 | |
| 180 | Node* node_at_depth(int i) const { |
| 181 | return (i >= _path.length()) ? NULL : _path.at(_path.length() - i - 1); |
| 182 | } |
| 183 | |
| 184 | protected: |
| 185 | |
| 186 | // Accessors available to the algorithm |
| 187 | int current_depth() const { return _path.length() - 1; } |
| 188 | |
| 189 | InstanceKlass* class_at_depth(int i) { |
| 190 | Node* n = node_at_depth(i); |
| 191 | return n == NULL ? NULL : n->_class; |
| 192 | } |
| 193 | InstanceKlass* current_class() { return class_at_depth(0); } |
| 194 | |
| 195 | void* data_at_depth(int i) { |
| 196 | Node* n = node_at_depth(i); |
| 197 | return n == NULL ? NULL : n->_algorithm_data; |
| 198 | } |
| 199 | void* current_data() { return data_at_depth(0); } |
| 200 | |
| 201 | public: |
| 202 | |
| 203 | void run(InstanceKlass* root) { |
| 204 | ALGO* algo = static_cast<ALGO*>(this); |
| 205 | |
| 206 | void* algo_data = algo->new_node_data(root); |
| 207 | push(root, algo_data); |
| 208 | bool top_needs_visit = true; |
| 209 | |
| 210 | do { |
| 211 | Node* top = current_top(); |
| 212 | if (top_needs_visit) { |
| 213 | if (algo->visit() == false) { |
| 214 | // algorithm does not want to continue along this path. Arrange |
| 215 | // it so that this state is immediately popped off the stack |
| 216 | top->set_super_visited(); |
| 217 | top->set_all_interfaces_visited(); |
| 218 | } |
| 219 | top_needs_visit = false; |
| 220 | } |
| 221 | |
| 222 | if (top->has_visited_super() && top->has_visited_all_interfaces()) { |
| 223 | algo->free_node_data(top->_algorithm_data); |
| 224 | pop(); |
| 225 | } else { |
| 226 | InstanceKlass* next = NULL; |
| 227 | if (top->has_visited_super() == false) { |
| 228 | next = top->next_super(); |
| 229 | top->set_super_visited(); |
| 230 | } else { |
| 231 | next = top->next_interface(); |
| 232 | top->increment_visited_interface(); |
| 233 | } |
| 234 | assert(next != NULL, "Otherwise we shouldn't be here" ); |
| 235 | algo_data = algo->new_node_data(next); |
| 236 | push(next, algo_data); |
| 237 | top_needs_visit = true; |
| 238 | } |
| 239 | } while (has_more_nodes()); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | class PrintHierarchy : public HierarchyVisitor<PrintHierarchy> { |
| 244 | private: |
| 245 | outputStream* _st; |
| 246 | public: |
| 247 | bool visit() { |
| 248 | InstanceKlass* cls = current_class(); |
| 249 | streamIndentor si(_st, current_depth() * 2); |
| 250 | _st->indent().print_cr("%s" , cls->name()->as_C_string()); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | void* new_node_data(InstanceKlass* cls) { return NULL; } |
| 255 | void free_node_data(void* data) { return; } |
| 256 | |
| 257 | PrintHierarchy(outputStream* st = tty) : _st(st) {} |
| 258 | }; |
| 259 | |
| 260 | // Used to register InstanceKlass objects and all related metadata structures |
| 261 | // (Methods, ConstantPools) as "in-use" by the current thread so that they can't |
| 262 | // be deallocated by class redefinition while we're using them. The classes are |
| 263 | // de-registered when this goes out of scope. |
| 264 | // |
| 265 | // Once a class is registered, we need not bother with methodHandles or |
| 266 | // constantPoolHandles for it's associated metadata. |
| 267 | class KeepAliveRegistrar : public StackObj { |
| 268 | private: |
| 269 | Thread* _thread; |
| 270 | GrowableArray<ConstantPool*> _keep_alive; |
| 271 | |
| 272 | public: |
| 273 | KeepAliveRegistrar(Thread* thread) : _thread(thread), _keep_alive(20) { |
| 274 | assert(thread == Thread::current(), "Must be current thread" ); |
| 275 | } |
| 276 | |
| 277 | ~KeepAliveRegistrar() { |
| 278 | for (int i = _keep_alive.length() - 1; i >= 0; --i) { |
| 279 | ConstantPool* cp = _keep_alive.at(i); |
| 280 | int idx = _thread->metadata_handles()->find_from_end(cp); |
| 281 | assert(idx > 0, "Must be in the list" ); |
| 282 | _thread->metadata_handles()->remove_at(idx); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Register a class as 'in-use' by the thread. It's fine to register a class |
| 287 | // multiple times (though perhaps inefficient) |
| 288 | void register_class(InstanceKlass* ik) { |
| 289 | ConstantPool* cp = ik->constants(); |
| 290 | _keep_alive.push(cp); |
| 291 | _thread->metadata_handles()->push(cp); |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | class KeepAliveVisitor : public HierarchyVisitor<KeepAliveVisitor> { |
| 296 | private: |
| 297 | KeepAliveRegistrar* _registrar; |
| 298 | |
| 299 | public: |
| 300 | KeepAliveVisitor(KeepAliveRegistrar* registrar) : _registrar(registrar) {} |
| 301 | |
| 302 | void* new_node_data(InstanceKlass* cls) { return NULL; } |
| 303 | void free_node_data(void* data) { return; } |
| 304 | |
| 305 | bool visit() { |
| 306 | _registrar->register_class(current_class()); |
| 307 | return true; |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | |
| 312 | // A method family contains a set of all methods that implement a single |
| 313 | // erased method. As members of the set are collected while walking over the |
| 314 | // hierarchy, they are tagged with a qualification state. The qualification |
| 315 | // state for an erased method is set to disqualified if there exists a path |
| 316 | // from the root of hierarchy to the method that contains an interleaving |
| 317 | // erased method defined in an interface. |
| 318 | |
| 319 | class MethodFamily : public ResourceObj { |
| 320 | private: |
| 321 | |
| 322 | GrowableArray<Pair<Method*,QualifiedState> > _members; |
| 323 | ResourceHashtable<Method*, int> _member_index; |
| 324 | |
| 325 | Method* _selected_target; // Filled in later, if a unique target exists |
| 326 | Symbol* _exception_message; // If no unique target is found |
| 327 | Symbol* _exception_name; // If no unique target is found |
| 328 | |
| 329 | bool contains_method(Method* method) { |
| 330 | int* lookup = _member_index.get(method); |
| 331 | return lookup != NULL; |
| 332 | } |
| 333 | |
| 334 | void add_method(Method* method, QualifiedState state) { |
| 335 | Pair<Method*,QualifiedState> entry(method, state); |
| 336 | _member_index.put(method, _members.length()); |
| 337 | _members.append(entry); |
| 338 | } |
| 339 | |
| 340 | void disqualify_method(Method* method) { |
| 341 | int* index = _member_index.get(method); |
| 342 | guarantee(index != NULL && *index >= 0 && *index < _members.length(), "bad index" ); |
| 343 | _members.at(*index).second = DISQUALIFIED; |
| 344 | } |
| 345 | |
| 346 | Symbol* generate_no_defaults_message(TRAPS) const; |
| 347 | Symbol* generate_method_message(Symbol *klass_name, Method* method, TRAPS) const; |
| 348 | Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const; |
| 349 | |
| 350 | public: |
| 351 | |
| 352 | MethodFamily() |
| 353 | : _selected_target(NULL), _exception_message(NULL), _exception_name(NULL) {} |
| 354 | |
| 355 | void set_target_if_empty(Method* m) { |
| 356 | if (_selected_target == NULL && !m->is_overpass()) { |
| 357 | _selected_target = m; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void record_qualified_method(Method* m) { |
| 362 | // If the method already exists in the set as qualified, this operation is |
| 363 | // redundant. If it already exists as disqualified, then we leave it as |
| 364 | // disqualfied. Thus we only add to the set if it's not already in the |
| 365 | // set. |
| 366 | if (!contains_method(m)) { |
| 367 | add_method(m, QUALIFIED); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void record_disqualified_method(Method* m) { |
| 372 | // If not in the set, add it as disqualified. If it's already in the set, |
| 373 | // then set the state to disqualified no matter what the previous state was. |
| 374 | if (!contains_method(m)) { |
| 375 | add_method(m, DISQUALIFIED); |
| 376 | } else { |
| 377 | disqualify_method(m); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | bool has_target() const { return _selected_target != NULL; } |
| 382 | bool throws_exception() { return _exception_message != NULL; } |
| 383 | |
| 384 | Method* get_selected_target() { return _selected_target; } |
| 385 | Symbol* get_exception_message() { return _exception_message; } |
| 386 | Symbol* get_exception_name() { return _exception_name; } |
| 387 | |
| 388 | // Either sets the target or the exception error message |
| 389 | void determine_target(InstanceKlass* root, TRAPS) { |
| 390 | if (has_target() || throws_exception()) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // Qualified methods are maximally-specific methods |
| 395 | // These include public, instance concrete (=default) and abstract methods |
| 396 | GrowableArray<Method*> qualified_methods; |
| 397 | int num_defaults = 0; |
| 398 | int default_index = -1; |
| 399 | int qualified_index = -1; |
| 400 | for (int i = 0; i < _members.length(); ++i) { |
| 401 | Pair<Method*,QualifiedState> entry = _members.at(i); |
| 402 | if (entry.second == QUALIFIED) { |
| 403 | qualified_methods.append(entry.first); |
| 404 | qualified_index++; |
| 405 | if (entry.first->is_default_method()) { |
| 406 | num_defaults++; |
| 407 | default_index = qualified_index; |
| 408 | |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (num_defaults == 0) { |
| 414 | // If the root klass has a static method with matching name and signature |
| 415 | // then do not generate an overpass method because it will hide the |
| 416 | // static method during resolution. |
| 417 | if (qualified_methods.length() == 0) { |
| 418 | _exception_message = generate_no_defaults_message(CHECK); |
| 419 | } else { |
| 420 | assert(root != NULL, "Null root class" ); |
| 421 | _exception_message = generate_method_message(root->name(), qualified_methods.at(0), CHECK); |
| 422 | } |
| 423 | _exception_name = vmSymbols::java_lang_AbstractMethodError(); |
| 424 | |
| 425 | // If only one qualified method is default, select that |
| 426 | } else if (num_defaults == 1) { |
| 427 | _selected_target = qualified_methods.at(default_index); |
| 428 | |
| 429 | } else if (num_defaults > 1) { |
| 430 | _exception_message = generate_conflicts_message(&qualified_methods,CHECK); |
| 431 | _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError(); |
| 432 | LogTarget(Debug, defaultmethods) lt; |
| 433 | if (lt.is_enabled()) { |
| 434 | LogStream ls(lt); |
| 435 | _exception_message->print_value_on(&ls); |
| 436 | ls.cr(); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void print_selected(outputStream* str, int indent) const { |
| 442 | assert(has_target(), "Should be called otherwise" ); |
| 443 | streamIndentor si(str, indent * 2); |
| 444 | str->indent().print("Selected method: " ); |
| 445 | print_method(str, _selected_target); |
| 446 | Klass* method_holder = _selected_target->method_holder(); |
| 447 | if (!method_holder->is_interface()) { |
| 448 | str->print(" : in superclass" ); |
| 449 | } |
| 450 | str->cr(); |
| 451 | } |
| 452 | |
| 453 | void print_exception(outputStream* str, int indent) { |
| 454 | assert(throws_exception(), "Should be called otherwise" ); |
| 455 | assert(_exception_name != NULL, "exception_name should be set" ); |
| 456 | streamIndentor si(str, indent * 2); |
| 457 | str->indent().print_cr("%s: %s" , _exception_name->as_C_string(), _exception_message->as_C_string()); |
| 458 | } |
| 459 | }; |
| 460 | |
| 461 | Symbol* MethodFamily::generate_no_defaults_message(TRAPS) const { |
| 462 | return SymbolTable::new_symbol("No qualifying defaults found" ); |
| 463 | } |
| 464 | |
| 465 | Symbol* MethodFamily::generate_method_message(Symbol *klass_name, Method* method, TRAPS) const { |
| 466 | stringStream ss; |
| 467 | ss.print("Method " ); |
| 468 | Symbol* name = method->name(); |
| 469 | Symbol* signature = method->signature(); |
| 470 | ss.write((const char*)klass_name->bytes(), klass_name->utf8_length()); |
| 471 | ss.print("." ); |
| 472 | ss.write((const char*)name->bytes(), name->utf8_length()); |
| 473 | ss.write((const char*)signature->bytes(), signature->utf8_length()); |
| 474 | ss.print(" is abstract" ); |
| 475 | return SymbolTable::new_symbol(ss.base(), (int)ss.size()); |
| 476 | } |
| 477 | |
| 478 | Symbol* MethodFamily::generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const { |
| 479 | stringStream ss; |
| 480 | ss.print("Conflicting default methods:" ); |
| 481 | for (int i = 0; i < methods->length(); ++i) { |
| 482 | Method* method = methods->at(i); |
| 483 | Symbol* klass = method->klass_name(); |
| 484 | Symbol* name = method->name(); |
| 485 | ss.print(" " ); |
| 486 | ss.write((const char*)klass->bytes(), klass->utf8_length()); |
| 487 | ss.print("." ); |
| 488 | ss.write((const char*)name->bytes(), name->utf8_length()); |
| 489 | } |
| 490 | return SymbolTable::new_symbol(ss.base(), (int)ss.size()); |
| 491 | } |
| 492 | |
| 493 | |
| 494 | class StateRestorer; |
| 495 | |
| 496 | // StatefulMethodFamily is a wrapper around a MethodFamily that maintains the |
| 497 | // qualification state during hierarchy visitation, and applies that state |
| 498 | // when adding members to the MethodFamily |
| 499 | class StatefulMethodFamily : public ResourceObj { |
| 500 | friend class StateRestorer; |
| 501 | private: |
| 502 | QualifiedState _qualification_state; |
| 503 | |
| 504 | void set_qualification_state(QualifiedState state) { |
| 505 | _qualification_state = state; |
| 506 | } |
| 507 | |
| 508 | protected: |
| 509 | MethodFamily _method_family; |
| 510 | |
| 511 | public: |
| 512 | StatefulMethodFamily() { |
| 513 | _qualification_state = QUALIFIED; |
| 514 | } |
| 515 | |
| 516 | void set_target_if_empty(Method* m) { _method_family.set_target_if_empty(m); } |
| 517 | |
| 518 | MethodFamily* get_method_family() { return &_method_family; } |
| 519 | |
| 520 | StateRestorer* record_method_and_dq_further(Method* mo); |
| 521 | }; |
| 522 | |
| 523 | class StateRestorer : public PseudoScopeMark { |
| 524 | private: |
| 525 | StatefulMethodFamily* _method; |
| 526 | QualifiedState _state_to_restore; |
| 527 | public: |
| 528 | StateRestorer(StatefulMethodFamily* dm, QualifiedState state) |
| 529 | : _method(dm), _state_to_restore(state) {} |
| 530 | ~StateRestorer() { destroy(); } |
| 531 | void restore_state() { _method->set_qualification_state(_state_to_restore); } |
| 532 | virtual void destroy() { restore_state(); } |
| 533 | }; |
| 534 | |
| 535 | StateRestorer* StatefulMethodFamily::record_method_and_dq_further(Method* mo) { |
| 536 | StateRestorer* mark = new StateRestorer(this, _qualification_state); |
| 537 | if (_qualification_state == QUALIFIED) { |
| 538 | _method_family.record_qualified_method(mo); |
| 539 | } else { |
| 540 | _method_family.record_disqualified_method(mo); |
| 541 | } |
| 542 | // Everything found "above"??? this method in the hierarchy walk is set to |
| 543 | // disqualified |
| 544 | set_qualification_state(DISQUALIFIED); |
| 545 | return mark; |
| 546 | } |
| 547 | |
| 548 | // Represents a location corresponding to a vtable slot for methods that |
| 549 | // neither the class nor any of it's ancestors provide an implementaion. |
| 550 | // Default methods may be present to fill this slot. |
| 551 | class EmptyVtableSlot : public ResourceObj { |
| 552 | private: |
| 553 | Symbol* _name; |
| 554 | Symbol* _signature; |
| 555 | int _size_of_parameters; |
| 556 | MethodFamily* _binding; |
| 557 | |
| 558 | public: |
| 559 | EmptyVtableSlot(Method* method) |
| 560 | : _name(method->name()), _signature(method->signature()), |
| 561 | _size_of_parameters(method->size_of_parameters()), _binding(NULL) {} |
| 562 | |
| 563 | Symbol* name() const { return _name; } |
| 564 | Symbol* signature() const { return _signature; } |
| 565 | int size_of_parameters() const { return _size_of_parameters; } |
| 566 | |
| 567 | void bind_family(MethodFamily* lm) { _binding = lm; } |
| 568 | bool is_bound() { return _binding != NULL; } |
| 569 | MethodFamily* get_binding() { return _binding; } |
| 570 | |
| 571 | void print_on(outputStream* str) const { |
| 572 | print_slot(str, name(), signature()); |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | static bool already_in_vtable_slots(GrowableArray<EmptyVtableSlot*>* slots, Method* m) { |
| 577 | bool found = false; |
| 578 | for (int j = 0; j < slots->length(); ++j) { |
| 579 | if (slots->at(j)->name() == m->name() && |
| 580 | slots->at(j)->signature() == m->signature() ) { |
| 581 | found = true; |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | return found; |
| 586 | } |
| 587 | |
| 588 | static void find_empty_vtable_slots(GrowableArray<EmptyVtableSlot*>* slots, |
| 589 | InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) { |
| 590 | |
| 591 | assert(klass != NULL, "Must be valid class" ); |
| 592 | |
| 593 | // All miranda methods are obvious candidates |
| 594 | for (int i = 0; i < mirandas->length(); ++i) { |
| 595 | Method* m = mirandas->at(i); |
| 596 | if (!already_in_vtable_slots(slots, m)) { |
| 597 | slots->append(new EmptyVtableSlot(m)); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Also any overpasses in our superclasses, that we haven't implemented. |
| 602 | // (can't use the vtable because it is not guaranteed to be initialized yet) |
| 603 | InstanceKlass* super = klass->java_super(); |
| 604 | while (super != NULL) { |
| 605 | for (int i = 0; i < super->methods()->length(); ++i) { |
| 606 | Method* m = super->methods()->at(i); |
| 607 | if (m->is_overpass() || m->is_static()) { |
| 608 | // m is a method that would have been a miranda if not for the |
| 609 | // default method processing that occurred on behalf of our superclass, |
| 610 | // so it's a method we want to re-examine in this new context. That is, |
| 611 | // unless we have a real implementation of it in the current class. |
| 612 | if (!already_in_vtable_slots(slots, m)) { |
| 613 | Method *impl = klass->lookup_method(m->name(), m->signature()); |
| 614 | if (impl == NULL || impl->is_overpass() || impl->is_static()) { |
| 615 | slots->append(new EmptyVtableSlot(m)); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // also any default methods in our superclasses |
| 622 | if (super->default_methods() != NULL) { |
| 623 | for (int i = 0; i < super->default_methods()->length(); ++i) { |
| 624 | Method* m = super->default_methods()->at(i); |
| 625 | // m is a method that would have been a miranda if not for the |
| 626 | // default method processing that occurred on behalf of our superclass, |
| 627 | // so it's a method we want to re-examine in this new context. That is, |
| 628 | // unless we have a real implementation of it in the current class. |
| 629 | if (!already_in_vtable_slots(slots, m)) { |
| 630 | Method* impl = klass->lookup_method(m->name(), m->signature()); |
| 631 | if (impl == NULL || impl->is_overpass() || impl->is_static()) { |
| 632 | slots->append(new EmptyVtableSlot(m)); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | super = super->java_super(); |
| 638 | } |
| 639 | |
| 640 | LogTarget(Debug, defaultmethods) lt; |
| 641 | if (lt.is_enabled()) { |
| 642 | lt.print("Slots that need filling:" ); |
| 643 | ResourceMark rm; |
| 644 | LogStream ls(lt); |
| 645 | streamIndentor si(&ls); |
| 646 | for (int i = 0; i < slots->length(); ++i) { |
| 647 | ls.indent(); |
| 648 | slots->at(i)->print_on(&ls); |
| 649 | ls.cr(); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // Iterates over the superinterface type hierarchy looking for all methods |
| 655 | // with a specific erased signature. |
| 656 | class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> { |
| 657 | private: |
| 658 | // Context data |
| 659 | Symbol* _method_name; |
| 660 | Symbol* _method_signature; |
| 661 | StatefulMethodFamily* _family; |
| 662 | bool _cur_class_is_interface; |
| 663 | |
| 664 | public: |
| 665 | FindMethodsByErasedSig(Symbol* name, Symbol* signature, bool is_interf) : |
| 666 | _method_name(name), _method_signature(signature), _family(NULL), |
| 667 | _cur_class_is_interface(is_interf) {} |
| 668 | |
| 669 | void get_discovered_family(MethodFamily** family) { |
| 670 | if (_family != NULL) { |
| 671 | *family = _family->get_method_family(); |
| 672 | } else { |
| 673 | *family = NULL; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); } |
| 678 | void free_node_data(void* node_data) { |
| 679 | PseudoScope::cast(node_data)->destroy(); |
| 680 | } |
| 681 | |
| 682 | // Find all methods on this hierarchy that match this |
| 683 | // method's erased (name, signature) |
| 684 | bool visit() { |
| 685 | PseudoScope* scope = PseudoScope::cast(current_data()); |
| 686 | InstanceKlass* iklass = current_class(); |
| 687 | |
| 688 | Method* m = iklass->find_method(_method_name, _method_signature); |
| 689 | // Private interface methods are not candidates for default methods. |
| 690 | // invokespecial to private interface methods doesn't use default method logic. |
| 691 | // Private class methods are not candidates for default methods. |
| 692 | // Private methods do not override default methods, so need to perform |
| 693 | // default method inheritance without including private methods. |
| 694 | // The overpasses are your supertypes' errors, we do not include them. |
| 695 | // Non-public methods in java.lang.Object are not candidates for default |
| 696 | // methods. |
| 697 | // Future: take access controls into account for superclass methods |
| 698 | if (m != NULL && !m->is_static() && !m->is_overpass() && !m->is_private() && |
| 699 | (!_cur_class_is_interface || !SystemDictionary::is_nonpublic_Object_method(m))) { |
| 700 | if (_family == NULL) { |
| 701 | _family = new StatefulMethodFamily(); |
| 702 | } |
| 703 | |
| 704 | if (iklass->is_interface()) { |
| 705 | StateRestorer* restorer = _family->record_method_and_dq_further(m); |
| 706 | scope->add_mark(restorer); |
| 707 | } else { |
| 708 | // This is the rule that methods in classes "win" (bad word) over |
| 709 | // methods in interfaces. This works because of single inheritance. |
| 710 | // Private methods in classes do not "win", they will be found |
| 711 | // first on searching, but overriding for invokevirtual needs |
| 712 | // to find default method candidates for the same signature |
| 713 | _family->set_target_if_empty(m); |
| 714 | } |
| 715 | } |
| 716 | return true; |
| 717 | } |
| 718 | |
| 719 | }; |
| 720 | |
| 721 | |
| 722 | |
| 723 | static void create_defaults_and_exceptions( |
| 724 | GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS); |
| 725 | |
| 726 | static void generate_erased_defaults( |
| 727 | InstanceKlass* klass, EmptyVtableSlot* slot, bool is_intf, TRAPS) { |
| 728 | |
| 729 | // sets up a set of methods with the same exact erased signature |
| 730 | FindMethodsByErasedSig visitor(slot->name(), slot->signature(), is_intf); |
| 731 | visitor.run(klass); |
| 732 | |
| 733 | MethodFamily* family; |
| 734 | visitor.get_discovered_family(&family); |
| 735 | if (family != NULL) { |
| 736 | family->determine_target(klass, CHECK); |
| 737 | slot->bind_family(family); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static void merge_in_new_methods(InstanceKlass* klass, |
| 742 | GrowableArray<Method*>* new_methods, TRAPS); |
| 743 | static void create_default_methods( InstanceKlass* klass, |
| 744 | GrowableArray<Method*>* new_methods, TRAPS); |
| 745 | |
| 746 | // This is the guts of the default methods implementation. This is called just |
| 747 | // after the classfile has been parsed if some ancestor has default methods. |
| 748 | // |
| 749 | // First it finds any name/signature slots that need any implementation (either |
| 750 | // because they are miranda or a superclass's implementation is an overpass |
| 751 | // itself). For each slot, iterate over the hierarchy, to see if they contain a |
| 752 | // signature that matches the slot we are looking at. |
| 753 | // |
| 754 | // For each slot filled, we either record the default method candidate in the |
| 755 | // klass default_methods list or, only to handle exception cases, we create an |
| 756 | // overpass method that throws an exception and add it to the klass methods list. |
| 757 | // The JVM does not create bridges nor handle generic signatures here. |
| 758 | void DefaultMethods::generate_default_methods( |
| 759 | InstanceKlass* klass, const GrowableArray<Method*>* mirandas, TRAPS) { |
| 760 | assert(klass != NULL, "invariant" ); |
| 761 | assert(klass != SystemDictionary::Object_klass(), "Shouldn't be called for Object" ); |
| 762 | |
| 763 | // This resource mark is the bound for all memory allocation that takes |
| 764 | // place during default method processing. After this goes out of scope, |
| 765 | // all (Resource) objects' memory will be reclaimed. Be careful if adding an |
| 766 | // embedded resource mark under here as that memory can't be used outside |
| 767 | // whatever scope it's in. |
| 768 | ResourceMark rm(THREAD); |
| 769 | |
| 770 | // Keep entire hierarchy alive for the duration of the computation |
| 771 | constantPoolHandle cp(THREAD, klass->constants()); |
| 772 | KeepAliveRegistrar keepAlive(THREAD); |
| 773 | KeepAliveVisitor loadKeepAlive(&keepAlive); |
| 774 | loadKeepAlive.run(klass); |
| 775 | |
| 776 | LogTarget(Debug, defaultmethods) lt; |
| 777 | if (lt.is_enabled()) { |
| 778 | ResourceMark rm; |
| 779 | lt.print("%s %s requires default method processing" , |
| 780 | klass->is_interface() ? "Interface" : "Class" , |
| 781 | klass->name()->as_klass_external_name()); |
| 782 | LogStream ls(lt); |
| 783 | PrintHierarchy printer(&ls); |
| 784 | printer.run(klass); |
| 785 | } |
| 786 | |
| 787 | GrowableArray<EmptyVtableSlot*> empty_slots; |
| 788 | find_empty_vtable_slots(&empty_slots, klass, mirandas, CHECK); |
| 789 | |
| 790 | if (empty_slots.length() > 0) { |
| 791 | for (int i = 0; i < empty_slots.length(); ++i) { |
| 792 | EmptyVtableSlot* slot = empty_slots.at(i); |
| 793 | LogTarget(Debug, defaultmethods) lt; |
| 794 | if (lt.is_enabled()) { |
| 795 | LogStream ls(lt); |
| 796 | streamIndentor si(&ls, 2); |
| 797 | ls.indent().print("Looking for default methods for slot " ); |
| 798 | slot->print_on(&ls); |
| 799 | ls.cr(); |
| 800 | } |
| 801 | generate_erased_defaults(klass, slot, klass->is_interface(), CHECK); |
| 802 | } |
| 803 | log_debug(defaultmethods)("Creating defaults and overpasses..." ); |
| 804 | create_defaults_and_exceptions(&empty_slots, klass, CHECK); |
| 805 | } |
| 806 | log_debug(defaultmethods)("Default method processing complete" ); |
| 807 | } |
| 808 | |
| 809 | static int assemble_method_error( |
| 810 | BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) { |
| 811 | |
| 812 | Symbol* init = vmSymbols::object_initializer_name(); |
| 813 | Symbol* sig = vmSymbols::string_void_signature(); |
| 814 | |
| 815 | BytecodeAssembler assem(buffer, cp); |
| 816 | |
| 817 | assem._new(errorName); |
| 818 | assem.dup(); |
| 819 | assem.load_string(message); |
| 820 | assem.invokespecial(errorName, init, sig); |
| 821 | assem.athrow(); |
| 822 | |
| 823 | return 3; // max stack size: [ exception, exception, string ] |
| 824 | } |
| 825 | |
| 826 | static Method* new_method( |
| 827 | BytecodeConstantPool* cp, BytecodeBuffer* bytecodes, Symbol* name, |
| 828 | Symbol* sig, AccessFlags flags, int max_stack, int params, |
| 829 | ConstMethod::MethodType mt, TRAPS) { |
| 830 | |
| 831 | address code_start = 0; |
| 832 | int code_length = 0; |
| 833 | InlineTableSizes sizes; |
| 834 | |
| 835 | if (bytecodes != NULL && bytecodes->length() > 0) { |
| 836 | code_start = static_cast<address>(bytecodes->adr_at(0)); |
| 837 | code_length = bytecodes->length(); |
| 838 | } |
| 839 | |
| 840 | Method* m = Method::allocate(cp->pool_holder()->class_loader_data(), |
| 841 | code_length, flags, &sizes, |
| 842 | mt, CHECK_NULL); |
| 843 | |
| 844 | m->set_constants(NULL); // This will get filled in later |
| 845 | m->set_name_index(cp->utf8(name)); |
| 846 | m->set_signature_index(cp->utf8(sig)); |
| 847 | ResultTypeFinder rtf(sig); |
| 848 | m->constMethod()->set_result_type(rtf.type()); |
| 849 | m->set_size_of_parameters(params); |
| 850 | m->set_max_stack(max_stack); |
| 851 | m->set_max_locals(params); |
| 852 | m->constMethod()->set_stackmap_data(NULL); |
| 853 | m->set_code(code_start); |
| 854 | |
| 855 | return m; |
| 856 | } |
| 857 | |
| 858 | static void switchover_constant_pool(BytecodeConstantPool* bpool, |
| 859 | InstanceKlass* klass, GrowableArray<Method*>* new_methods, TRAPS) { |
| 860 | |
| 861 | if (new_methods->length() > 0) { |
| 862 | ConstantPool* cp = bpool->create_constant_pool(CHECK); |
| 863 | if (cp != klass->constants()) { |
| 864 | // Copy resolved anonymous class into new constant pool. |
| 865 | if (klass->is_unsafe_anonymous()) { |
| 866 | cp->klass_at_put(klass->this_class_index(), klass); |
| 867 | } |
| 868 | klass->class_loader_data()->add_to_deallocate_list(klass->constants()); |
| 869 | klass->set_constants(cp); |
| 870 | cp->set_pool_holder(klass); |
| 871 | |
| 872 | for (int i = 0; i < new_methods->length(); ++i) { |
| 873 | new_methods->at(i)->set_constants(cp); |
| 874 | } |
| 875 | for (int i = 0; i < klass->methods()->length(); ++i) { |
| 876 | Method* mo = klass->methods()->at(i); |
| 877 | mo->set_constants(cp); |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // Create default_methods list for the current class. |
| 884 | // With the VM only processing erased signatures, the VM only |
| 885 | // creates an overpass in a conflict case or a case with no candidates. |
| 886 | // This allows virtual methods to override the overpass, but ensures |
| 887 | // that a local method search will find the exception rather than an abstract |
| 888 | // or default method that is not a valid candidate. |
| 889 | // |
| 890 | // Note that if overpass method are ever created that are not exception |
| 891 | // throwing methods then the loader constraint checking logic for vtable and |
| 892 | // itable creation needs to be changed to check loader constraints for the |
| 893 | // overpass methods that do not throw exceptions. |
| 894 | static void create_defaults_and_exceptions(GrowableArray<EmptyVtableSlot*>* slots, |
| 895 | InstanceKlass* klass, TRAPS) { |
| 896 | |
| 897 | GrowableArray<Method*> overpasses; |
| 898 | GrowableArray<Method*> defaults; |
| 899 | BytecodeConstantPool bpool(klass->constants()); |
| 900 | |
| 901 | for (int i = 0; i < slots->length(); ++i) { |
| 902 | EmptyVtableSlot* slot = slots->at(i); |
| 903 | |
| 904 | if (slot->is_bound()) { |
| 905 | MethodFamily* method = slot->get_binding(); |
| 906 | BytecodeBuffer buffer; |
| 907 | |
| 908 | LogTarget(Debug, defaultmethods) lt; |
| 909 | if (lt.is_enabled()) { |
| 910 | ResourceMark rm(THREAD); |
| 911 | LogStream ls(lt); |
| 912 | ls.print("for slot: " ); |
| 913 | slot->print_on(&ls); |
| 914 | ls.cr(); |
| 915 | if (method->has_target()) { |
| 916 | method->print_selected(&ls, 1); |
| 917 | } else if (method->throws_exception()) { |
| 918 | method->print_exception(&ls, 1); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if (method->has_target()) { |
| 923 | Method* selected = method->get_selected_target(); |
| 924 | if (selected->method_holder()->is_interface()) { |
| 925 | assert(!selected->is_private(), "pushing private interface method as default" ); |
| 926 | defaults.push(selected); |
| 927 | } |
| 928 | } else if (method->throws_exception()) { |
| 929 | int max_stack = assemble_method_error(&bpool, &buffer, |
| 930 | method->get_exception_name(), method->get_exception_message(), CHECK); |
| 931 | AccessFlags flags = accessFlags_from( |
| 932 | JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE); |
| 933 | Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(), |
| 934 | flags, max_stack, slot->size_of_parameters(), |
| 935 | ConstMethod::OVERPASS, CHECK); |
| 936 | // We push to the methods list: |
| 937 | // overpass methods which are exception throwing methods |
| 938 | if (m != NULL) { |
| 939 | overpasses.push(m); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | |
| 946 | log_debug(defaultmethods)("Created %d overpass methods" , overpasses.length()); |
| 947 | log_debug(defaultmethods)("Created %d default methods" , defaults.length()); |
| 948 | |
| 949 | if (overpasses.length() > 0) { |
| 950 | switchover_constant_pool(&bpool, klass, &overpasses, CHECK); |
| 951 | merge_in_new_methods(klass, &overpasses, CHECK); |
| 952 | } |
| 953 | if (defaults.length() > 0) { |
| 954 | create_default_methods(klass, &defaults, CHECK); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | static void create_default_methods(InstanceKlass* klass, |
| 959 | GrowableArray<Method*>* new_methods, TRAPS) { |
| 960 | |
| 961 | int new_size = new_methods->length(); |
| 962 | Array<Method*>* total_default_methods = MetadataFactory::new_array<Method*>( |
| 963 | klass->class_loader_data(), new_size, NULL, CHECK); |
| 964 | for (int index = 0; index < new_size; index++ ) { |
| 965 | total_default_methods->at_put(index, new_methods->at(index)); |
| 966 | } |
| 967 | Method::sort_methods(total_default_methods, /*set_idnums=*/false); |
| 968 | |
| 969 | klass->set_default_methods(total_default_methods); |
| 970 | } |
| 971 | |
| 972 | static void sort_methods(GrowableArray<Method*>* methods) { |
| 973 | // Note that this must sort using the same key as is used for sorting |
| 974 | // methods in InstanceKlass. |
| 975 | bool sorted = true; |
| 976 | for (int i = methods->length() - 1; i > 0; --i) { |
| 977 | for (int j = 0; j < i; ++j) { |
| 978 | Method* m1 = methods->at(j); |
| 979 | Method* m2 = methods->at(j + 1); |
| 980 | if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) { |
| 981 | methods->at_put(j, m2); |
| 982 | methods->at_put(j + 1, m1); |
| 983 | sorted = false; |
| 984 | } |
| 985 | } |
| 986 | if (sorted) break; |
| 987 | sorted = true; |
| 988 | } |
| 989 | #ifdef ASSERT |
| 990 | uintptr_t prev = 0; |
| 991 | for (int i = 0; i < methods->length(); ++i) { |
| 992 | Method* mh = methods->at(i); |
| 993 | uintptr_t nv = (uintptr_t)mh->name(); |
| 994 | assert(nv >= prev, "Incorrect overpass method ordering" ); |
| 995 | prev = nv; |
| 996 | } |
| 997 | #endif |
| 998 | } |
| 999 | |
| 1000 | static void merge_in_new_methods(InstanceKlass* klass, |
| 1001 | GrowableArray<Method*>* new_methods, TRAPS) { |
| 1002 | |
| 1003 | enum { ANNOTATIONS, PARAMETERS, DEFAULTS, NUM_ARRAYS }; |
| 1004 | |
| 1005 | Array<Method*>* original_methods = klass->methods(); |
| 1006 | Array<int>* original_ordering = klass->method_ordering(); |
| 1007 | Array<int>* merged_ordering = Universe::the_empty_int_array(); |
| 1008 | |
| 1009 | int new_size = klass->methods()->length() + new_methods->length(); |
| 1010 | |
| 1011 | Array<Method*>* merged_methods = MetadataFactory::new_array<Method*>( |
| 1012 | klass->class_loader_data(), new_size, NULL, CHECK); |
| 1013 | |
| 1014 | // original_ordering might be empty if this class has no methods of its own |
| 1015 | if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { |
| 1016 | merged_ordering = MetadataFactory::new_array<int>( |
| 1017 | klass->class_loader_data(), new_size, CHECK); |
| 1018 | } |
| 1019 | int method_order_index = klass->methods()->length(); |
| 1020 | |
| 1021 | sort_methods(new_methods); |
| 1022 | |
| 1023 | // Perform grand merge of existing methods and new methods |
| 1024 | int orig_idx = 0; |
| 1025 | int new_idx = 0; |
| 1026 | |
| 1027 | for (int i = 0; i < new_size; ++i) { |
| 1028 | Method* orig_method = NULL; |
| 1029 | Method* new_method = NULL; |
| 1030 | if (orig_idx < original_methods->length()) { |
| 1031 | orig_method = original_methods->at(orig_idx); |
| 1032 | } |
| 1033 | if (new_idx < new_methods->length()) { |
| 1034 | new_method = new_methods->at(new_idx); |
| 1035 | } |
| 1036 | |
| 1037 | if (orig_method != NULL && |
| 1038 | (new_method == NULL || orig_method->name() < new_method->name())) { |
| 1039 | merged_methods->at_put(i, orig_method); |
| 1040 | original_methods->at_put(orig_idx, NULL); |
| 1041 | if (merged_ordering->length() > 0) { |
| 1042 | assert(original_ordering != NULL && original_ordering->length() > 0, |
| 1043 | "should have original order information for this method" ); |
| 1044 | merged_ordering->at_put(i, original_ordering->at(orig_idx)); |
| 1045 | } |
| 1046 | ++orig_idx; |
| 1047 | } else { |
| 1048 | merged_methods->at_put(i, new_method); |
| 1049 | if (merged_ordering->length() > 0) { |
| 1050 | merged_ordering->at_put(i, method_order_index++); |
| 1051 | } |
| 1052 | ++new_idx; |
| 1053 | } |
| 1054 | // update idnum for new location |
| 1055 | merged_methods->at(i)->set_method_idnum(i); |
| 1056 | merged_methods->at(i)->set_orig_method_idnum(i); |
| 1057 | } |
| 1058 | |
| 1059 | // Verify correct order |
| 1060 | #ifdef ASSERT |
| 1061 | uintptr_t prev = 0; |
| 1062 | for (int i = 0; i < merged_methods->length(); ++i) { |
| 1063 | Method* mo = merged_methods->at(i); |
| 1064 | uintptr_t nv = (uintptr_t)mo->name(); |
| 1065 | assert(nv >= prev, "Incorrect method ordering" ); |
| 1066 | prev = nv; |
| 1067 | } |
| 1068 | #endif |
| 1069 | |
| 1070 | // Replace klass methods with new merged lists |
| 1071 | klass->set_methods(merged_methods); |
| 1072 | klass->set_initial_method_idnum(new_size); |
| 1073 | klass->set_method_ordering(merged_ordering); |
| 1074 | |
| 1075 | // Free metadata |
| 1076 | ClassLoaderData* cld = klass->class_loader_data(); |
| 1077 | if (original_methods->length() > 0) { |
| 1078 | MetadataFactory::free_array(cld, original_methods); |
| 1079 | } |
| 1080 | if (original_ordering != NULL && original_ordering->length() > 0) { |
| 1081 | MetadataFactory::free_array(cld, original_ordering); |
| 1082 | } |
| 1083 | } |
| 1084 | |