src/share/vm/classfile/defaultMethods.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8604
04d83ba48607
child 9572
624a0741915c
permissions
-rw-r--r--

Merge

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

mercurial