src/share/vm/classfile/defaultMethods.cpp

Tue, 18 Mar 2014 19:07:22 +0100

author
pliden
date
Tue, 18 Mar 2014 19:07:22 +0100
changeset 6413
595c0f60d50d
parent 6256
c3f3cfd39184
child 6632
386dd1c71858
permissions
-rw-r--r--

8029075: String deduplication in G1
Summary: Implementation of JEP 192, http://openjdk.java.net/jeps/192
Reviewed-by: brutisso, tschatzl, coleenp

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

mercurial