src/share/vm/classfile/defaultMethods.cpp

Fri, 30 Aug 2013 09:50:49 +0100

author
chegar
date
Fri, 30 Aug 2013 09:50:49 +0100
changeset 5879
07b5f47d7a18
parent 5377
50257d6f5aaa
child 5599
91b93f523ec6
permissions
-rw-r--r--

Merge

kamg@4245 1 /*
coleenp@4572 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
kamg@4245 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
kamg@4245 4 *
kamg@4245 5 * This code is free software; you can redistribute it and/or modify it
kamg@4245 6 * under the terms of the GNU General Public License version 2 only, as
kamg@4245 7 * published by the Free Software Foundation.
kamg@4245 8 *
kamg@4245 9 * This code is distributed in the hope that it will be useful, but WITHOUT
kamg@4245 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kamg@4245 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kamg@4245 12 * version 2 for more details (a copy is included in the LICENSE file that
kamg@4245 13 * accompanied this code).
kamg@4245 14 *
kamg@4245 15 * You should have received a copy of the GNU General Public License version
kamg@4245 16 * 2 along with this work; if not, write to the Free Software Foundation,
kamg@4245 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
kamg@4245 18 *
kamg@4245 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
kamg@4245 20 * or visit www.oracle.com if you need additional information or have any
kamg@4245 21 * questions.
kamg@4245 22 *
kamg@4245 23 */
kamg@4245 24
kamg@4245 25 #include "precompiled.hpp"
kamg@4245 26 #include "classfile/bytecodeAssembler.hpp"
kamg@4245 27 #include "classfile/defaultMethods.hpp"
kamg@4245 28 #include "classfile/genericSignatures.hpp"
kamg@4245 29 #include "classfile/symbolTable.hpp"
kamg@4245 30 #include "memory/allocation.hpp"
kamg@4245 31 #include "memory/metadataFactory.hpp"
kamg@4245 32 #include "memory/resourceArea.hpp"
kamg@4245 33 #include "runtime/signature.hpp"
kamg@4245 34 #include "runtime/thread.hpp"
kamg@4245 35 #include "oops/instanceKlass.hpp"
kamg@4245 36 #include "oops/klass.hpp"
kamg@4245 37 #include "oops/method.hpp"
kamg@4245 38 #include "utilities/accessFlags.hpp"
kamg@4245 39 #include "utilities/exceptions.hpp"
kamg@4245 40 #include "utilities/ostream.hpp"
kamg@4245 41 #include "utilities/pair.hpp"
kamg@4245 42 #include "utilities/resourceHash.hpp"
kamg@4245 43
kamg@4245 44 typedef enum { QUALIFIED, DISQUALIFIED } QualifiedState;
kamg@4245 45
kamg@4245 46 // Because we use an iterative algorithm when iterating over the type
kamg@4245 47 // hierarchy, we can't use traditional scoped objects which automatically do
kamg@4245 48 // cleanup in the destructor when the scope is exited. PseudoScope (and
kamg@4245 49 // PseudoScopeMark) provides a similar functionality, but for when you want a
kamg@4245 50 // scoped object in non-stack memory (such as in resource memory, as we do
kamg@4245 51 // here). You've just got to remember to call 'destroy()' on the scope when
kamg@4245 52 // leaving it (and marks have to be explicitly added).
kamg@4245 53 class PseudoScopeMark : public ResourceObj {
kamg@4245 54 public:
kamg@4245 55 virtual void destroy() = 0;
kamg@4245 56 };
kamg@4245 57
kamg@4245 58 class PseudoScope : public ResourceObj {
kamg@4245 59 private:
kamg@4245 60 GrowableArray<PseudoScopeMark*> _marks;
kamg@4245 61 public:
kamg@4245 62
kamg@4245 63 static PseudoScope* cast(void* data) {
kamg@4245 64 return static_cast<PseudoScope*>(data);
kamg@4245 65 }
kamg@4245 66
kamg@4245 67 void add_mark(PseudoScopeMark* psm) {
kamg@4245 68 _marks.append(psm);
kamg@4245 69 }
kamg@4245 70
kamg@4245 71 void destroy() {
kamg@4245 72 for (int i = 0; i < _marks.length(); ++i) {
kamg@4245 73 _marks.at(i)->destroy();
kamg@4245 74 }
kamg@4245 75 }
kamg@4245 76 };
kamg@4245 77
kamg@4245 78 class ContextMark : public PseudoScopeMark {
kamg@4245 79 private:
kamg@4245 80 generic::Context::Mark _mark;
kamg@4245 81 public:
kamg@4245 82 ContextMark(const generic::Context::Mark& cm) : _mark(cm) {}
kamg@4245 83 virtual void destroy() { _mark.destroy(); }
kamg@4245 84 };
kamg@4245 85
kamg@4245 86 #ifndef PRODUCT
kamg@4245 87 static void print_slot(outputStream* str, Symbol* name, Symbol* signature) {
kamg@4245 88 ResourceMark rm;
kamg@4245 89 str->print("%s%s", name->as_C_string(), signature->as_C_string());
kamg@4245 90 }
kamg@4245 91
kamg@4245 92 static void print_method(outputStream* str, Method* mo, bool with_class=true) {
kamg@4245 93 ResourceMark rm;
kamg@4245 94 if (with_class) {
kamg@4245 95 str->print("%s.", mo->klass_name()->as_C_string());
kamg@4245 96 }
kamg@4245 97 print_slot(str, mo->name(), mo->signature());
kamg@4245 98 }
kamg@4245 99 #endif // ndef PRODUCT
kamg@4245 100
kamg@4245 101 /**
kamg@4245 102 * Perform a depth-first iteration over the class hierarchy, applying
kamg@4245 103 * algorithmic logic as it goes.
kamg@4245 104 *
kamg@4245 105 * This class is one half of the inheritance hierarchy analysis mechanism.
kamg@4245 106 * It is meant to be used in conjunction with another class, the algorithm,
kamg@4245 107 * which is indicated by the ALGO template parameter. This class can be
kamg@4245 108 * paired with any algorithm class that provides the required methods.
kamg@4245 109 *
kamg@4245 110 * This class contains all the mechanics for iterating over the class hierarchy
kamg@4245 111 * starting at a particular root, without recursing (thus limiting stack growth
kamg@4245 112 * from this point). It visits each superclass (if present) and superinterface
kamg@4245 113 * in a depth-first manner, with callbacks to the ALGO class as each class is
kamg@4245 114 * encountered (visit()), The algorithm can cut-off further exploration of a
kamg@4245 115 * particular branch by returning 'false' from a visit() call.
kamg@4245 116 *
kamg@4245 117 * The ALGO class, must provide a visit() method, which each of which will be
kamg@4245 118 * called once for each node in the inheritance tree during the iteration. In
kamg@4245 119 * addition, it can provide a memory block via new_node_data(InstanceKlass*),
kamg@4245 120 * which it can use for node-specific storage (and access via the
kamg@4245 121 * current_data() and data_at_depth(int) methods).
kamg@4245 122 *
kamg@4245 123 * Bare minimum needed to be an ALGO class:
kamg@4245 124 * class Algo : public HierarchyVisitor<Algo> {
kamg@4245 125 * void* new_node_data(InstanceKlass* cls) { return NULL; }
kamg@4245 126 * void free_node_data(void* data) { return; }
kamg@4245 127 * bool visit() { return true; }
kamg@4245 128 * };
kamg@4245 129 */
kamg@4245 130 template <class ALGO>
kamg@4245 131 class HierarchyVisitor : StackObj {
kamg@4245 132 private:
kamg@4245 133
kamg@4245 134 class Node : public ResourceObj {
kamg@4245 135 public:
kamg@4245 136 InstanceKlass* _class;
kamg@4245 137 bool _super_was_visited;
kamg@4245 138 int _interface_index;
kamg@4245 139 void* _algorithm_data;
kamg@4245 140
kamg@4245 141 Node(InstanceKlass* cls, void* data, bool visit_super)
kamg@4245 142 : _class(cls), _super_was_visited(!visit_super),
kamg@4245 143 _interface_index(0), _algorithm_data(data) {}
kamg@4245 144
kamg@4245 145 int number_of_interfaces() { return _class->local_interfaces()->length(); }
kamg@4245 146 int interface_index() { return _interface_index; }
kamg@4245 147 void set_super_visited() { _super_was_visited = true; }
kamg@4245 148 void increment_visited_interface() { ++_interface_index; }
kamg@4245 149 void set_all_interfaces_visited() {
kamg@4245 150 _interface_index = number_of_interfaces();
kamg@4245 151 }
kamg@4245 152 bool has_visited_super() { return _super_was_visited; }
kamg@4245 153 bool has_visited_all_interfaces() {
kamg@4245 154 return interface_index() >= number_of_interfaces();
kamg@4245 155 }
kamg@4245 156 InstanceKlass* interface_at(int index) {
kamg@4245 157 return InstanceKlass::cast(_class->local_interfaces()->at(index));
kamg@4245 158 }
kamg@4245 159 InstanceKlass* next_super() { return _class->java_super(); }
kamg@4245 160 InstanceKlass* next_interface() {
kamg@4245 161 return interface_at(interface_index());
kamg@4245 162 }
kamg@4245 163 };
kamg@4245 164
kamg@4245 165 bool _cancelled;
kamg@4245 166 GrowableArray<Node*> _path;
kamg@4245 167
kamg@4245 168 Node* current_top() const { return _path.top(); }
kamg@4245 169 bool has_more_nodes() const { return !_path.is_empty(); }
kamg@4245 170 void push(InstanceKlass* cls, void* data) {
kamg@4245 171 assert(cls != NULL, "Requires a valid instance class");
kamg@4245 172 Node* node = new Node(cls, data, has_super(cls));
kamg@4245 173 _path.push(node);
kamg@4245 174 }
kamg@4245 175 void pop() { _path.pop(); }
kamg@4245 176
kamg@4245 177 void reset_iteration() {
kamg@4245 178 _cancelled = false;
kamg@4245 179 _path.clear();
kamg@4245 180 }
kamg@4245 181 bool is_cancelled() const { return _cancelled; }
kamg@4245 182
kamg@4245 183 static bool has_super(InstanceKlass* cls) {
kamg@4245 184 return cls->super() != NULL && !cls->is_interface();
kamg@4245 185 }
kamg@4245 186
kamg@4245 187 Node* node_at_depth(int i) const {
kamg@4245 188 return (i >= _path.length()) ? NULL : _path.at(_path.length() - i - 1);
kamg@4245 189 }
kamg@4245 190
kamg@4245 191 protected:
kamg@4245 192
kamg@4245 193 // Accessors available to the algorithm
kamg@4245 194 int current_depth() const { return _path.length() - 1; }
kamg@4245 195
kamg@4245 196 InstanceKlass* class_at_depth(int i) {
kamg@4245 197 Node* n = node_at_depth(i);
kamg@4245 198 return n == NULL ? NULL : n->_class;
kamg@4245 199 }
kamg@4245 200 InstanceKlass* current_class() { return class_at_depth(0); }
kamg@4245 201
kamg@4245 202 void* data_at_depth(int i) {
kamg@4245 203 Node* n = node_at_depth(i);
kamg@4245 204 return n == NULL ? NULL : n->_algorithm_data;
kamg@4245 205 }
kamg@4245 206 void* current_data() { return data_at_depth(0); }
kamg@4245 207
kamg@4245 208 void cancel_iteration() { _cancelled = true; }
kamg@4245 209
kamg@4245 210 public:
kamg@4245 211
kamg@4245 212 void run(InstanceKlass* root) {
kamg@4245 213 ALGO* algo = static_cast<ALGO*>(this);
kamg@4245 214
kamg@4245 215 reset_iteration();
kamg@4245 216
kamg@4245 217 void* algo_data = algo->new_node_data(root);
kamg@4245 218 push(root, algo_data);
kamg@4245 219 bool top_needs_visit = true;
kamg@4245 220
kamg@4245 221 do {
kamg@4245 222 Node* top = current_top();
kamg@4245 223 if (top_needs_visit) {
kamg@4245 224 if (algo->visit() == false) {
kamg@4245 225 // algorithm does not want to continue along this path. Arrange
kamg@4245 226 // it so that this state is immediately popped off the stack
kamg@4245 227 top->set_super_visited();
kamg@4245 228 top->set_all_interfaces_visited();
kamg@4245 229 }
kamg@4245 230 top_needs_visit = false;
kamg@4245 231 }
kamg@4245 232
kamg@4245 233 if (top->has_visited_super() && top->has_visited_all_interfaces()) {
kamg@4245 234 algo->free_node_data(top->_algorithm_data);
kamg@4245 235 pop();
kamg@4245 236 } else {
kamg@4245 237 InstanceKlass* next = NULL;
kamg@4245 238 if (top->has_visited_super() == false) {
kamg@4245 239 next = top->next_super();
kamg@4245 240 top->set_super_visited();
kamg@4245 241 } else {
kamg@4245 242 next = top->next_interface();
kamg@4245 243 top->increment_visited_interface();
kamg@4245 244 }
kamg@4245 245 assert(next != NULL, "Otherwise we shouldn't be here");
kamg@4245 246 algo_data = algo->new_node_data(next);
kamg@4245 247 push(next, algo_data);
kamg@4245 248 top_needs_visit = true;
kamg@4245 249 }
kamg@4245 250 } while (!is_cancelled() && has_more_nodes());
kamg@4245 251 }
kamg@4245 252 };
kamg@4245 253
kamg@4245 254 #ifndef PRODUCT
kamg@4245 255 class PrintHierarchy : public HierarchyVisitor<PrintHierarchy> {
kamg@4245 256 public:
kamg@4245 257
kamg@4245 258 bool visit() {
kamg@4245 259 InstanceKlass* cls = current_class();
kamg@4245 260 streamIndentor si(tty, current_depth() * 2);
kamg@4245 261 tty->indent().print_cr("%s", cls->name()->as_C_string());
kamg@4245 262 return true;
kamg@4245 263 }
kamg@4245 264
kamg@4245 265 void* new_node_data(InstanceKlass* cls) { return NULL; }
kamg@4245 266 void free_node_data(void* data) { return; }
kamg@4245 267 };
kamg@4245 268 #endif // ndef PRODUCT
kamg@4245 269
kamg@4245 270 // Used to register InstanceKlass objects and all related metadata structures
kamg@4245 271 // (Methods, ConstantPools) as "in-use" by the current thread so that they can't
kamg@4245 272 // be deallocated by class redefinition while we're using them. The classes are
kamg@4245 273 // de-registered when this goes out of scope.
kamg@4245 274 //
kamg@4245 275 // Once a class is registered, we need not bother with methodHandles or
kamg@4245 276 // constantPoolHandles for it's associated metadata.
kamg@4245 277 class KeepAliveRegistrar : public StackObj {
kamg@4245 278 private:
kamg@4245 279 Thread* _thread;
kamg@4245 280 GrowableArray<ConstantPool*> _keep_alive;
kamg@4245 281
kamg@4245 282 public:
kamg@4245 283 KeepAliveRegistrar(Thread* thread) : _thread(thread), _keep_alive(20) {
kamg@4245 284 assert(thread == Thread::current(), "Must be current thread");
kamg@4245 285 }
kamg@4245 286
kamg@4245 287 ~KeepAliveRegistrar() {
kamg@4245 288 for (int i = _keep_alive.length() - 1; i >= 0; --i) {
kamg@4245 289 ConstantPool* cp = _keep_alive.at(i);
kamg@4245 290 int idx = _thread->metadata_handles()->find_from_end(cp);
kamg@4245 291 assert(idx > 0, "Must be in the list");
kamg@4245 292 _thread->metadata_handles()->remove_at(idx);
kamg@4245 293 }
kamg@4245 294 }
kamg@4245 295
kamg@4245 296 // Register a class as 'in-use' by the thread. It's fine to register a class
kamg@4245 297 // multiple times (though perhaps inefficient)
kamg@4245 298 void register_class(InstanceKlass* ik) {
kamg@4245 299 ConstantPool* cp = ik->constants();
kamg@4245 300 _keep_alive.push(cp);
kamg@4245 301 _thread->metadata_handles()->push(cp);
kamg@4245 302 }
kamg@4245 303 };
kamg@4245 304
kamg@4245 305 class KeepAliveVisitor : public HierarchyVisitor<KeepAliveVisitor> {
kamg@4245 306 private:
kamg@4245 307 KeepAliveRegistrar* _registrar;
kamg@4245 308
kamg@4245 309 public:
kamg@4245 310 KeepAliveVisitor(KeepAliveRegistrar* registrar) : _registrar(registrar) {}
kamg@4245 311
kamg@4245 312 void* new_node_data(InstanceKlass* cls) { return NULL; }
kamg@4245 313 void free_node_data(void* data) { return; }
kamg@4245 314
kamg@4245 315 bool visit() {
kamg@4245 316 _registrar->register_class(current_class());
kamg@4245 317 return true;
kamg@4245 318 }
kamg@4245 319 };
kamg@4245 320
acorn@5377 321
kamg@4245 322 // A method family contains a set of all methods that implement a single
acorn@5377 323 // erased method. As members of the set are collected while walking over the
kamg@4245 324 // hierarchy, they are tagged with a qualification state. The qualification
kamg@4245 325 // state for an erased method is set to disqualified if there exists a path
kamg@4245 326 // from the root of hierarchy to the method that contains an interleaving
acorn@5377 327 // erased method defined in an interface.
acorn@5377 328
kamg@4245 329 class MethodFamily : public ResourceObj {
kamg@4245 330 private:
kamg@4245 331
kamg@4245 332 GrowableArray<Pair<Method*,QualifiedState> > _members;
kamg@4245 333 ResourceHashtable<Method*, int> _member_index;
kamg@4245 334
kamg@4245 335 Method* _selected_target; // Filled in later, if a unique target exists
kamg@4245 336 Symbol* _exception_message; // If no unique target is found
kamg@4245 337
kamg@4245 338 bool contains_method(Method* method) {
kamg@4245 339 int* lookup = _member_index.get(method);
kamg@4245 340 return lookup != NULL;
kamg@4245 341 }
kamg@4245 342
kamg@4245 343 void add_method(Method* method, QualifiedState state) {
kamg@4245 344 Pair<Method*,QualifiedState> entry(method, state);
kamg@4245 345 _member_index.put(method, _members.length());
kamg@4245 346 _members.append(entry);
kamg@4245 347 }
kamg@4245 348
kamg@4245 349 void disqualify_method(Method* method) {
kamg@4245 350 int* index = _member_index.get(method);
morris@4778 351 guarantee(index != NULL && *index >= 0 && *index < _members.length(), "bad index");
kamg@4245 352 _members.at(*index).second = DISQUALIFIED;
kamg@4245 353 }
kamg@4245 354
kamg@4245 355 Symbol* generate_no_defaults_message(TRAPS) const;
kamg@4245 356 Symbol* generate_abstract_method_message(Method* method, TRAPS) const;
kamg@4245 357 Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const;
kamg@4245 358
kamg@4245 359 public:
kamg@4245 360
acorn@5377 361 MethodFamily()
acorn@5377 362 : _selected_target(NULL), _exception_message(NULL) {}
kamg@4245 363
kamg@4245 364 void set_target_if_empty(Method* m) {
kamg@4245 365 if (_selected_target == NULL && !m->is_overpass()) {
kamg@4245 366 _selected_target = m;
kamg@4245 367 }
kamg@4245 368 }
kamg@4245 369
kamg@4245 370 void record_qualified_method(Method* m) {
kamg@4245 371 // If the method already exists in the set as qualified, this operation is
kamg@4245 372 // redundant. If it already exists as disqualified, then we leave it as
kamg@4245 373 // disqualfied. Thus we only add to the set if it's not already in the
kamg@4245 374 // set.
kamg@4245 375 if (!contains_method(m)) {
kamg@4245 376 add_method(m, QUALIFIED);
kamg@4245 377 }
kamg@4245 378 }
kamg@4245 379
kamg@4245 380 void record_disqualified_method(Method* m) {
kamg@4245 381 // If not in the set, add it as disqualified. If it's already in the set,
kamg@4245 382 // then set the state to disqualified no matter what the previous state was.
kamg@4245 383 if (!contains_method(m)) {
kamg@4245 384 add_method(m, DISQUALIFIED);
kamg@4245 385 } else {
kamg@4245 386 disqualify_method(m);
kamg@4245 387 }
kamg@4245 388 }
kamg@4245 389
kamg@4245 390 bool has_target() const { return _selected_target != NULL; }
kamg@4245 391 bool throws_exception() { return _exception_message != NULL; }
kamg@4245 392
kamg@4245 393 Method* get_selected_target() { return _selected_target; }
kamg@4245 394 Symbol* get_exception_message() { return _exception_message; }
kamg@4245 395
kamg@4245 396 // Either sets the target or the exception error message
kamg@4245 397 void determine_target(InstanceKlass* root, TRAPS) {
kamg@4245 398 if (has_target() || throws_exception()) {
kamg@4245 399 return;
kamg@4245 400 }
kamg@4245 401
kamg@4245 402 GrowableArray<Method*> qualified_methods;
kamg@4245 403 for (int i = 0; i < _members.length(); ++i) {
kamg@4245 404 Pair<Method*,QualifiedState> entry = _members.at(i);
kamg@4245 405 if (entry.second == QUALIFIED) {
kamg@4245 406 qualified_methods.append(entry.first);
kamg@4245 407 }
kamg@4245 408 }
kamg@4245 409
kamg@4245 410 if (qualified_methods.length() == 0) {
kamg@4245 411 _exception_message = generate_no_defaults_message(CHECK);
kamg@4245 412 } else if (qualified_methods.length() == 1) {
kamg@4245 413 Method* method = qualified_methods.at(0);
kamg@4245 414 if (method->is_abstract()) {
kamg@4245 415 _exception_message = generate_abstract_method_message(method, CHECK);
kamg@4245 416 } else {
kamg@4245 417 _selected_target = qualified_methods.at(0);
kamg@4245 418 }
kamg@4245 419 } else {
kamg@4245 420 _exception_message = generate_conflicts_message(&qualified_methods,CHECK);
kamg@4245 421 }
kamg@4245 422
kamg@4245 423 assert((has_target() ^ throws_exception()) == 1,
kamg@4245 424 "One and only one must be true");
kamg@4245 425 }
kamg@4245 426
kamg@4245 427 bool contains_signature(Symbol* query) {
kamg@4245 428 for (int i = 0; i < _members.length(); ++i) {
kamg@4245 429 if (query == _members.at(i).first->signature()) {
kamg@4245 430 return true;
kamg@4245 431 }
kamg@4245 432 }
kamg@4245 433 return false;
kamg@4245 434 }
kamg@4245 435
kamg@4245 436 #ifndef PRODUCT
acorn@5377 437 void print_sig_on(outputStream* str, Symbol* signature, int indent) const {
kamg@4245 438 streamIndentor si(str, indent * 2);
kamg@4245 439
acorn@5377 440 str->indent().print_cr("Logical Method %s:", signature->as_C_string());
kamg@4245 441
kamg@4245 442 streamIndentor si2(str);
kamg@4245 443 for (int i = 0; i < _members.length(); ++i) {
kamg@4245 444 str->indent();
kamg@4245 445 print_method(str, _members.at(i).first);
kamg@4245 446 if (_members.at(i).second == DISQUALIFIED) {
kamg@4245 447 str->print(" (disqualified)");
kamg@4245 448 }
kamg@4245 449 str->print_cr("");
kamg@4245 450 }
kamg@4245 451
kamg@4245 452 if (_selected_target != NULL) {
kamg@4245 453 print_selected(str, 1);
kamg@4245 454 }
kamg@4245 455 }
kamg@4245 456
kamg@4245 457 void print_selected(outputStream* str, int indent) const {
kamg@4245 458 assert(has_target(), "Should be called otherwise");
kamg@4245 459 streamIndentor si(str, indent * 2);
kamg@4245 460 str->indent().print("Selected method: ");
kamg@4245 461 print_method(str, _selected_target);
kamg@4245 462 str->print_cr("");
kamg@4245 463 }
kamg@4245 464
kamg@4245 465 void print_exception(outputStream* str, int indent) {
kamg@4245 466 assert(throws_exception(), "Should be called otherwise");
kamg@4245 467 streamIndentor si(str, indent * 2);
kamg@4245 468 str->indent().print_cr("%s", _exception_message->as_C_string());
kamg@4245 469 }
kamg@4245 470 #endif // ndef PRODUCT
kamg@4245 471 };
kamg@4245 472
kamg@4245 473 Symbol* MethodFamily::generate_no_defaults_message(TRAPS) const {
kamg@4245 474 return SymbolTable::new_symbol("No qualifying defaults found", CHECK_NULL);
kamg@4245 475 }
kamg@4245 476
kamg@4245 477 Symbol* MethodFamily::generate_abstract_method_message(Method* method, TRAPS) const {
kamg@4245 478 Symbol* klass = method->klass_name();
kamg@4245 479 Symbol* name = method->name();
kamg@4245 480 Symbol* sig = method->signature();
kamg@4245 481 stringStream ss;
kamg@4245 482 ss.print("Method ");
kamg@4245 483 ss.write((const char*)klass->bytes(), klass->utf8_length());
kamg@4245 484 ss.print(".");
kamg@4245 485 ss.write((const char*)name->bytes(), name->utf8_length());
kamg@4245 486 ss.write((const char*)sig->bytes(), sig->utf8_length());
kamg@4245 487 ss.print(" is abstract");
kamg@4245 488 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL);
kamg@4245 489 }
kamg@4245 490
kamg@4245 491 Symbol* MethodFamily::generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const {
kamg@4245 492 stringStream ss;
kamg@4245 493 ss.print("Conflicting default methods:");
kamg@4245 494 for (int i = 0; i < methods->length(); ++i) {
kamg@4245 495 Method* method = methods->at(i);
kamg@4245 496 Symbol* klass = method->klass_name();
kamg@4245 497 Symbol* name = method->name();
kamg@4245 498 ss.print(" ");
kamg@4245 499 ss.write((const char*)klass->bytes(), klass->utf8_length());
kamg@4245 500 ss.print(".");
kamg@4245 501 ss.write((const char*)name->bytes(), name->utf8_length());
kamg@4245 502 }
kamg@4245 503 return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL);
kamg@4245 504 }
kamg@4245 505
acorn@5377 506 // A generic method family contains a set of all methods that implement a single
acorn@5377 507 // language-level method. Because of erasure, these methods may have different
acorn@5377 508 // signatures. As members of the set are collected while walking over the
acorn@5377 509 // hierarchy, they are tagged with a qualification state. The qualification
acorn@5377 510 // state for an erased method is set to disqualified if there exists a path
acorn@5377 511 // from the root of hierarchy to the method that contains an interleaving
acorn@5377 512 // language-equivalent method defined in an interface.
acorn@5377 513 class GenericMethodFamily : public MethodFamily {
acorn@5377 514 private:
acorn@5377 515
acorn@5377 516 generic::MethodDescriptor* _descriptor; // language-level description
acorn@5377 517
acorn@5377 518 public:
acorn@5377 519
acorn@5377 520 GenericMethodFamily(generic::MethodDescriptor* canonical_desc)
acorn@5377 521 : _descriptor(canonical_desc) {}
acorn@5377 522
acorn@5377 523 generic::MethodDescriptor* descriptor() const { return _descriptor; }
acorn@5377 524
acorn@5377 525 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
acorn@5377 526 return descriptor()->covariant_match(md, ctx);
acorn@5377 527 }
acorn@5377 528
acorn@5377 529 #ifndef PRODUCT
acorn@5377 530 Symbol* get_generic_sig() const {
acorn@5377 531
acorn@5377 532 generic::Context ctx(NULL); // empty, as _descriptor already canonicalized
acorn@5377 533 TempNewSymbol sig = descriptor()->reify_signature(&ctx, Thread::current());
acorn@5377 534 return sig;
acorn@5377 535 }
acorn@5377 536 #endif // ndef PRODUCT
acorn@5377 537 };
acorn@5377 538
kamg@4245 539 class StateRestorer;
kamg@4245 540
acorn@5377 541 // StatefulMethodFamily is a wrapper around a MethodFamily that maintains the
kamg@4245 542 // qualification state during hierarchy visitation, and applies that state
acorn@5377 543 // when adding members to the MethodFamily
kamg@4245 544 class StatefulMethodFamily : public ResourceObj {
kamg@4245 545 friend class StateRestorer;
kamg@4245 546 private:
kamg@4245 547 QualifiedState _qualification_state;
kamg@4245 548
kamg@4245 549 void set_qualification_state(QualifiedState state) {
kamg@4245 550 _qualification_state = state;
kamg@4245 551 }
kamg@4245 552
acorn@5377 553 protected:
acorn@5377 554 MethodFamily* _method_family;
acorn@5377 555
kamg@4245 556 public:
acorn@5377 557 StatefulMethodFamily() {
acorn@5377 558 _method_family = new MethodFamily();
acorn@5377 559 _qualification_state = QUALIFIED;
kamg@4245 560 }
kamg@4245 561
acorn@5377 562 StatefulMethodFamily(MethodFamily* mf) {
acorn@5377 563 _method_family = mf;
acorn@5377 564 _qualification_state = QUALIFIED;
acorn@5377 565 }
kamg@4245 566
acorn@5377 567 void set_target_if_empty(Method* m) { _method_family->set_target_if_empty(m); }
acorn@5377 568
acorn@5377 569 MethodFamily* get_method_family() { return _method_family; }
acorn@5377 570
acorn@5377 571 StateRestorer* record_method_and_dq_further(Method* mo);
acorn@5377 572 };
acorn@5377 573
acorn@5377 574
acorn@5377 575 // StatefulGenericMethodFamily is a wrapper around GenericMethodFamily that maintains the
acorn@5377 576 // qualification state during hierarchy visitation, and applies that state
acorn@5377 577 // when adding members to the GenericMethodFamily.
acorn@5377 578 class StatefulGenericMethodFamily : public StatefulMethodFamily {
acorn@5377 579
acorn@5377 580 public:
acorn@5377 581 StatefulGenericMethodFamily(generic::MethodDescriptor* md, generic::Context* ctx)
acorn@5377 582 : StatefulMethodFamily(new GenericMethodFamily(md->canonicalize(ctx))) {
acorn@5377 583
acorn@5377 584 }
acorn@5377 585 GenericMethodFamily* get_method_family() {
acorn@5377 586 return (GenericMethodFamily*)_method_family;
acorn@5377 587 }
kamg@4245 588
kamg@4245 589 bool descriptor_matches(generic::MethodDescriptor* md, generic::Context* ctx) {
acorn@5377 590 return get_method_family()->descriptor_matches(md, ctx);
kamg@4245 591 }
kamg@4245 592 };
kamg@4245 593
kamg@4245 594 class StateRestorer : public PseudoScopeMark {
kamg@4245 595 private:
kamg@4245 596 StatefulMethodFamily* _method;
kamg@4245 597 QualifiedState _state_to_restore;
kamg@4245 598 public:
kamg@4245 599 StateRestorer(StatefulMethodFamily* dm, QualifiedState state)
kamg@4245 600 : _method(dm), _state_to_restore(state) {}
kamg@4245 601 ~StateRestorer() { destroy(); }
kamg@4245 602 void restore_state() { _method->set_qualification_state(_state_to_restore); }
kamg@4245 603 virtual void destroy() { restore_state(); }
kamg@4245 604 };
kamg@4245 605
kamg@4245 606 StateRestorer* StatefulMethodFamily::record_method_and_dq_further(Method* mo) {
kamg@4245 607 StateRestorer* mark = new StateRestorer(this, _qualification_state);
kamg@4245 608 if (_qualification_state == QUALIFIED) {
acorn@5377 609 _method_family->record_qualified_method(mo);
kamg@4245 610 } else {
acorn@5377 611 _method_family->record_disqualified_method(mo);
kamg@4245 612 }
kamg@4245 613 // Everything found "above"??? this method in the hierarchy walk is set to
kamg@4245 614 // disqualified
kamg@4245 615 set_qualification_state(DISQUALIFIED);
kamg@4245 616 return mark;
kamg@4245 617 }
kamg@4245 618
acorn@5377 619 class StatefulGenericMethodFamilies : public ResourceObj {
kamg@4245 620 private:
acorn@5377 621 GrowableArray<StatefulGenericMethodFamily*> _methods;
kamg@4245 622
kamg@4245 623 public:
acorn@5377 624 StatefulGenericMethodFamily* find_matching(
kamg@4245 625 generic::MethodDescriptor* md, generic::Context* ctx) {
kamg@4245 626 for (int i = 0; i < _methods.length(); ++i) {
acorn@5377 627 StatefulGenericMethodFamily* existing = _methods.at(i);
kamg@4245 628 if (existing->descriptor_matches(md, ctx)) {
kamg@4245 629 return existing;
kamg@4245 630 }
kamg@4245 631 }
kamg@4245 632 return NULL;
kamg@4245 633 }
kamg@4245 634
acorn@5377 635 StatefulGenericMethodFamily* find_matching_or_create(
kamg@4245 636 generic::MethodDescriptor* md, generic::Context* ctx) {
acorn@5377 637 StatefulGenericMethodFamily* method = find_matching(md, ctx);
kamg@4245 638 if (method == NULL) {
acorn@5377 639 method = new StatefulGenericMethodFamily(md, ctx);
kamg@4245 640 _methods.append(method);
kamg@4245 641 }
kamg@4245 642 return method;
kamg@4245 643 }
kamg@4245 644
acorn@5377 645 void extract_families_into(GrowableArray<GenericMethodFamily*>* array) {
kamg@4245 646 for (int i = 0; i < _methods.length(); ++i) {
kamg@4245 647 array->append(_methods.at(i)->get_method_family());
kamg@4245 648 }
kamg@4245 649 }
kamg@4245 650 };
kamg@4245 651
kamg@4245 652 // Represents a location corresponding to a vtable slot for methods that
kamg@4245 653 // neither the class nor any of it's ancestors provide an implementaion.
kamg@4245 654 // Default methods may be present to fill this slot.
kamg@4245 655 class EmptyVtableSlot : public ResourceObj {
kamg@4245 656 private:
kamg@4245 657 Symbol* _name;
kamg@4245 658 Symbol* _signature;
kamg@4245 659 int _size_of_parameters;
kamg@4245 660 MethodFamily* _binding;
kamg@4245 661
kamg@4245 662 public:
kamg@4245 663 EmptyVtableSlot(Method* method)
kamg@4245 664 : _name(method->name()), _signature(method->signature()),
kamg@4245 665 _size_of_parameters(method->size_of_parameters()), _binding(NULL) {}
kamg@4245 666
kamg@4245 667 Symbol* name() const { return _name; }
kamg@4245 668 Symbol* signature() const { return _signature; }
kamg@4245 669 int size_of_parameters() const { return _size_of_parameters; }
kamg@4245 670
kamg@4245 671 void bind_family(MethodFamily* lm) { _binding = lm; }
kamg@4245 672 bool is_bound() { return _binding != NULL; }
kamg@4245 673 MethodFamily* get_binding() { return _binding; }
kamg@4245 674
kamg@4245 675 #ifndef PRODUCT
kamg@4245 676 void print_on(outputStream* str) const {
kamg@4245 677 print_slot(str, name(), signature());
kamg@4245 678 }
kamg@4245 679 #endif // ndef PRODUCT
kamg@4245 680 };
kamg@4245 681
kamg@4245 682 static GrowableArray<EmptyVtableSlot*>* find_empty_vtable_slots(
kamg@4245 683 InstanceKlass* klass, GrowableArray<Method*>* mirandas, TRAPS) {
kamg@4245 684
kamg@4245 685 assert(klass != NULL, "Must be valid class");
kamg@4245 686
kamg@4245 687 GrowableArray<EmptyVtableSlot*>* slots = new GrowableArray<EmptyVtableSlot*>();
kamg@4245 688
kamg@4245 689 // All miranda methods are obvious candidates
kamg@4245 690 for (int i = 0; i < mirandas->length(); ++i) {
kamg@4245 691 EmptyVtableSlot* slot = new EmptyVtableSlot(mirandas->at(i));
kamg@4245 692 slots->append(slot);
kamg@4245 693 }
kamg@4245 694
kamg@4245 695 // Also any overpasses in our superclasses, that we haven't implemented.
kamg@4245 696 // (can't use the vtable because it is not guaranteed to be initialized yet)
kamg@4245 697 InstanceKlass* super = klass->java_super();
kamg@4245 698 while (super != NULL) {
kamg@4245 699 for (int i = 0; i < super->methods()->length(); ++i) {
kamg@4245 700 Method* m = super->methods()->at(i);
kamg@4245 701 if (m->is_overpass()) {
kamg@4245 702 // m is a method that would have been a miranda if not for the
kamg@4245 703 // default method processing that occurred on behalf of our superclass,
kamg@4245 704 // so it's a method we want to re-examine in this new context. That is,
kamg@4245 705 // unless we have a real implementation of it in the current class.
kamg@4245 706 Method* impl = klass->lookup_method(m->name(), m->signature());
kamg@4245 707 if (impl == NULL || impl->is_overpass()) {
kamg@4245 708 slots->append(new EmptyVtableSlot(m));
kamg@4245 709 }
kamg@4245 710 }
kamg@4245 711 }
kamg@4245 712 super = super->java_super();
kamg@4245 713 }
kamg@4245 714
kamg@4245 715 #ifndef PRODUCT
kamg@4245 716 if (TraceDefaultMethods) {
kamg@4245 717 tty->print_cr("Slots that need filling:");
kamg@4245 718 streamIndentor si(tty);
kamg@4245 719 for (int i = 0; i < slots->length(); ++i) {
kamg@4245 720 tty->indent();
kamg@4245 721 slots->at(i)->print_on(tty);
kamg@4245 722 tty->print_cr("");
kamg@4245 723 }
kamg@4245 724 }
kamg@4245 725 #endif // ndef PRODUCT
kamg@4245 726 return slots;
kamg@4245 727 }
kamg@4245 728
acorn@5377 729 // Iterates over the superinterface type hierarchy looking for all methods
acorn@5377 730 // with a specific erased signature.
acorn@5377 731 class FindMethodsByErasedSig : public HierarchyVisitor<FindMethodsByErasedSig> {
acorn@5377 732 private:
acorn@5377 733 // Context data
acorn@5377 734 Symbol* _method_name;
acorn@5377 735 Symbol* _method_signature;
acorn@5377 736 StatefulMethodFamily* _family;
acorn@5377 737
acorn@5377 738 public:
acorn@5377 739 FindMethodsByErasedSig(Symbol* name, Symbol* signature) :
acorn@5377 740 _method_name(name), _method_signature(signature),
acorn@5377 741 _family(NULL) {}
acorn@5377 742
acorn@5377 743 void get_discovered_family(MethodFamily** family) {
acorn@5377 744 if (_family != NULL) {
acorn@5377 745 *family = _family->get_method_family();
acorn@5377 746 } else {
acorn@5377 747 *family = NULL;
acorn@5377 748 }
acorn@5377 749 }
acorn@5377 750
acorn@5377 751 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
acorn@5377 752 void free_node_data(void* node_data) {
acorn@5377 753 PseudoScope::cast(node_data)->destroy();
acorn@5377 754 }
acorn@5377 755
acorn@5377 756 // Find all methods on this hierarchy that match this
acorn@5377 757 // method's erased (name, signature)
acorn@5377 758 bool visit() {
acorn@5377 759 PseudoScope* scope = PseudoScope::cast(current_data());
acorn@5377 760 InstanceKlass* iklass = current_class();
acorn@5377 761
acorn@5377 762 Method* m = iklass->find_method(_method_name, _method_signature);
acorn@5377 763 if (m != NULL) {
acorn@5377 764 if (_family == NULL) {
acorn@5377 765 _family = new StatefulMethodFamily();
acorn@5377 766 }
acorn@5377 767
acorn@5377 768 if (iklass->is_interface()) {
acorn@5377 769 StateRestorer* restorer = _family->record_method_and_dq_further(m);
acorn@5377 770 scope->add_mark(restorer);
acorn@5377 771 } else {
acorn@5377 772 // This is the rule that methods in classes "win" (bad word) over
acorn@5377 773 // methods in interfaces. This works because of single inheritance
acorn@5377 774 _family->set_target_if_empty(m);
acorn@5377 775 }
acorn@5377 776 }
acorn@5377 777 return true;
acorn@5377 778 }
acorn@5377 779
acorn@5377 780 };
acorn@5377 781
kamg@4245 782 // Iterates over the type hierarchy looking for all methods with a specific
kamg@4245 783 // method name. The result of this is a set of method families each of
kamg@4245 784 // which is populated with a set of methods that implement the same
kamg@4245 785 // language-level signature.
acorn@5377 786 class FindMethodsByGenericSig : public HierarchyVisitor<FindMethodsByGenericSig> {
kamg@4245 787 private:
kamg@4245 788 // Context data
kamg@4245 789 Thread* THREAD;
kamg@4245 790 generic::DescriptorCache* _cache;
kamg@4245 791 Symbol* _method_name;
kamg@4245 792 generic::Context* _ctx;
acorn@5377 793 StatefulGenericMethodFamilies _families;
kamg@4245 794
kamg@4245 795 public:
kamg@4245 796
acorn@5377 797 FindMethodsByGenericSig(generic::DescriptorCache* cache, Symbol* name,
kamg@4245 798 generic::Context* ctx, Thread* thread) :
kamg@4245 799 _cache(cache), _method_name(name), _ctx(ctx), THREAD(thread) {}
kamg@4245 800
acorn@5377 801 void get_discovered_families(GrowableArray<GenericMethodFamily*>* methods) {
kamg@4245 802 _families.extract_families_into(methods);
kamg@4245 803 }
kamg@4245 804
kamg@4245 805 void* new_node_data(InstanceKlass* cls) { return new PseudoScope(); }
kamg@4245 806 void free_node_data(void* node_data) {
kamg@4245 807 PseudoScope::cast(node_data)->destroy();
kamg@4245 808 }
kamg@4245 809
kamg@4245 810 bool visit() {
kamg@4245 811 PseudoScope* scope = PseudoScope::cast(current_data());
kamg@4245 812 InstanceKlass* klass = current_class();
kamg@4245 813 InstanceKlass* sub = current_depth() > 0 ? class_at_depth(1) : NULL;
kamg@4245 814
kamg@4245 815 ContextMark* cm = new ContextMark(_ctx->mark());
kamg@4245 816 scope->add_mark(cm); // will restore context when scope is freed
kamg@4245 817
kamg@4245 818 _ctx->apply_type_arguments(sub, klass, THREAD);
kamg@4245 819
kamg@4245 820 int start, end = 0;
kamg@4245 821 start = klass->find_method_by_name(_method_name, &end);
kamg@4245 822 if (start != -1) {
kamg@4245 823 for (int i = start; i < end; ++i) {
kamg@4245 824 Method* m = klass->methods()->at(i);
kamg@4245 825 // This gets the method's parameter list with its generic type
kamg@4245 826 // parameters resolved
kamg@4245 827 generic::MethodDescriptor* md = _cache->descriptor_for(m, THREAD);
kamg@4245 828
kamg@4245 829 // Find all methods on this hierarchy that match this method
kamg@4245 830 // (name, signature). This class collects other families of this
kamg@4245 831 // method name.
acorn@5377 832 StatefulGenericMethodFamily* family =
kamg@4245 833 _families.find_matching_or_create(md, _ctx);
kamg@4245 834
kamg@4245 835 if (klass->is_interface()) {
kamg@4245 836 // ???
kamg@4245 837 StateRestorer* restorer = family->record_method_and_dq_further(m);
kamg@4245 838 scope->add_mark(restorer);
kamg@4245 839 } else {
kamg@4245 840 // This is the rule that methods in classes "win" (bad word) over
kamg@4245 841 // methods in interfaces. This works because of single inheritance
kamg@4245 842 family->set_target_if_empty(m);
kamg@4245 843 }
kamg@4245 844 }
kamg@4245 845 }
kamg@4245 846 return true;
kamg@4245 847 }
kamg@4245 848 };
kamg@4245 849
kamg@4245 850 #ifndef PRODUCT
acorn@5377 851 static void print_generic_families(
acorn@5377 852 GrowableArray<GenericMethodFamily*>* methods, Symbol* match) {
kamg@4245 853 streamIndentor si(tty, 4);
kamg@4245 854 if (methods->length() == 0) {
kamg@4245 855 tty->indent();
kamg@4245 856 tty->print_cr("No Logical Method found");
kamg@4245 857 }
kamg@4245 858 for (int i = 0; i < methods->length(); ++i) {
kamg@4245 859 tty->indent();
acorn@5377 860 GenericMethodFamily* lm = methods->at(i);
kamg@4245 861 if (lm->contains_signature(match)) {
kamg@4245 862 tty->print_cr("<Matching>");
kamg@4245 863 } else {
kamg@4245 864 tty->print_cr("<Non-Matching>");
kamg@4245 865 }
acorn@5377 866 lm->print_sig_on(tty, lm->get_generic_sig(), 1);
kamg@4245 867 }
kamg@4245 868 }
kamg@4245 869 #endif // ndef PRODUCT
kamg@4245 870
acorn@5377 871 static void create_overpasses(
acorn@5377 872 GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS);
acorn@5377 873
acorn@5377 874 static void generate_generic_defaults(
acorn@5377 875 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
acorn@5377 876 EmptyVtableSlot* slot, int current_slot_index, TRAPS) {
acorn@5377 877
acorn@5377 878 if (slot->is_bound()) {
acorn@5377 879 #ifndef PRODUCT
acorn@5377 880 if (TraceDefaultMethods) {
acorn@5377 881 streamIndentor si(tty, 4);
acorn@5377 882 tty->indent().print_cr("Already bound to logical method:");
acorn@5377 883 GenericMethodFamily* lm = (GenericMethodFamily*)(slot->get_binding());
acorn@5377 884 lm->print_sig_on(tty, lm->get_generic_sig(), 1);
acorn@5377 885 }
acorn@5377 886 #endif // ndef PRODUCT
acorn@5377 887 return; // covered by previous processing
acorn@5377 888 }
acorn@5377 889
acorn@5377 890 generic::DescriptorCache cache;
acorn@5377 891
acorn@5377 892 generic::Context ctx(&cache);
acorn@5377 893 FindMethodsByGenericSig visitor(&cache, slot->name(), &ctx, CHECK);
acorn@5377 894 visitor.run(klass);
acorn@5377 895
acorn@5377 896 GrowableArray<GenericMethodFamily*> discovered_families;
acorn@5377 897 visitor.get_discovered_families(&discovered_families);
acorn@5377 898
acorn@5377 899 #ifndef PRODUCT
acorn@5377 900 if (TraceDefaultMethods) {
acorn@5377 901 print_generic_families(&discovered_families, slot->signature());
acorn@5377 902 }
acorn@5377 903 #endif // ndef PRODUCT
acorn@5377 904
acorn@5377 905 // Find and populate any other slots that match the discovered families
acorn@5377 906 for (int j = current_slot_index; j < empty_slots->length(); ++j) {
acorn@5377 907 EmptyVtableSlot* open_slot = empty_slots->at(j);
acorn@5377 908
acorn@5377 909 if (slot->name() == open_slot->name()) {
acorn@5377 910 for (int k = 0; k < discovered_families.length(); ++k) {
acorn@5377 911 GenericMethodFamily* lm = discovered_families.at(k);
acorn@5377 912
acorn@5377 913 if (lm->contains_signature(open_slot->signature())) {
acorn@5377 914 lm->determine_target(klass, CHECK);
acorn@5377 915 open_slot->bind_family(lm);
acorn@5377 916 }
acorn@5377 917 }
acorn@5377 918 }
acorn@5377 919 }
acorn@5377 920 }
acorn@5377 921
acorn@5377 922 static void generate_erased_defaults(
acorn@5377 923 InstanceKlass* klass, GrowableArray<EmptyVtableSlot*>* empty_slots,
acorn@5377 924 EmptyVtableSlot* slot, TRAPS) {
acorn@5377 925
acorn@5377 926 // sets up a set of methods with the same exact erased signature
acorn@5377 927 FindMethodsByErasedSig visitor(slot->name(), slot->signature());
acorn@5377 928 visitor.run(klass);
acorn@5377 929
acorn@5377 930 MethodFamily* family;
acorn@5377 931 visitor.get_discovered_family(&family);
acorn@5377 932 if (family != NULL) {
acorn@5377 933 family->determine_target(klass, CHECK);
acorn@5377 934 slot->bind_family(family);
acorn@5377 935 }
acorn@5377 936 }
acorn@5377 937
kamg@4245 938 static void merge_in_new_methods(InstanceKlass* klass,
kamg@4245 939 GrowableArray<Method*>* new_methods, TRAPS);
kamg@4245 940
kamg@4245 941 // This is the guts of the default methods implementation. This is called just
kamg@4245 942 // after the classfile has been parsed if some ancestor has default methods.
kamg@4245 943 //
kamg@4245 944 // First if finds any name/signature slots that need any implementation (either
kamg@4245 945 // because they are miranda or a superclass's implementation is an overpass
kamg@4245 946 // itself). For each slot, iterate over the hierarchy, using generic signature
kamg@4245 947 // information to partition any methods that match the name into method families
kamg@4245 948 // where each family contains methods whose signatures are equivalent at the
kamg@4245 949 // language level (i.e., their reified parameters match and return values are
kamg@4245 950 // covariant). Check those sets to see if they contain a signature that matches
kamg@4245 951 // the slot we're looking at (if we're lucky, there might be other empty slots
kamg@4245 952 // that we can fill using the same analysis).
kamg@4245 953 //
kamg@4245 954 // For each slot filled, we generate an overpass method that either calls the
kamg@4245 955 // unique default method candidate using invokespecial, or throws an exception
kamg@4245 956 // (in the case of no default method candidates, or more than one valid
kamg@4245 957 // candidate). These methods are then added to the class's method list. If
kamg@4245 958 // the method set we're using contains methods (qualified or not) with a
kamg@4245 959 // different runtime signature than the method we're creating, then we have to
kamg@4245 960 // create bridges with those signatures too.
kamg@4245 961 void DefaultMethods::generate_default_methods(
kamg@4245 962 InstanceKlass* klass, GrowableArray<Method*>* mirandas, TRAPS) {
kamg@4245 963
kamg@4245 964 // This resource mark is the bound for all memory allocation that takes
kamg@4245 965 // place during default method processing. After this goes out of scope,
kamg@4245 966 // all (Resource) objects' memory will be reclaimed. Be careful if adding an
kamg@4245 967 // embedded resource mark under here as that memory can't be used outside
kamg@4245 968 // whatever scope it's in.
kamg@4245 969 ResourceMark rm(THREAD);
kamg@4245 970
kamg@4245 971 // Keep entire hierarchy alive for the duration of the computation
kamg@4245 972 KeepAliveRegistrar keepAlive(THREAD);
kamg@4245 973 KeepAliveVisitor loadKeepAlive(&keepAlive);
kamg@4245 974 loadKeepAlive.run(klass);
kamg@4245 975
kamg@4245 976 #ifndef PRODUCT
kamg@4245 977 if (TraceDefaultMethods) {
kamg@4245 978 ResourceMark rm; // be careful with these!
kamg@4245 979 tty->print_cr("Class %s requires default method processing",
kamg@4245 980 klass->name()->as_klass_external_name());
kamg@4245 981 PrintHierarchy printer;
kamg@4245 982 printer.run(klass);
kamg@4245 983 }
kamg@4245 984 #endif // ndef PRODUCT
kamg@4245 985
kamg@4245 986 GrowableArray<EmptyVtableSlot*>* empty_slots =
kamg@4245 987 find_empty_vtable_slots(klass, mirandas, CHECK);
kamg@4245 988
kamg@4245 989 for (int i = 0; i < empty_slots->length(); ++i) {
kamg@4245 990 EmptyVtableSlot* slot = empty_slots->at(i);
kamg@4245 991 #ifndef PRODUCT
kamg@4245 992 if (TraceDefaultMethods) {
kamg@4245 993 streamIndentor si(tty, 2);
kamg@4245 994 tty->indent().print("Looking for default methods for slot ");
kamg@4245 995 slot->print_on(tty);
kamg@4245 996 tty->print_cr("");
kamg@4245 997 }
kamg@4245 998 #endif // ndef PRODUCT
acorn@5377 999
acorn@5377 1000 if (ParseGenericDefaults) {
acorn@5377 1001 generate_generic_defaults(klass, empty_slots, slot, i, CHECK);
acorn@5377 1002 } else {
acorn@5377 1003 generate_erased_defaults(klass, empty_slots, slot, CHECK);
kamg@4245 1004 }
acorn@5377 1005 }
kamg@4245 1006 #ifndef PRODUCT
kamg@4245 1007 if (TraceDefaultMethods) {
kamg@4245 1008 tty->print_cr("Creating overpasses...");
kamg@4245 1009 }
kamg@4245 1010 #endif // ndef PRODUCT
kamg@4245 1011
kamg@4245 1012 create_overpasses(empty_slots, klass, CHECK);
kamg@4245 1013
kamg@4245 1014 #ifndef PRODUCT
kamg@4245 1015 if (TraceDefaultMethods) {
kamg@4245 1016 tty->print_cr("Default method processing complete");
kamg@4245 1017 }
kamg@4245 1018 #endif // ndef PRODUCT
kamg@4245 1019 }
kamg@4245 1020
kamg@4245 1021 /**
kamg@4245 1022 * Generic analysis was used upon interface '_target' and found a unique
kamg@4245 1023 * default method candidate with generic signature '_method_desc'. This
kamg@4245 1024 * method is only viable if it would also be in the set of default method
kamg@4245 1025 * candidates if we ran a full analysis on the current class.
kamg@4245 1026 *
kamg@4245 1027 * The only reason that the method would not be in the set of candidates for
kamg@4245 1028 * the current class is if that there's another covariantly matching method
kamg@4245 1029 * which is "more specific" than the found method -- i.e., one could find a
kamg@4245 1030 * path in the interface hierarchy in which the matching method appears
kamg@4245 1031 * before we get to '_target'.
kamg@4245 1032 *
kamg@4245 1033 * In order to determine this, we examine all of the implemented
kamg@4245 1034 * interfaces. If we find path that leads to the '_target' interface, then
kamg@4245 1035 * we examine that path to see if there are any methods that would shadow
kamg@4245 1036 * the selected method along that path.
kamg@4245 1037 */
kamg@4245 1038 class ShadowChecker : public HierarchyVisitor<ShadowChecker> {
acorn@5377 1039 protected:
kamg@4245 1040 Thread* THREAD;
kamg@4245 1041
kamg@4245 1042 InstanceKlass* _target;
kamg@4245 1043
kamg@4245 1044 Symbol* _method_name;
kamg@4245 1045 InstanceKlass* _method_holder;
acorn@5377 1046 bool _found_shadow;
acorn@5377 1047
acorn@5377 1048
acorn@5377 1049 public:
acorn@5377 1050
acorn@5377 1051 ShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder,
acorn@5377 1052 InstanceKlass* target)
acorn@5377 1053 : THREAD(thread), _method_name(name), _method_holder(holder),
acorn@5377 1054 _target(target), _found_shadow(false) {}
acorn@5377 1055
acorn@5377 1056 void* new_node_data(InstanceKlass* cls) { return NULL; }
acorn@5377 1057 void free_node_data(void* data) { return; }
acorn@5377 1058
acorn@5377 1059 bool visit() {
acorn@5377 1060 InstanceKlass* ik = current_class();
acorn@5377 1061 if (ik == _target && current_depth() == 1) {
acorn@5377 1062 return false; // This was the specified super -- no need to search it
acorn@5377 1063 }
acorn@5377 1064 if (ik == _method_holder || ik == _target) {
acorn@5377 1065 // We found a path that should be examined to see if it shadows _method
acorn@5377 1066 if (path_has_shadow()) {
acorn@5377 1067 _found_shadow = true;
acorn@5377 1068 cancel_iteration();
acorn@5377 1069 }
acorn@5377 1070 return false; // no need to continue up hierarchy
acorn@5377 1071 }
acorn@5377 1072 return true;
acorn@5377 1073 }
acorn@5377 1074
acorn@5377 1075 virtual bool path_has_shadow() = 0;
acorn@5377 1076 bool found_shadow() { return _found_shadow; }
acorn@5377 1077 };
acorn@5377 1078
acorn@5377 1079 // Used for Invokespecial.
acorn@5377 1080 // Invokespecial is allowed to invoke a concrete interface method
acorn@5377 1081 // and can be used to disambuiguate among qualified candidates,
acorn@5377 1082 // which are methods in immediate superinterfaces,
acorn@5377 1083 // but may not be used to invoke a candidate that would be shadowed
acorn@5377 1084 // from the perspective of the caller.
acorn@5377 1085 // Invokespecial is also used in the overpass generation today
acorn@5377 1086 // We re-run the shadowchecker because we can't distinguish this case,
acorn@5377 1087 // but it should return the same answer, since the overpass target
acorn@5377 1088 // is now the invokespecial caller.
acorn@5377 1089 class ErasedShadowChecker : public ShadowChecker {
acorn@5377 1090 private:
acorn@5377 1091 bool path_has_shadow() {
acorn@5377 1092
acorn@5377 1093 for (int i = current_depth() - 1; i > 0; --i) {
acorn@5377 1094 InstanceKlass* ik = class_at_depth(i);
acorn@5377 1095
acorn@5377 1096 if (ik->is_interface()) {
acorn@5377 1097 int end;
acorn@5377 1098 int start = ik->find_method_by_name(_method_name, &end);
acorn@5377 1099 if (start != -1) {
acorn@5377 1100 return true;
acorn@5377 1101 }
acorn@5377 1102 }
acorn@5377 1103 }
acorn@5377 1104 return false;
acorn@5377 1105 }
acorn@5377 1106 public:
acorn@5377 1107
acorn@5377 1108 ErasedShadowChecker(Thread* thread, Symbol* name, InstanceKlass* holder,
acorn@5377 1109 InstanceKlass* target)
acorn@5377 1110 : ShadowChecker(thread, name, holder, target) {}
acorn@5377 1111 };
acorn@5377 1112
acorn@5377 1113 class GenericShadowChecker : public ShadowChecker {
acorn@5377 1114 private:
acorn@5377 1115 generic::DescriptorCache* _cache;
kamg@4245 1116 generic::MethodDescriptor* _method_desc;
kamg@4245 1117
kamg@4245 1118 bool path_has_shadow() {
kamg@4245 1119 generic::Context ctx(_cache);
kamg@4245 1120
kamg@4245 1121 for (int i = current_depth() - 1; i > 0; --i) {
kamg@4245 1122 InstanceKlass* ik = class_at_depth(i);
kamg@4245 1123 InstanceKlass* sub = class_at_depth(i + 1);
kamg@4245 1124 ctx.apply_type_arguments(sub, ik, THREAD);
kamg@4245 1125
kamg@4245 1126 if (ik->is_interface()) {
kamg@4245 1127 int end;
kamg@4245 1128 int start = ik->find_method_by_name(_method_name, &end);
kamg@4245 1129 if (start != -1) {
kamg@4245 1130 for (int j = start; j < end; ++j) {
kamg@4245 1131 Method* mo = ik->methods()->at(j);
kamg@4245 1132 generic::MethodDescriptor* md = _cache->descriptor_for(mo, THREAD);
kamg@4245 1133 if (_method_desc->covariant_match(md, &ctx)) {
kamg@4245 1134 return true;
kamg@4245 1135 }
kamg@4245 1136 }
kamg@4245 1137 }
kamg@4245 1138 }
kamg@4245 1139 }
kamg@4245 1140 return false;
kamg@4245 1141 }
kamg@4245 1142
kamg@4245 1143 public:
kamg@4245 1144
acorn@5377 1145 GenericShadowChecker(generic::DescriptorCache* cache, Thread* thread,
kamg@4245 1146 Symbol* name, InstanceKlass* holder, generic::MethodDescriptor* desc,
kamg@4245 1147 InstanceKlass* target)
acorn@5377 1148 : ShadowChecker(thread, name, holder, target) {
acorn@5377 1149 _cache = cache;
acorn@5377 1150 _method_desc = desc;
acorn@5377 1151 }
acorn@5377 1152 };
kamg@4245 1153
kamg@4245 1154
acorn@5377 1155
acorn@5377 1156 // Find the unique qualified candidate from the perspective of the super_class
acorn@5377 1157 // which is the resolved_klass, which must be an immediate superinterface
acorn@5377 1158 // of klass
acorn@5377 1159 Method* find_erased_super_default(InstanceKlass* current_class, InstanceKlass* super_class, Symbol* method_name, Symbol* sig, TRAPS) {
acorn@5377 1160
acorn@5377 1161 FindMethodsByErasedSig visitor(method_name, sig);
acorn@5377 1162 visitor.run(super_class); // find candidates from resolved_klass
acorn@5377 1163
acorn@5377 1164 MethodFamily* family;
acorn@5377 1165 visitor.get_discovered_family(&family);
acorn@5377 1166
acorn@5377 1167 if (family != NULL) {
acorn@5377 1168 family->determine_target(current_class, CHECK_NULL); // get target from current_class
kamg@4245 1169 }
kamg@4245 1170
acorn@5377 1171 if (family->has_target()) {
acorn@5377 1172 Method* target = family->get_selected_target();
kamg@4245 1173 InstanceKlass* holder = InstanceKlass::cast(target->method_holder());
kamg@4245 1174
kamg@4245 1175 // Verify that the identified method is valid from the context of
acorn@5377 1176 // the current class, which is the caller class for invokespecial
acorn@5377 1177 // link resolution, i.e. ensure there it is not shadowed.
acorn@5377 1178 // You can use invokespecial to disambiguate interface methods, but
acorn@5377 1179 // you can not use it to skip over an interface method that would shadow it.
acorn@5377 1180 ErasedShadowChecker checker(THREAD, target->name(), holder, super_class);
kamg@4245 1181 checker.run(current_class);
kamg@4245 1182
kamg@4245 1183 if (checker.found_shadow()) {
kamg@4245 1184 #ifndef PRODUCT
kamg@4245 1185 if (TraceDefaultMethods) {
kamg@4245 1186 tty->print_cr(" Only candidate found was shadowed.");
kamg@4245 1187 }
kamg@4245 1188 #endif // ndef PRODUCT
kamg@4245 1189 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
kamg@4245 1190 "Accessible default method not found", NULL);
kamg@4245 1191 } else {
kamg@4245 1192 #ifndef PRODUCT
kamg@4245 1193 if (TraceDefaultMethods) {
acorn@5377 1194 family->print_sig_on(tty, target->signature(), 1);
kamg@4245 1195 }
kamg@4245 1196 #endif // ndef PRODUCT
kamg@4245 1197 return target;
kamg@4245 1198 }
kamg@4245 1199 } else {
acorn@5377 1200 assert(family->throws_exception(), "must have target or throw");
acorn@5377 1201 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
acorn@5377 1202 family->get_exception_message()->as_C_string(), NULL);
acorn@5377 1203 }
acorn@5377 1204 }
acorn@5377 1205
acorn@5377 1206 // super_class is assumed to be the direct super of current_class
acorn@5377 1207 Method* find_generic_super_default( InstanceKlass* current_class,
acorn@5377 1208 InstanceKlass* super_class,
acorn@5377 1209 Symbol* method_name, Symbol* sig, TRAPS) {
acorn@5377 1210 generic::DescriptorCache cache;
acorn@5377 1211 generic::Context ctx(&cache);
acorn@5377 1212
acorn@5377 1213 // Prime the initial generic context for current -> super_class
acorn@5377 1214 ctx.apply_type_arguments(current_class, super_class, CHECK_NULL);
acorn@5377 1215
acorn@5377 1216 FindMethodsByGenericSig visitor(&cache, method_name, &ctx, CHECK_NULL);
acorn@5377 1217 visitor.run(super_class);
acorn@5377 1218
acorn@5377 1219 GrowableArray<GenericMethodFamily*> families;
acorn@5377 1220 visitor.get_discovered_families(&families);
acorn@5377 1221
acorn@5377 1222 #ifndef PRODUCT
acorn@5377 1223 if (TraceDefaultMethods) {
acorn@5377 1224 print_generic_families(&families, sig);
acorn@5377 1225 }
acorn@5377 1226 #endif // ndef PRODUCT
acorn@5377 1227
acorn@5377 1228 GenericMethodFamily* selected_family = NULL;
acorn@5377 1229
acorn@5377 1230 for (int i = 0; i < families.length(); ++i) {
acorn@5377 1231 GenericMethodFamily* lm = families.at(i);
acorn@5377 1232 if (lm->contains_signature(sig)) {
acorn@5377 1233 lm->determine_target(current_class, CHECK_NULL);
acorn@5377 1234 selected_family = lm;
acorn@5377 1235 }
acorn@5377 1236 }
acorn@5377 1237
acorn@5377 1238 if (selected_family->has_target()) {
acorn@5377 1239 Method* target = selected_family->get_selected_target();
acorn@5377 1240 InstanceKlass* holder = InstanceKlass::cast(target->method_holder());
acorn@5377 1241
acorn@5377 1242 // Verify that the identified method is valid from the context of
acorn@5377 1243 // the current class
acorn@5377 1244 GenericShadowChecker checker(&cache, THREAD, target->name(),
acorn@5377 1245 holder, selected_family->descriptor(), super_class);
acorn@5377 1246 checker.run(current_class);
acorn@5377 1247
acorn@5377 1248 if (checker.found_shadow()) {
acorn@5377 1249 #ifndef PRODUCT
acorn@5377 1250 if (TraceDefaultMethods) {
acorn@5377 1251 tty->print_cr(" Only candidate found was shadowed.");
acorn@5377 1252 }
acorn@5377 1253 #endif // ndef PRODUCT
acorn@5377 1254 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
acorn@5377 1255 "Accessible default method not found", NULL);
acorn@5377 1256 } else {
acorn@5377 1257 return target;
acorn@5377 1258 }
acorn@5377 1259 } else {
kamg@4245 1260 assert(selected_family->throws_exception(), "must have target or throw");
kamg@4245 1261 THROW_MSG_(vmSymbols::java_lang_AbstractMethodError(),
kamg@4245 1262 selected_family->get_exception_message()->as_C_string(), NULL);
kamg@4245 1263 }
kamg@4245 1264 }
kamg@4245 1265
acorn@5377 1266 // This is called during linktime when we find an invokespecial call that
acorn@5377 1267 // refers to a direct superinterface. It indicates that we should find the
acorn@5377 1268 // default method in the hierarchy of that superinterface, and if that method
acorn@5377 1269 // would have been a candidate from the point of view of 'this' class, then we
acorn@5377 1270 // return that method.
acorn@5377 1271 // This logic assumes that the super is a direct superclass of the caller
acorn@5377 1272 Method* DefaultMethods::find_super_default(
acorn@5377 1273 Klass* cls, Klass* super, Symbol* method_name, Symbol* sig, TRAPS) {
acorn@5377 1274
acorn@5377 1275 ResourceMark rm(THREAD);
acorn@5377 1276
acorn@5377 1277 assert(cls != NULL && super != NULL, "Need real classes");
acorn@5377 1278
acorn@5377 1279 InstanceKlass* current_class = InstanceKlass::cast(cls);
acorn@5377 1280 InstanceKlass* super_class = InstanceKlass::cast(super);
acorn@5377 1281
acorn@5377 1282 // Keep entire hierarchy alive for the duration of the computation
acorn@5377 1283 KeepAliveRegistrar keepAlive(THREAD);
acorn@5377 1284 KeepAliveVisitor loadKeepAlive(&keepAlive);
acorn@5377 1285 loadKeepAlive.run(current_class); // get hierarchy from current class
acorn@5377 1286
acorn@5377 1287 #ifndef PRODUCT
acorn@5377 1288 if (TraceDefaultMethods) {
acorn@5377 1289 tty->print_cr("Finding super default method %s.%s%s from %s",
acorn@5377 1290 super_class->name()->as_C_string(),
acorn@5377 1291 method_name->as_C_string(), sig->as_C_string(),
acorn@5377 1292 current_class->name()->as_C_string());
acorn@5377 1293 }
acorn@5377 1294 #endif // ndef PRODUCT
acorn@5377 1295
acorn@5377 1296 assert(super_class->is_interface(), "only call for default methods");
acorn@5377 1297
acorn@5377 1298 Method* target = NULL;
acorn@5377 1299 if (ParseGenericDefaults) {
acorn@5377 1300 target = find_generic_super_default(current_class, super_class,
acorn@5377 1301 method_name, sig, CHECK_NULL);
acorn@5377 1302 } else {
acorn@5377 1303 target = find_erased_super_default(current_class, super_class,
acorn@5377 1304 method_name, sig, CHECK_NULL);
acorn@5377 1305 }
acorn@5377 1306
acorn@5377 1307 #ifndef PRODUCT
acorn@5377 1308 if (target != NULL) {
acorn@5377 1309 if (TraceDefaultMethods) {
acorn@5377 1310 tty->print(" Returning ");
acorn@5377 1311 print_method(tty, target, true);
acorn@5377 1312 tty->print_cr("");
acorn@5377 1313 }
acorn@5377 1314 }
acorn@5377 1315 #endif // ndef PRODUCT
acorn@5377 1316 return target;
acorn@5377 1317 }
acorn@5377 1318
acorn@5377 1319 #ifndef PRODUCT
acorn@5377 1320 // Return true is broad type is a covariant return of narrow type
acorn@5377 1321 static bool covariant_return_type(BasicType narrow, BasicType broad) {
acorn@5377 1322 if (narrow == broad) {
acorn@5377 1323 return true;
acorn@5377 1324 }
acorn@5377 1325 if (broad == T_OBJECT) {
acorn@5377 1326 return true;
acorn@5377 1327 }
acorn@5377 1328 return false;
acorn@5377 1329 }
acorn@5377 1330 #endif // ndef PRODUCT
kamg@4245 1331
kamg@4245 1332 static int assemble_redirect(
kamg@4245 1333 BytecodeConstantPool* cp, BytecodeBuffer* buffer,
kamg@4245 1334 Symbol* incoming, Method* target, TRAPS) {
kamg@4245 1335
kamg@4245 1336 BytecodeAssembler assem(buffer, cp);
kamg@4245 1337
kamg@4245 1338 SignatureStream in(incoming, true);
kamg@4245 1339 SignatureStream out(target->signature(), true);
kamg@4245 1340 u2 parameter_count = 0;
kamg@4245 1341
kamg@4245 1342 assem.aload(parameter_count++); // load 'this'
kamg@4245 1343
kamg@4245 1344 while (!in.at_return_type()) {
kamg@4245 1345 assert(!out.at_return_type(), "Parameter counts do not match");
kamg@4245 1346 BasicType bt = in.type();
kamg@4245 1347 assert(out.type() == bt, "Parameter types are not compatible");
kamg@4245 1348 assem.load(bt, parameter_count);
kamg@4245 1349 if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) {
kamg@4245 1350 assem.checkcast(out.as_symbol(THREAD));
kamg@4245 1351 } else if (bt == T_LONG || bt == T_DOUBLE) {
kamg@4245 1352 ++parameter_count; // longs and doubles use two slots
kamg@4245 1353 }
kamg@4245 1354 ++parameter_count;
kamg@4245 1355 in.next();
kamg@4245 1356 out.next();
kamg@4245 1357 }
kamg@4245 1358 assert(out.at_return_type(), "Parameter counts do not match");
acorn@5377 1359 assert(covariant_return_type(out.type(), in.type()), "Return types are not compatible");
kamg@4245 1360
kamg@4245 1361 if (parameter_count == 1 && (in.type() == T_LONG || in.type() == T_DOUBLE)) {
kamg@4245 1362 ++parameter_count; // need room for return value
kamg@4245 1363 }
kamg@4245 1364 if (target->method_holder()->is_interface()) {
kamg@4245 1365 assem.invokespecial(target);
kamg@4245 1366 } else {
kamg@4245 1367 assem.invokevirtual(target);
kamg@4245 1368 }
kamg@4245 1369
kamg@4245 1370 if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) {
kamg@4245 1371 assem.checkcast(in.as_symbol(THREAD));
kamg@4245 1372 }
kamg@4245 1373 assem._return(in.type());
kamg@4245 1374 return parameter_count;
kamg@4245 1375 }
kamg@4245 1376
kamg@4245 1377 static int assemble_abstract_method_error(
kamg@4245 1378 BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* message, TRAPS) {
kamg@4245 1379
kamg@4245 1380 Symbol* errorName = vmSymbols::java_lang_AbstractMethodError();
kamg@4245 1381 Symbol* init = vmSymbols::object_initializer_name();
kamg@4245 1382 Symbol* sig = vmSymbols::string_void_signature();
kamg@4245 1383
kamg@4245 1384 BytecodeAssembler assem(buffer, cp);
kamg@4245 1385
kamg@4245 1386 assem._new(errorName);
kamg@4245 1387 assem.dup();
kamg@4245 1388 assem.load_string(message);
kamg@4245 1389 assem.invokespecial(errorName, init, sig);
kamg@4245 1390 assem.athrow();
kamg@4245 1391
kamg@4245 1392 return 3; // max stack size: [ exception, exception, string ]
kamg@4245 1393 }
kamg@4245 1394
kamg@4245 1395 static Method* new_method(
kamg@4245 1396 BytecodeConstantPool* cp, BytecodeBuffer* bytecodes, Symbol* name,
kamg@4245 1397 Symbol* sig, AccessFlags flags, int max_stack, int params,
kamg@4245 1398 ConstMethod::MethodType mt, TRAPS) {
kamg@4245 1399
acorn@5377 1400 address code_start = 0;
acorn@5377 1401 int code_length = 0;
coleenp@4572 1402 InlineTableSizes sizes;
kamg@4245 1403
acorn@5377 1404 if (bytecodes != NULL && bytecodes->length() > 0) {
acorn@5377 1405 code_start = static_cast<address>(bytecodes->adr_at(0));
acorn@5377 1406 code_length = bytecodes->length();
acorn@5377 1407 }
acorn@5377 1408
kamg@4245 1409 Method* m = Method::allocate(cp->pool_holder()->class_loader_data(),
coleenp@4572 1410 code_length, flags, &sizes,
coleenp@4398 1411 mt, CHECK_NULL);
kamg@4245 1412
kamg@4245 1413 m->set_constants(NULL); // This will get filled in later
kamg@4245 1414 m->set_name_index(cp->utf8(name));
kamg@4245 1415 m->set_signature_index(cp->utf8(sig));
kamg@4245 1416 #ifdef CC_INTERP
kamg@4245 1417 ResultTypeFinder rtf(sig);
kamg@4245 1418 m->set_result_index(rtf.type());
kamg@4245 1419 #endif
kamg@4245 1420 m->set_size_of_parameters(params);
kamg@4245 1421 m->set_max_stack(max_stack);
kamg@4245 1422 m->set_max_locals(params);
kamg@4245 1423 m->constMethod()->set_stackmap_data(NULL);
kamg@4245 1424 m->set_code(code_start);
kamg@4245 1425 m->set_force_inline(true);
kamg@4245 1426
kamg@4245 1427 return m;
kamg@4245 1428 }
kamg@4245 1429
kamg@4245 1430 static void switchover_constant_pool(BytecodeConstantPool* bpool,
kamg@4245 1431 InstanceKlass* klass, GrowableArray<Method*>* new_methods, TRAPS) {
kamg@4245 1432
kamg@4245 1433 if (new_methods->length() > 0) {
kamg@4245 1434 ConstantPool* cp = bpool->create_constant_pool(CHECK);
kamg@4245 1435 if (cp != klass->constants()) {
kamg@4245 1436 klass->class_loader_data()->add_to_deallocate_list(klass->constants());
kamg@4245 1437 klass->set_constants(cp);
kamg@4245 1438 cp->set_pool_holder(klass);
kamg@4245 1439
kamg@4245 1440 for (int i = 0; i < new_methods->length(); ++i) {
kamg@4245 1441 new_methods->at(i)->set_constants(cp);
kamg@4245 1442 }
kamg@4245 1443 for (int i = 0; i < klass->methods()->length(); ++i) {
kamg@4245 1444 Method* mo = klass->methods()->at(i);
kamg@4245 1445 mo->set_constants(cp);
kamg@4245 1446 }
kamg@4245 1447 }
kamg@4245 1448 }
kamg@4245 1449 }
kamg@4245 1450
kamg@4245 1451 // A "bridge" is a method created by javac to bridge the gap between
kamg@4245 1452 // an implementation and a generically-compatible, but different, signature.
kamg@4245 1453 // Bridges have actual bytecode implementation in classfiles.
kamg@4245 1454 // An "overpass", on the other hand, performs the same function as a bridge
kamg@4245 1455 // but does not occur in a classfile; the VM creates overpass itself,
kamg@4245 1456 // when it needs a path to get from a call site to an default method, and
kamg@4245 1457 // a bridge doesn't exist.
kamg@4245 1458 static void create_overpasses(
kamg@4245 1459 GrowableArray<EmptyVtableSlot*>* slots,
kamg@4245 1460 InstanceKlass* klass, TRAPS) {
kamg@4245 1461
kamg@4245 1462 GrowableArray<Method*> overpasses;
kamg@4245 1463 BytecodeConstantPool bpool(klass->constants());
kamg@4245 1464
kamg@4245 1465 for (int i = 0; i < slots->length(); ++i) {
kamg@4245 1466 EmptyVtableSlot* slot = slots->at(i);
kamg@4245 1467
kamg@4245 1468 if (slot->is_bound()) {
kamg@4245 1469 MethodFamily* method = slot->get_binding();
kamg@4245 1470 int max_stack = 0;
kamg@4245 1471 BytecodeBuffer buffer;
kamg@4245 1472
kamg@4245 1473 #ifndef PRODUCT
kamg@4245 1474 if (TraceDefaultMethods) {
kamg@4245 1475 tty->print("for slot: ");
kamg@4245 1476 slot->print_on(tty);
kamg@4245 1477 tty->print_cr("");
kamg@4245 1478 if (method->has_target()) {
kamg@4245 1479 method->print_selected(tty, 1);
kamg@4245 1480 } else {
kamg@4245 1481 method->print_exception(tty, 1);
kamg@4245 1482 }
kamg@4245 1483 }
kamg@4245 1484 #endif // ndef PRODUCT
kamg@4245 1485 if (method->has_target()) {
kamg@4245 1486 Method* selected = method->get_selected_target();
kamg@4245 1487 max_stack = assemble_redirect(
kamg@4245 1488 &bpool, &buffer, slot->signature(), selected, CHECK);
kamg@4245 1489 } else if (method->throws_exception()) {
kamg@4245 1490 max_stack = assemble_abstract_method_error(
kamg@4245 1491 &bpool, &buffer, method->get_exception_message(), CHECK);
kamg@4245 1492 }
kamg@4245 1493 AccessFlags flags = accessFlags_from(
kamg@4245 1494 JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE);
kamg@4245 1495 Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(),
kamg@4245 1496 flags, max_stack, slot->size_of_parameters(),
kamg@4245 1497 ConstMethod::OVERPASS, CHECK);
kamg@4245 1498 if (m != NULL) {
kamg@4245 1499 overpasses.push(m);
kamg@4245 1500 }
kamg@4245 1501 }
kamg@4245 1502 }
kamg@4245 1503
kamg@4245 1504 #ifndef PRODUCT
kamg@4245 1505 if (TraceDefaultMethods) {
kamg@4245 1506 tty->print_cr("Created %d overpass methods", overpasses.length());
kamg@4245 1507 }
kamg@4245 1508 #endif // ndef PRODUCT
kamg@4245 1509
kamg@4245 1510 switchover_constant_pool(&bpool, klass, &overpasses, CHECK);
kamg@4245 1511 merge_in_new_methods(klass, &overpasses, CHECK);
kamg@4245 1512 }
kamg@4245 1513
kamg@4245 1514 static void sort_methods(GrowableArray<Method*>* methods) {
kamg@4245 1515 // Note that this must sort using the same key as is used for sorting
kamg@4245 1516 // methods in InstanceKlass.
kamg@4245 1517 bool sorted = true;
kamg@4245 1518 for (int i = methods->length() - 1; i > 0; --i) {
kamg@4245 1519 for (int j = 0; j < i; ++j) {
kamg@4245 1520 Method* m1 = methods->at(j);
kamg@4245 1521 Method* m2 = methods->at(j + 1);
kamg@4245 1522 if ((uintptr_t)m1->name() > (uintptr_t)m2->name()) {
kamg@4245 1523 methods->at_put(j, m2);
kamg@4245 1524 methods->at_put(j + 1, m1);
kamg@4245 1525 sorted = false;
kamg@4245 1526 }
kamg@4245 1527 }
kamg@4245 1528 if (sorted) break;
kamg@4245 1529 sorted = true;
kamg@4245 1530 }
kamg@4245 1531 #ifdef ASSERT
kamg@4245 1532 uintptr_t prev = 0;
kamg@4245 1533 for (int i = 0; i < methods->length(); ++i) {
kamg@4245 1534 Method* mh = methods->at(i);
kamg@4245 1535 uintptr_t nv = (uintptr_t)mh->name();
kamg@4245 1536 assert(nv >= prev, "Incorrect overpass method ordering");
kamg@4245 1537 prev = nv;
kamg@4245 1538 }
kamg@4245 1539 #endif
kamg@4245 1540 }
kamg@4245 1541
kamg@4245 1542 static void merge_in_new_methods(InstanceKlass* klass,
kamg@4245 1543 GrowableArray<Method*>* new_methods, TRAPS) {
kamg@4245 1544
kamg@4245 1545 enum { ANNOTATIONS, PARAMETERS, DEFAULTS, NUM_ARRAYS };
kamg@4245 1546
kamg@4245 1547 Array<Method*>* original_methods = klass->methods();
kamg@4245 1548 Array<int>* original_ordering = klass->method_ordering();
kamg@4245 1549 Array<int>* merged_ordering = Universe::the_empty_int_array();
kamg@4245 1550
kamg@4245 1551 int new_size = klass->methods()->length() + new_methods->length();
kamg@4245 1552
kamg@4245 1553 Array<Method*>* merged_methods = MetadataFactory::new_array<Method*>(
kamg@4245 1554 klass->class_loader_data(), new_size, NULL, CHECK);
coleenp@4572 1555
kamg@4245 1556 if (original_ordering != NULL && original_ordering->length() > 0) {
kamg@4245 1557 merged_ordering = MetadataFactory::new_array<int>(
kamg@4245 1558 klass->class_loader_data(), new_size, CHECK);
kamg@4245 1559 }
kamg@4245 1560 int method_order_index = klass->methods()->length();
kamg@4245 1561
kamg@4245 1562 sort_methods(new_methods);
kamg@4245 1563
kamg@4245 1564 // Perform grand merge of existing methods and new methods
kamg@4245 1565 int orig_idx = 0;
kamg@4245 1566 int new_idx = 0;
kamg@4245 1567
kamg@4245 1568 for (int i = 0; i < new_size; ++i) {
kamg@4245 1569 Method* orig_method = NULL;
kamg@4245 1570 Method* new_method = NULL;
kamg@4245 1571 if (orig_idx < original_methods->length()) {
kamg@4245 1572 orig_method = original_methods->at(orig_idx);
kamg@4245 1573 }
kamg@4245 1574 if (new_idx < new_methods->length()) {
kamg@4245 1575 new_method = new_methods->at(new_idx);
kamg@4245 1576 }
kamg@4245 1577
kamg@4245 1578 if (orig_method != NULL &&
kamg@4245 1579 (new_method == NULL || orig_method->name() < new_method->name())) {
kamg@4245 1580 merged_methods->at_put(i, orig_method);
kamg@4245 1581 original_methods->at_put(orig_idx, NULL);
kamg@4245 1582 if (merged_ordering->length() > 0) {
kamg@4245 1583 merged_ordering->at_put(i, original_ordering->at(orig_idx));
kamg@4245 1584 }
kamg@4245 1585 ++orig_idx;
kamg@4245 1586 } else {
kamg@4245 1587 merged_methods->at_put(i, new_method);
kamg@4245 1588 if (merged_ordering->length() > 0) {
kamg@4245 1589 merged_ordering->at_put(i, method_order_index++);
kamg@4245 1590 }
kamg@4245 1591 ++new_idx;
kamg@4245 1592 }
kamg@4245 1593 // update idnum for new location
kamg@4245 1594 merged_methods->at(i)->set_method_idnum(i);
kamg@4245 1595 }
kamg@4245 1596
kamg@4245 1597 // Verify correct order
kamg@4245 1598 #ifdef ASSERT
kamg@4245 1599 uintptr_t prev = 0;
kamg@4245 1600 for (int i = 0; i < merged_methods->length(); ++i) {
kamg@4245 1601 Method* mo = merged_methods->at(i);
kamg@4245 1602 uintptr_t nv = (uintptr_t)mo->name();
kamg@4245 1603 assert(nv >= prev, "Incorrect method ordering");
kamg@4245 1604 prev = nv;
kamg@4245 1605 }
kamg@4245 1606 #endif
kamg@4245 1607
kamg@4245 1608 // Replace klass methods with new merged lists
kamg@4245 1609 klass->set_methods(merged_methods);
sspitsyn@5209 1610 klass->set_initial_method_idnum(new_size);
kamg@4245 1611
kamg@4245 1612 ClassLoaderData* cld = klass->class_loader_data();
kamg@4245 1613 MetadataFactory::free_array(cld, original_methods);
kamg@4245 1614 if (original_ordering->length() > 0) {
kamg@4245 1615 klass->set_method_ordering(merged_ordering);
kamg@4245 1616 MetadataFactory::free_array(cld, original_ordering);
kamg@4245 1617 }
kamg@4245 1618 }
kamg@4245 1619

mercurial