Mon, 07 Oct 2013 12:20:28 -0400
8009130: Lambda: Fix access controls, loader constraints.
Summary: New default methods list with inherited superinterface methods
Reviewed-by: minqi, sspitsyn, coleenp
1.1 --- a/src/share/vm/classfile/classFileParser.cpp Sun Oct 06 16:13:50 2013 +0200 1.2 +++ b/src/share/vm/classfile/classFileParser.cpp Mon Oct 07 12:20:28 2013 -0400 1.3 @@ -4080,8 +4080,7 @@ 1.4 1.5 // Generate any default methods - default methods are interface methods 1.6 // that have a default implementation. This is new with Lambda project. 1.7 - if (has_default_methods && !access_flags.is_interface() && 1.8 - local_interfaces->length() > 0) { 1.9 + if (has_default_methods && !access_flags.is_interface() ) { 1.10 DefaultMethods::generate_default_methods( 1.11 this_klass(), &all_mirandas, CHECK_(nullHandle)); 1.12 }
2.1 --- a/src/share/vm/classfile/defaultMethods.cpp Sun Oct 06 16:13:50 2013 +0200 2.2 +++ b/src/share/vm/classfile/defaultMethods.cpp Mon Oct 07 12:20:28 2013 -0400 2.3 @@ -345,7 +345,6 @@ 2.4 } 2.5 2.6 Symbol* generate_no_defaults_message(TRAPS) const; 2.7 - Symbol* generate_abstract_method_message(Method* method, TRAPS) const; 2.8 Symbol* generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const; 2.9 2.10 public: 2.11 @@ -404,20 +403,19 @@ 2.12 _exception_message = generate_no_defaults_message(CHECK); 2.13 _exception_name = vmSymbols::java_lang_AbstractMethodError(); 2.14 } else if (qualified_methods.length() == 1) { 2.15 + // leave abstract methods alone, they will be found via normal search path 2.16 Method* method = qualified_methods.at(0); 2.17 - if (method->is_abstract()) { 2.18 - _exception_message = generate_abstract_method_message(method, CHECK); 2.19 - _exception_name = vmSymbols::java_lang_AbstractMethodError(); 2.20 - } else { 2.21 + if (!method->is_abstract()) { 2.22 _selected_target = qualified_methods.at(0); 2.23 } 2.24 } else { 2.25 _exception_message = generate_conflicts_message(&qualified_methods,CHECK); 2.26 _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError(); 2.27 + if (TraceDefaultMethods) { 2.28 + _exception_message->print_value_on(tty); 2.29 + tty->print_cr(""); 2.30 + } 2.31 } 2.32 - 2.33 - assert((has_target() ^ throws_exception()) == 1, 2.34 - "One and only one must be true"); 2.35 } 2.36 2.37 bool contains_signature(Symbol* query) { 2.38 @@ -475,20 +473,6 @@ 2.39 return SymbolTable::new_symbol("No qualifying defaults found", CHECK_NULL); 2.40 } 2.41 2.42 -Symbol* MethodFamily::generate_abstract_method_message(Method* method, TRAPS) const { 2.43 - Symbol* klass = method->klass_name(); 2.44 - Symbol* name = method->name(); 2.45 - Symbol* sig = method->signature(); 2.46 - stringStream ss; 2.47 - ss.print("Method "); 2.48 - ss.write((const char*)klass->bytes(), klass->utf8_length()); 2.49 - ss.print("."); 2.50 - ss.write((const char*)name->bytes(), name->utf8_length()); 2.51 - ss.write((const char*)sig->bytes(), sig->utf8_length()); 2.52 - ss.print(" is abstract"); 2.53 - return SymbolTable::new_symbol(ss.base(), (int)ss.size(), CHECK_NULL); 2.54 -} 2.55 - 2.56 Symbol* MethodFamily::generate_conflicts_message(GrowableArray<Method*>* methods, TRAPS) const { 2.57 stringStream ss; 2.58 ss.print("Conflicting default methods:"); 2.59 @@ -595,6 +579,18 @@ 2.60 #endif // ndef PRODUCT 2.61 }; 2.62 2.63 +static bool already_in_vtable_slots(GrowableArray<EmptyVtableSlot*>* slots, Method* m) { 2.64 + bool found = false; 2.65 + for (int j = 0; j < slots->length(); ++j) { 2.66 + if (slots->at(j)->name() == m->name() && 2.67 + slots->at(j)->signature() == m->signature() ) { 2.68 + found = true; 2.69 + break; 2.70 + } 2.71 + } 2.72 + return found; 2.73 +} 2.74 + 2.75 static GrowableArray<EmptyVtableSlot*>* find_empty_vtable_slots( 2.76 InstanceKlass* klass, GrowableArray<Method*>* mirandas, TRAPS) { 2.77 2.78 @@ -604,8 +600,10 @@ 2.79 2.80 // All miranda methods are obvious candidates 2.81 for (int i = 0; i < mirandas->length(); ++i) { 2.82 - EmptyVtableSlot* slot = new EmptyVtableSlot(mirandas->at(i)); 2.83 - slots->append(slot); 2.84 + Method* m = mirandas->at(i); 2.85 + if (!already_in_vtable_slots(slots, m)) { 2.86 + slots->append(new EmptyVtableSlot(m)); 2.87 + } 2.88 } 2.89 2.90 // Also any overpasses in our superclasses, that we haven't implemented. 2.91 @@ -621,7 +619,26 @@ 2.92 // unless we have a real implementation of it in the current class. 2.93 Method* impl = klass->lookup_method(m->name(), m->signature()); 2.94 if (impl == NULL || impl->is_overpass()) { 2.95 - slots->append(new EmptyVtableSlot(m)); 2.96 + if (!already_in_vtable_slots(slots, m)) { 2.97 + slots->append(new EmptyVtableSlot(m)); 2.98 + } 2.99 + } 2.100 + } 2.101 + } 2.102 + 2.103 + // also any default methods in our superclasses 2.104 + if (super->default_methods() != NULL) { 2.105 + for (int i = 0; i < super->default_methods()->length(); ++i) { 2.106 + Method* m = super->default_methods()->at(i); 2.107 + // m is a method that would have been a miranda if not for the 2.108 + // default method processing that occurred on behalf of our superclass, 2.109 + // so it's a method we want to re-examine in this new context. That is, 2.110 + // unless we have a real implementation of it in the current class. 2.111 + Method* impl = klass->lookup_method(m->name(), m->signature()); 2.112 + if (impl == NULL || impl->is_overpass()) { 2.113 + if (!already_in_vtable_slots(slots, m)) { 2.114 + slots->append(new EmptyVtableSlot(m)); 2.115 + } 2.116 } 2.117 } 2.118 } 2.119 @@ -679,7 +696,7 @@ 2.120 // private interface methods are not candidates for default methods 2.121 // invokespecial to private interface methods doesn't use default method logic 2.122 // future: take access controls into account for superclass methods 2.123 - if (m != NULL && (!iklass->is_interface() || m->is_public())) { 2.124 + if (m != NULL && !m->is_static() && (!iklass->is_interface() || m->is_public())) { 2.125 if (_family == NULL) { 2.126 _family = new StatefulMethodFamily(); 2.127 } 2.128 @@ -700,7 +717,7 @@ 2.129 2.130 2.131 2.132 -static void create_overpasses( 2.133 +static void create_defaults_and_exceptions( 2.134 GrowableArray<EmptyVtableSlot*>* slots, InstanceKlass* klass, TRAPS); 2.135 2.136 static void generate_erased_defaults( 2.137 @@ -721,6 +738,8 @@ 2.138 2.139 static void merge_in_new_methods(InstanceKlass* klass, 2.140 GrowableArray<Method*>* new_methods, TRAPS); 2.141 +static void create_default_methods( InstanceKlass* klass, 2.142 + GrowableArray<Method*>* new_methods, TRAPS); 2.143 2.144 // This is the guts of the default methods implementation. This is called just 2.145 // after the classfile has been parsed if some ancestor has default methods. 2.146 @@ -782,7 +801,7 @@ 2.147 } 2.148 #endif // ndef PRODUCT 2.149 2.150 - create_overpasses(empty_slots, klass, CHECK); 2.151 + create_defaults_and_exceptions(empty_slots, klass, CHECK); 2.152 2.153 #ifndef PRODUCT 2.154 if (TraceDefaultMethods) { 2.155 @@ -791,66 +810,6 @@ 2.156 #endif // ndef PRODUCT 2.157 } 2.158 2.159 - 2.160 - 2.161 -#ifdef ASSERT 2.162 -// Return true is broad type is a covariant return of narrow type 2.163 -static bool covariant_return_type(BasicType narrow, BasicType broad) { 2.164 - if (narrow == broad) { 2.165 - return true; 2.166 - } 2.167 - if (broad == T_OBJECT) { 2.168 - return true; 2.169 - } 2.170 - return false; 2.171 -} 2.172 -#endif 2.173 - 2.174 -static int assemble_redirect( 2.175 - BytecodeConstantPool* cp, BytecodeBuffer* buffer, 2.176 - Symbol* incoming, Method* target, TRAPS) { 2.177 - 2.178 - BytecodeAssembler assem(buffer, cp); 2.179 - 2.180 - SignatureStream in(incoming, true); 2.181 - SignatureStream out(target->signature(), true); 2.182 - u2 parameter_count = 0; 2.183 - 2.184 - assem.aload(parameter_count++); // load 'this' 2.185 - 2.186 - while (!in.at_return_type()) { 2.187 - assert(!out.at_return_type(), "Parameter counts do not match"); 2.188 - BasicType bt = in.type(); 2.189 - assert(out.type() == bt, "Parameter types are not compatible"); 2.190 - assem.load(bt, parameter_count); 2.191 - if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) { 2.192 - assem.checkcast(out.as_symbol(THREAD)); 2.193 - } else if (bt == T_LONG || bt == T_DOUBLE) { 2.194 - ++parameter_count; // longs and doubles use two slots 2.195 - } 2.196 - ++parameter_count; 2.197 - in.next(); 2.198 - out.next(); 2.199 - } 2.200 - assert(out.at_return_type(), "Parameter counts do not match"); 2.201 - assert(covariant_return_type(out.type(), in.type()), "Return types are not compatible"); 2.202 - 2.203 - if (parameter_count == 1 && (in.type() == T_LONG || in.type() == T_DOUBLE)) { 2.204 - ++parameter_count; // need room for return value 2.205 - } 2.206 - if (target->method_holder()->is_interface()) { 2.207 - assem.invokespecial(target); 2.208 - } else { 2.209 - assem.invokevirtual(target); 2.210 - } 2.211 - 2.212 - if (in.is_object() && in.as_symbol(THREAD) != out.as_symbol(THREAD)) { 2.213 - assem.checkcast(in.as_symbol(THREAD)); 2.214 - } 2.215 - assem._return(in.type()); 2.216 - return parameter_count; 2.217 -} 2.218 - 2.219 static int assemble_method_error( 2.220 BytecodeConstantPool* cp, BytecodeBuffer* buffer, Symbol* errorName, Symbol* message, TRAPS) { 2.221 2.222 @@ -924,18 +883,18 @@ 2.223 } 2.224 } 2.225 2.226 -// A "bridge" is a method created by javac to bridge the gap between 2.227 -// an implementation and a generically-compatible, but different, signature. 2.228 -// Bridges have actual bytecode implementation in classfiles. 2.229 -// An "overpass", on the other hand, performs the same function as a bridge 2.230 -// but does not occur in a classfile; the VM creates overpass itself, 2.231 -// when it needs a path to get from a call site to an default method, and 2.232 -// a bridge doesn't exist. 2.233 -static void create_overpasses( 2.234 +// Create default_methods list for the current class. 2.235 +// With the VM only processing erased signatures, the VM only 2.236 +// creates an overpass in a conflict case or a case with no candidates. 2.237 +// This allows virtual methods to override the overpass, but ensures 2.238 +// that a local method search will find the exception rather than an abstract 2.239 +// or default method that is not a valid candidate. 2.240 +static void create_defaults_and_exceptions( 2.241 GrowableArray<EmptyVtableSlot*>* slots, 2.242 InstanceKlass* klass, TRAPS) { 2.243 2.244 GrowableArray<Method*> overpasses; 2.245 + GrowableArray<Method*> defaults; 2.246 BytecodeConstantPool bpool(klass->constants()); 2.247 2.248 for (int i = 0; i < slots->length(); ++i) { 2.249 @@ -943,7 +902,6 @@ 2.250 2.251 if (slot->is_bound()) { 2.252 MethodFamily* method = slot->get_binding(); 2.253 - int max_stack = 0; 2.254 BytecodeBuffer buffer; 2.255 2.256 #ifndef PRODUCT 2.257 @@ -953,26 +911,27 @@ 2.258 tty->print_cr(""); 2.259 if (method->has_target()) { 2.260 method->print_selected(tty, 1); 2.261 - } else { 2.262 + } else if (method->throws_exception()) { 2.263 method->print_exception(tty, 1); 2.264 } 2.265 } 2.266 #endif // ndef PRODUCT 2.267 + 2.268 if (method->has_target()) { 2.269 Method* selected = method->get_selected_target(); 2.270 if (selected->method_holder()->is_interface()) { 2.271 - max_stack = assemble_redirect( 2.272 - &bpool, &buffer, slot->signature(), selected, CHECK); 2.273 + defaults.push(selected); 2.274 } 2.275 } else if (method->throws_exception()) { 2.276 - max_stack = assemble_method_error(&bpool, &buffer, method->get_exception_name(), method->get_exception_message(), CHECK); 2.277 - } 2.278 - if (max_stack != 0) { 2.279 + int max_stack = assemble_method_error(&bpool, &buffer, 2.280 + method->get_exception_name(), method->get_exception_message(), CHECK); 2.281 AccessFlags flags = accessFlags_from( 2.282 JVM_ACC_PUBLIC | JVM_ACC_SYNTHETIC | JVM_ACC_BRIDGE); 2.283 - Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(), 2.284 + Method* m = new_method(&bpool, &buffer, slot->name(), slot->signature(), 2.285 flags, max_stack, slot->size_of_parameters(), 2.286 ConstMethod::OVERPASS, CHECK); 2.287 + // We push to the methods list: 2.288 + // overpass methods which are exception throwing methods 2.289 if (m != NULL) { 2.290 overpasses.push(m); 2.291 } 2.292 @@ -983,11 +942,31 @@ 2.293 #ifndef PRODUCT 2.294 if (TraceDefaultMethods) { 2.295 tty->print_cr("Created %d overpass methods", overpasses.length()); 2.296 + tty->print_cr("Created %d default methods", defaults.length()); 2.297 } 2.298 #endif // ndef PRODUCT 2.299 2.300 - switchover_constant_pool(&bpool, klass, &overpasses, CHECK); 2.301 - merge_in_new_methods(klass, &overpasses, CHECK); 2.302 + if (overpasses.length() > 0) { 2.303 + switchover_constant_pool(&bpool, klass, &overpasses, CHECK); 2.304 + merge_in_new_methods(klass, &overpasses, CHECK); 2.305 + } 2.306 + if (defaults.length() > 0) { 2.307 + create_default_methods(klass, &defaults, CHECK); 2.308 + } 2.309 +} 2.310 + 2.311 +static void create_default_methods( InstanceKlass* klass, 2.312 + GrowableArray<Method*>* new_methods, TRAPS) { 2.313 + 2.314 + int new_size = new_methods->length(); 2.315 + Array<Method*>* total_default_methods = MetadataFactory::new_array<Method*>( 2.316 + klass->class_loader_data(), new_size, NULL, CHECK); 2.317 + for (int index = 0; index < new_size; index++ ) { 2.318 + total_default_methods->at_put(index, new_methods->at(index)); 2.319 + } 2.320 + Method::sort_methods(total_default_methods, false, false); 2.321 + 2.322 + klass->set_default_methods(total_default_methods); 2.323 } 2.324 2.325 static void sort_methods(GrowableArray<Method*>* methods) {
3.1 --- a/src/share/vm/code/dependencies.cpp Sun Oct 06 16:13:50 2013 +0200 3.2 +++ b/src/share/vm/code/dependencies.cpp Mon Oct 07 12:20:28 2013 -0400 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3.6 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -812,8 +812,8 @@ 3.11 Klass* k = ctxk; 3.12 Method* lm = k->lookup_method(m->name(), m->signature()); 3.13 if (lm == NULL && k->oop_is_instance()) { 3.14 - // It might be an abstract interface method, devoid of mirandas. 3.15 - lm = ((InstanceKlass*)k)->lookup_method_in_all_interfaces(m->name(), 3.16 + // It might be an interface method 3.17 + lm = ((InstanceKlass*)k)->lookup_method_in_ordered_interfaces(m->name(), 3.18 m->signature()); 3.19 } 3.20 if (lm == m)
4.1 --- a/src/share/vm/interpreter/linkResolver.cpp Sun Oct 06 16:13:50 2013 +0200 4.2 +++ b/src/share/vm/interpreter/linkResolver.cpp Mon Oct 07 12:20:28 2013 -0400 4.3 @@ -1,4 +1,5 @@ 4.4 /* 4.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 4.6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 * 4.8 * This code is free software; you can redistribute it and/or modify it 4.9 @@ -221,8 +222,17 @@ 4.10 // 4.11 // According to JVM spec. $5.4.3c & $5.4.3d 4.12 4.13 +// Look up method in klasses, including static methods 4.14 +// Then look up local default methods 4.15 void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { 4.16 Method* result_oop = klass->uncached_lookup_method(name, signature); 4.17 + if (result_oop == NULL) { 4.18 + Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); 4.19 + if (default_methods != NULL) { 4.20 + result_oop = InstanceKlass::find_method(default_methods, name, signature); 4.21 + } 4.22 + } 4.23 + 4.24 if (EnableInvokeDynamic && result_oop != NULL) { 4.25 vmIntrinsics::ID iid = result_oop->intrinsic_id(); 4.26 if (MethodHandles::is_signature_polymorphic(iid)) { 4.27 @@ -234,6 +244,7 @@ 4.28 } 4.29 4.30 // returns first instance method 4.31 +// Looks up method in classes, then looks up local default methods 4.32 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { 4.33 Method* result_oop = klass->uncached_lookup_method(name, signature); 4.34 result = methodHandle(THREAD, result_oop); 4.35 @@ -241,13 +252,38 @@ 4.36 klass = KlassHandle(THREAD, result->method_holder()->super()); 4.37 result = methodHandle(THREAD, klass->uncached_lookup_method(name, signature)); 4.38 } 4.39 + 4.40 + if (result.is_null()) { 4.41 + Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); 4.42 + if (default_methods != NULL) { 4.43 + result = methodHandle(InstanceKlass::find_method(default_methods, name, signature)); 4.44 + assert(result.is_null() || !result->is_static(), "static defaults not allowed"); 4.45 + } 4.46 + } 4.47 } 4.48 4.49 +int LinkResolver::vtable_index_of_interface_method(KlassHandle klass, 4.50 + methodHandle resolved_method, TRAPS) { 4.51 4.52 -int LinkResolver::vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { 4.53 - ResourceMark rm(THREAD); 4.54 - klassVtable *vt = InstanceKlass::cast(klass())->vtable(); 4.55 - return vt->index_of_miranda(name, signature); 4.56 + int vtable_index = Method::invalid_vtable_index; 4.57 + Symbol* name = resolved_method->name(); 4.58 + Symbol* signature = resolved_method->signature(); 4.59 + 4.60 + // First check in default method array 4.61 + if (!resolved_method->is_abstract() && 4.62 + (InstanceKlass::cast(klass())->default_methods() != NULL)) { 4.63 + int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), name, signature); 4.64 + if (index >= 0 ) { 4.65 + vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index); 4.66 + } 4.67 + } 4.68 + if (vtable_index == Method::invalid_vtable_index) { 4.69 + // get vtable_index for miranda methods 4.70 + ResourceMark rm(THREAD); 4.71 + klassVtable *vt = InstanceKlass::cast(klass())->vtable(); 4.72 + vtable_index = vt->index_of_miranda(name, signature); 4.73 + } 4.74 + return vtable_index; 4.75 } 4.76 4.77 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { 4.78 @@ -625,6 +661,12 @@ 4.79 resolved_method->method_holder()->internal_name() 4.80 ); 4.81 resolved_method->access_flags().print_on(tty); 4.82 + if (resolved_method->is_default_method()) { 4.83 + tty->print("default"); 4.84 + } 4.85 + if (resolved_method->is_overpass()) { 4.86 + tty->print("overpass"); 4.87 + } 4.88 tty->cr(); 4.89 } 4.90 } 4.91 @@ -853,6 +895,7 @@ 4.92 resolved_method->signature())); 4.93 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 4.94 } 4.95 + 4.96 if (TraceItables && Verbose) { 4.97 ResourceMark rm(THREAD); 4.98 tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 4.99 @@ -864,8 +907,7 @@ 4.100 resolved_method->method_holder()->internal_name() 4.101 ); 4.102 resolved_method->access_flags().print_on(tty); 4.103 - if (resolved_method->method_holder()->is_interface() && 4.104 - !resolved_method->is_abstract()) { 4.105 + if (resolved_method->is_default_method()) { 4.106 tty->print("default"); 4.107 } 4.108 if (resolved_method->is_overpass()) { 4.109 @@ -945,10 +987,12 @@ 4.110 sel_method->method_holder()->internal_name() 4.111 ); 4.112 sel_method->access_flags().print_on(tty); 4.113 - if (sel_method->method_holder()->is_interface() && 4.114 - !sel_method->is_abstract()) { 4.115 + if (sel_method->is_default_method()) { 4.116 tty->print("default"); 4.117 } 4.118 + if (sel_method->is_overpass()) { 4.119 + tty->print("overpass"); 4.120 + } 4.121 tty->cr(); 4.122 } 4.123 4.124 @@ -996,26 +1040,25 @@ 4.125 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 4.126 } 4.127 4.128 - if (PrintVtables && Verbose) { 4.129 - ResourceMark rm(THREAD); 4.130 - tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 4.131 - (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), 4.132 - (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), 4.133 - Method::name_and_sig_as_C_string(resolved_klass(), 4.134 - resolved_method->name(), 4.135 - resolved_method->signature()), 4.136 - resolved_method->method_holder()->internal_name() 4.137 - ); 4.138 - resolved_method->access_flags().print_on(tty); 4.139 - if (resolved_method->method_holder()->is_interface() && 4.140 - !resolved_method->is_abstract()) { 4.141 - tty->print("default"); 4.142 - } 4.143 - if (resolved_method->is_overpass()) { 4.144 - tty->print("overpass"); 4.145 - } 4.146 - tty->cr(); 4.147 - } 4.148 + if (PrintVtables && Verbose) { 4.149 + ResourceMark rm(THREAD); 4.150 + tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 4.151 + (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), 4.152 + (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), 4.153 + Method::name_and_sig_as_C_string(resolved_klass(), 4.154 + resolved_method->name(), 4.155 + resolved_method->signature()), 4.156 + resolved_method->method_holder()->internal_name() 4.157 + ); 4.158 + resolved_method->access_flags().print_on(tty); 4.159 + if (resolved_method->is_default_method()) { 4.160 + tty->print("default"); 4.161 + } 4.162 + if (resolved_method->is_overpass()) { 4.163 + tty->print("overpass"); 4.164 + } 4.165 + tty->cr(); 4.166 + } 4.167 } 4.168 4.169 // throws runtime exceptions 4.170 @@ -1045,10 +1088,8 @@ 4.171 4.172 // do lookup based on receiver klass using the vtable index 4.173 if (resolved_method->method_holder()->is_interface()) { // miranda method 4.174 - vtable_index = vtable_index_of_miranda_method(resolved_klass, 4.175 - resolved_method->name(), 4.176 - resolved_method->signature(), CHECK); 4.177 - 4.178 + vtable_index = vtable_index_of_interface_method(resolved_klass, 4.179 + resolved_method, CHECK); 4.180 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 4.181 4.182 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); 4.183 @@ -1104,11 +1145,10 @@ 4.184 vtable_index 4.185 ); 4.186 selected_method->access_flags().print_on(tty); 4.187 - if (selected_method->method_holder()->is_interface() && 4.188 - !selected_method->is_abstract()) { 4.189 + if (selected_method->is_default_method()) { 4.190 tty->print("default"); 4.191 } 4.192 - if (resolved_method->is_overpass()) { 4.193 + if (selected_method->is_overpass()) { 4.194 tty->print("overpass"); 4.195 } 4.196 tty->cr(); 4.197 @@ -1191,7 +1231,6 @@ 4.198 sel_method->name(), 4.199 sel_method->signature())); 4.200 } 4.201 - 4.202 // check if abstract 4.203 if (check_null_and_abstract && sel_method->is_abstract()) { 4.204 ResourceMark rm(THREAD); 4.205 @@ -1220,11 +1259,10 @@ 4.206 sel_method->method_holder()->internal_name() 4.207 ); 4.208 sel_method->access_flags().print_on(tty); 4.209 - if (sel_method->method_holder()->is_interface() && 4.210 - !sel_method->is_abstract()) { 4.211 + if (sel_method->is_default_method()) { 4.212 tty->print("default"); 4.213 } 4.214 - if (resolved_method->is_overpass()) { 4.215 + if (sel_method->is_overpass()) { 4.216 tty->print("overpass"); 4.217 } 4.218 tty->cr();
5.1 --- a/src/share/vm/interpreter/linkResolver.hpp Sun Oct 06 16:13:50 2013 +0200 5.2 +++ b/src/share/vm/interpreter/linkResolver.hpp Mon Oct 07 12:20:28 2013 -0400 5.3 @@ -130,8 +130,7 @@ 5.4 static void lookup_polymorphic_method (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, 5.5 KlassHandle current_klass, Handle *appendix_result_or_null, Handle *method_type_result, TRAPS); 5.6 5.7 - static int vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 5.8 - 5.9 + static int vtable_index_of_interface_method(KlassHandle klass, methodHandle resolved_method, TRAPS); 5.10 static void resolve_klass (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); 5.11 5.12 static void resolve_pool (KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS);
6.1 --- a/src/share/vm/memory/heapInspection.hpp Sun Oct 06 16:13:50 2013 +0200 6.2 +++ b/src/share/vm/memory/heapInspection.hpp Mon Oct 07 12:20:28 2013 -0400 6.3 @@ -73,6 +73,10 @@ 6.4 "Number of bytes used by the InstanceKlass::methods() array") \ 6.5 f(method_ordering_bytes, IK_method_ordering, \ 6.6 "Number of bytes used by the InstanceKlass::method_ordering() array") \ 6.7 + f(default_methods_array_bytes, IK_default_methods, \ 6.8 + "Number of bytes used by the InstanceKlass::default_methods() array") \ 6.9 + f(default_vtable_indices_bytes, IK_default_vtable_indices, \ 6.10 + "Number of bytes used by the InstanceKlass::default_vtable_indices() array") \ 6.11 f(local_interfaces_bytes, IK_local_interfaces, \ 6.12 "Number of bytes used by the InstanceKlass::local_interfaces() array") \ 6.13 f(transitive_interfaces_bytes, IK_transitive_interfaces, \
7.1 --- a/src/share/vm/oops/instanceKlass.cpp Sun Oct 06 16:13:50 2013 +0200 7.2 +++ b/src/share/vm/oops/instanceKlass.cpp Mon Oct 07 12:20:28 2013 -0400 7.3 @@ -238,6 +238,13 @@ 7.4 } 7.5 } 7.6 7.7 +// create a new array of vtable_indices for default methods 7.8 +Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) { 7.9 + Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL); 7.10 + assert(default_vtable_indices() == NULL, "only create once"); 7.11 + set_default_vtable_indices(vtable_indices); 7.12 + return vtable_indices; 7.13 +} 7.14 7.15 InstanceKlass::InstanceKlass(int vtable_len, 7.16 int itable_len, 7.17 @@ -263,6 +270,8 @@ 7.18 set_array_klasses(NULL); 7.19 set_methods(NULL); 7.20 set_method_ordering(NULL); 7.21 + set_default_methods(NULL); 7.22 + set_default_vtable_indices(NULL); 7.23 set_local_interfaces(NULL); 7.24 set_transitive_interfaces(NULL); 7.25 init_implementor(); 7.26 @@ -376,6 +385,21 @@ 7.27 } 7.28 set_method_ordering(NULL); 7.29 7.30 + // default methods can be empty 7.31 + if (default_methods() != NULL && 7.32 + default_methods() != Universe::the_empty_method_array()) { 7.33 + MetadataFactory::free_array<Method*>(loader_data, default_methods()); 7.34 + } 7.35 + // Do NOT deallocate the default methods, they are owned by superinterfaces. 7.36 + set_default_methods(NULL); 7.37 + 7.38 + // default methods vtable indices can be empty 7.39 + if (default_vtable_indices() != NULL) { 7.40 + MetadataFactory::free_array<int>(loader_data, default_vtable_indices()); 7.41 + } 7.42 + set_default_vtable_indices(NULL); 7.43 + 7.44 + 7.45 // This array is in Klass, but remove it with the InstanceKlass since 7.46 // this place would be the only caller and it can share memory with transitive 7.47 // interfaces. 7.48 @@ -1354,32 +1378,44 @@ 7.49 return -1; 7.50 } 7.51 7.52 +// find_method looks up the name/signature in the local methods array 7.53 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { 7.54 return InstanceKlass::find_method(methods(), name, signature); 7.55 } 7.56 7.57 +// find_method looks up the name/signature in the local methods array 7.58 Method* InstanceKlass::find_method( 7.59 Array<Method*>* methods, Symbol* name, Symbol* signature) { 7.60 + int hit = find_method_index(methods, name, signature); 7.61 + return hit >= 0 ? methods->at(hit): NULL; 7.62 +} 7.63 + 7.64 +// Used directly for default_methods to find the index into the 7.65 +// default_vtable_indices, and indirectly by find_method 7.66 +// find_method_index looks in the local methods array to return the index 7.67 +// of the matching name/signature 7.68 +int InstanceKlass::find_method_index( 7.69 + Array<Method*>* methods, Symbol* name, Symbol* signature) { 7.70 int hit = binary_search(methods, name); 7.71 if (hit != -1) { 7.72 Method* m = methods->at(hit); 7.73 // Do linear search to find matching signature. First, quick check 7.74 // for common case 7.75 - if (m->signature() == signature) return m; 7.76 + if (m->signature() == signature) return hit; 7.77 // search downwards through overloaded methods 7.78 int i; 7.79 for (i = hit - 1; i >= 0; --i) { 7.80 Method* m = methods->at(i); 7.81 assert(m->is_method(), "must be method"); 7.82 if (m->name() != name) break; 7.83 - if (m->signature() == signature) return m; 7.84 + if (m->signature() == signature) return i; 7.85 } 7.86 // search upwards 7.87 for (i = hit + 1; i < methods->length(); ++i) { 7.88 Method* m = methods->at(i); 7.89 assert(m->is_method(), "must be method"); 7.90 if (m->name() != name) break; 7.91 - if (m->signature() == signature) return m; 7.92 + if (m->signature() == signature) return i; 7.93 } 7.94 // not found 7.95 #ifdef ASSERT 7.96 @@ -1387,9 +1423,8 @@ 7.97 assert(index == -1, err_msg("binary search should have found entry %d", index)); 7.98 #endif 7.99 } 7.100 - return NULL; 7.101 + return -1; 7.102 } 7.103 - 7.104 int InstanceKlass::find_method_by_name(Symbol* name, int* end) { 7.105 return find_method_by_name(methods(), name, end); 7.106 } 7.107 @@ -1408,6 +1443,7 @@ 7.108 return -1; 7.109 } 7.110 7.111 +// lookup_method searches both the local methods array and all superclasses methods arrays 7.112 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const { 7.113 Klass* klass = const_cast<InstanceKlass*>(this); 7.114 while (klass != NULL) { 7.115 @@ -1418,6 +1454,21 @@ 7.116 return NULL; 7.117 } 7.118 7.119 +// lookup a method in the default methods list then in all transitive interfaces 7.120 +// Do NOT return private or static methods 7.121 +Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name, 7.122 + Symbol* signature) const { 7.123 + Method* m; 7.124 + if (default_methods() != NULL) { 7.125 + m = find_method(default_methods(), name, signature); 7.126 + } 7.127 + // Look up interfaces 7.128 + if (m == NULL) { 7.129 + m = lookup_method_in_all_interfaces(name, signature); 7.130 + } 7.131 + return m; 7.132 +} 7.133 + 7.134 // lookup a method in all the interfaces that this class implements 7.135 // Do NOT return private or static methods, new in JDK8 which are not externally visible 7.136 // They should only be found in the initial InterfaceMethodRef 7.137 @@ -2548,6 +2599,42 @@ 7.138 return m; 7.139 } 7.140 7.141 + 7.142 +#if INCLUDE_JVMTI 7.143 +// update default_methods for redefineclasses for methods that are 7.144 +// not yet in the vtable due to concurrent subclass define and superinterface 7.145 +// redefinition 7.146 +// Note: those in the vtable, should have been updated via adjust_method_entries 7.147 +void InstanceKlass::adjust_default_methods(Method** old_methods, Method** new_methods, 7.148 + int methods_length, bool* trace_name_printed) { 7.149 + // search the default_methods for uses of either obsolete or EMCP methods 7.150 + if (default_methods() != NULL) { 7.151 + for (int j = 0; j < methods_length; j++) { 7.152 + Method* old_method = old_methods[j]; 7.153 + Method* new_method = new_methods[j]; 7.154 + 7.155 + for (int index = 0; index < default_methods()->length(); index ++) { 7.156 + if (default_methods()->at(index) == old_method) { 7.157 + default_methods()->at_put(index, new_method); 7.158 + if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 7.159 + if (!(*trace_name_printed)) { 7.160 + // RC_TRACE_MESG macro has an embedded ResourceMark 7.161 + RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s", 7.162 + external_name(), 7.163 + old_method->method_holder()->external_name())); 7.164 + *trace_name_printed = true; 7.165 + } 7.166 + RC_TRACE(0x00100000, ("default method update: %s(%s) ", 7.167 + new_method->name()->as_C_string(), 7.168 + new_method->signature()->as_C_string())); 7.169 + } 7.170 + } 7.171 + } 7.172 + } 7.173 + } 7.174 +} 7.175 +#endif // INCLUDE_JVMTI 7.176 + 7.177 // On-stack replacement stuff 7.178 void InstanceKlass::add_osr_nmethod(nmethod* n) { 7.179 // only one compilation can be active 7.180 @@ -2742,11 +2829,21 @@ 7.181 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr(); 7.182 if (Verbose || WizardMode) { 7.183 Array<Method*>* method_array = methods(); 7.184 - for(int i = 0; i < method_array->length(); i++) { 7.185 + for (int i = 0; i < method_array->length(); i++) { 7.186 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); 7.187 } 7.188 } 7.189 - st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr(); 7.190 + st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr(); 7.191 + st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr(); 7.192 + if (Verbose && default_methods() != NULL) { 7.193 + Array<Method*>* method_array = default_methods(); 7.194 + for (int i = 0; i < method_array->length(); i++) { 7.195 + st->print("%d : ", i); method_array->at(i)->print_value(); st->cr(); 7.196 + } 7.197 + } 7.198 + if (default_vtable_indices() != NULL) { 7.199 + st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr(); 7.200 + } 7.201 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr(); 7.202 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr(); 7.203 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr(); 7.204 @@ -3099,6 +3196,19 @@ 7.205 } 7.206 } 7.207 7.208 + // Verify default methods 7.209 + if (default_methods() != NULL) { 7.210 + Array<Method*>* methods = this->default_methods(); 7.211 + for (int j = 0; j < methods->length(); j++) { 7.212 + guarantee(methods->at(j)->is_method(), "non-method in methods array"); 7.213 + } 7.214 + for (int j = 0; j < methods->length() - 1; j++) { 7.215 + Method* m1 = methods->at(j); 7.216 + Method* m2 = methods->at(j + 1); 7.217 + guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly"); 7.218 + } 7.219 + } 7.220 + 7.221 // Verify JNI static field identifiers 7.222 if (jni_ids() != NULL) { 7.223 jni_ids()->verify(this);
8.1 --- a/src/share/vm/oops/instanceKlass.hpp Sun Oct 06 16:13:50 2013 +0200 8.2 +++ b/src/share/vm/oops/instanceKlass.hpp Mon Oct 07 12:20:28 2013 -0400 8.3 @@ -269,12 +269,18 @@ 8.4 8.5 // Method array. 8.6 Array<Method*>* _methods; 8.7 + // Default Method Array, concrete methods inherited from interfaces 8.8 + Array<Method*>* _default_methods; 8.9 // Interface (Klass*s) this class declares locally to implement. 8.10 Array<Klass*>* _local_interfaces; 8.11 // Interface (Klass*s) this class implements transitively. 8.12 Array<Klass*>* _transitive_interfaces; 8.13 // Int array containing the original order of method in the class file (for JVMTI). 8.14 Array<int>* _method_ordering; 8.15 + // Int array containing the vtable_indices for default_methods 8.16 + // offset matches _default_methods offset 8.17 + Array<int>* _default_vtable_indices; 8.18 + 8.19 // Instance and static variable information, starts with 6-tuples of shorts 8.20 // [access, name index, sig index, initval index, low_offset, high_offset] 8.21 // for all fields, followed by the generic signature data at the end of 8.22 @@ -356,6 +362,15 @@ 8.23 void set_method_ordering(Array<int>* m) { _method_ordering = m; } 8.24 void copy_method_ordering(intArray* m, TRAPS); 8.25 8.26 + // default_methods 8.27 + Array<Method*>* default_methods() const { return _default_methods; } 8.28 + void set_default_methods(Array<Method*>* a) { _default_methods = a; } 8.29 + 8.30 + // default method vtable_indices 8.31 + Array<int>* default_vtable_indices() const { return _default_vtable_indices; } 8.32 + void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; } 8.33 + Array<int>* create_new_default_vtable_indices(int len, TRAPS); 8.34 + 8.35 // interfaces 8.36 Array<Klass*>* local_interfaces() const { return _local_interfaces; } 8.37 void set_local_interfaces(Array<Klass*>* a) { 8.38 @@ -501,12 +516,18 @@ 8.39 Method* find_method(Symbol* name, Symbol* signature) const; 8.40 static Method* find_method(Array<Method*>* methods, Symbol* name, Symbol* signature); 8.41 8.42 + // find a local method index in default_methods (returns -1 if not found) 8.43 + static int find_method_index(Array<Method*>* methods, Symbol* name, Symbol* signature); 8.44 + 8.45 // lookup operation (returns NULL if not found) 8.46 Method* uncached_lookup_method(Symbol* name, Symbol* signature) const; 8.47 8.48 // lookup a method in all the interfaces that this class implements 8.49 // (returns NULL if not found) 8.50 Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature) const; 8.51 + // lookup a method in local defaults then in all interfaces 8.52 + // (returns NULL if not found) 8.53 + Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const; 8.54 8.55 // Find method indices by name. If a method with the specified name is 8.56 // found the index to the first method is returned, and 'end' is filled in 8.57 @@ -910,6 +931,11 @@ 8.58 klassItable* itable() const; // return new klassItable wrapper 8.59 Method* method_at_itable(Klass* holder, int index, TRAPS); 8.60 8.61 +#if INCLUDE_JVMTI 8.62 + void adjust_default_methods(Method** old_methods, Method** new_methods, 8.63 + int methods_length, bool* trace_name_printed); 8.64 +#endif // INCLUDE_JVMTI 8.65 + 8.66 // Garbage collection 8.67 void oop_follow_contents(oop obj); 8.68 int oop_adjust_pointers(oop obj);
9.1 --- a/src/share/vm/oops/klassVtable.cpp Sun Oct 06 16:13:50 2013 +0200 9.2 +++ b/src/share/vm/oops/klassVtable.cpp Mon Oct 07 12:20:28 2013 -0400 9.3 @@ -83,7 +83,7 @@ 9.4 9.5 GrowableArray<Method*> new_mirandas(20); 9.6 // compute the number of mirandas methods that must be added to the end 9.7 - get_mirandas(&new_mirandas, all_mirandas, super, methods, local_interfaces); 9.8 + get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); 9.9 *num_new_mirandas = new_mirandas.length(); 9.10 9.11 vtable_length += *num_new_mirandas * vtableEntry::size(); 9.12 @@ -186,7 +186,7 @@ 9.13 assert(methods->at(i)->is_method(), "must be a Method*"); 9.14 methodHandle mh(THREAD, methods->at(i)); 9.15 9.16 - bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK); 9.17 + bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK); 9.18 9.19 if (needs_new_entry) { 9.20 put_method_at(mh(), initialized); 9.21 @@ -195,7 +195,35 @@ 9.22 } 9.23 } 9.24 9.25 - // add miranda methods to end of vtable. 9.26 + // update vtable with default_methods 9.27 + Array<Method*>* default_methods = ik()->default_methods(); 9.28 + if (default_methods != NULL) { 9.29 + len = default_methods->length(); 9.30 + if (len > 0) { 9.31 + Array<int>* def_vtable_indices = NULL; 9.32 + if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) { 9.33 + def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK); 9.34 + } else { 9.35 + assert(def_vtable_indices->length() == len, "reinit vtable len?"); 9.36 + } 9.37 + for (int i = 0; i < len; i++) { 9.38 + HandleMark hm(THREAD); 9.39 + assert(default_methods->at(i)->is_method(), "must be a Method*"); 9.40 + methodHandle mh(THREAD, default_methods->at(i)); 9.41 + 9.42 + bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK); 9.43 + 9.44 + // needs new entry 9.45 + if (needs_new_entry) { 9.46 + put_method_at(mh(), initialized); 9.47 + def_vtable_indices->at_put(i, initialized); //set vtable index 9.48 + initialized++; 9.49 + } 9.50 + } 9.51 + } 9.52 + } 9.53 + 9.54 + // add miranda methods; it will also return the updated initialized 9.55 initialized = fill_in_mirandas(initialized); 9.56 9.57 // In class hierarchies where the accessibility is not increasing (i.e., going from private -> 9.58 @@ -230,14 +258,19 @@ 9.59 #ifndef PRODUCT 9.60 if (PrintVtables && Verbose) { 9.61 ResourceMark rm(THREAD); 9.62 + char* sig = target_method()->name_and_sig_as_C_string(); 9.63 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ", 9.64 supersuperklass->internal_name(), 9.65 - _klass->internal_name(), (target_method() != NULL) ? 9.66 - target_method()->name()->as_C_string() : "<NULL>", vtable_index); 9.67 + _klass->internal_name(), sig, vtable_index); 9.68 super_method->access_flags().print_on(tty); 9.69 + if (super_method->is_default_method()) { 9.70 + tty->print("default"); 9.71 + } 9.72 tty->print("overriders flags: "); 9.73 target_method->access_flags().print_on(tty); 9.74 - tty->cr(); 9.75 + if (target_method->is_default_method()) { 9.76 + tty->print("default"); 9.77 + } 9.78 } 9.79 #endif /*PRODUCT*/ 9.80 break; // return found superk 9.81 @@ -258,16 +291,31 @@ 9.82 // OR return true if a new vtable entry is required. 9.83 // Only called for InstanceKlass's, i.e. not for arrays 9.84 // If that changed, could not use _klass as handle for klass 9.85 -bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, 9.86 - bool checkconstraints, TRAPS) { 9.87 +bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, 9.88 + int super_vtable_len, int default_index, 9.89 + bool checkconstraints, TRAPS) { 9.90 ResourceMark rm; 9.91 bool allocate_new = true; 9.92 assert(klass->oop_is_instance(), "must be InstanceKlass"); 9.93 - assert(klass == target_method()->method_holder(), "caller resp."); 9.94 9.95 - // Initialize the method's vtable index to "nonvirtual". 9.96 - // If we allocate a vtable entry, we will update it to a non-negative number. 9.97 - target_method()->set_vtable_index(Method::nonvirtual_vtable_index); 9.98 + Array<int>* def_vtable_indices = NULL; 9.99 + bool is_default = false; 9.100 + // default methods are concrete methods in superinterfaces which are added to the vtable 9.101 + // with their real method_holder 9.102 + // Since vtable and itable indices share the same storage, don't touch 9.103 + // the default method's real vtable/itable index 9.104 + // default_vtable_indices stores the vtable value relative to this inheritor 9.105 + if (default_index >= 0 ) { 9.106 + is_default = true; 9.107 + def_vtable_indices = klass->default_vtable_indices(); 9.108 + assert(def_vtable_indices != NULL, "def vtable alloc?"); 9.109 + assert(default_index <= def_vtable_indices->length(), "def vtable len?"); 9.110 + } else { 9.111 + assert(klass == target_method()->method_holder(), "caller resp."); 9.112 + // Initialize the method's vtable index to "nonvirtual". 9.113 + // If we allocate a vtable entry, we will update it to a non-negative number. 9.114 + target_method()->set_vtable_index(Method::nonvirtual_vtable_index); 9.115 + } 9.116 9.117 // Static and <init> methods are never in 9.118 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { 9.119 @@ -284,6 +332,8 @@ 9.120 // An interface never allocates new vtable slots, only inherits old ones. 9.121 // This method will either be assigned its own itable index later, 9.122 // or be assigned an inherited vtable index in the loop below. 9.123 + // default methods store their vtable indices in the inheritors default_vtable_indices 9.124 + assert (default_index == -1, "interfaces don't store resolved default methods"); 9.125 target_method()->set_vtable_index(Method::pending_itable_index); 9.126 } 9.127 9.128 @@ -307,8 +357,15 @@ 9.129 9.130 Symbol* name = target_method()->name(); 9.131 Symbol* signature = target_method()->signature(); 9.132 - Handle target_loader(THREAD, _klass()->class_loader()); 9.133 - Symbol* target_classname = _klass->name(); 9.134 + 9.135 + KlassHandle target_klass(THREAD, target_method()->method_holder()); 9.136 + if (target_klass == NULL) { 9.137 + target_klass = _klass; 9.138 + } 9.139 + 9.140 + Handle target_loader(THREAD, target_klass->class_loader()); 9.141 + 9.142 + Symbol* target_classname = target_klass->name(); 9.143 for(int i = 0; i < super_vtable_len; i++) { 9.144 Method* super_method = method_at(i); 9.145 // Check if method name matches 9.146 @@ -317,10 +374,14 @@ 9.147 // get super_klass for method_holder for the found method 9.148 InstanceKlass* super_klass = super_method->method_holder(); 9.149 9.150 - if ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) || 9.151 - ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) 9.152 - && ((super_klass = find_transitive_override(super_klass, target_method, i, target_loader, 9.153 - target_classname, THREAD)) != (InstanceKlass*)NULL))) { 9.154 + if (is_default 9.155 + || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) 9.156 + || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) 9.157 + && ((super_klass = find_transitive_override(super_klass, 9.158 + target_method, i, target_loader, 9.159 + target_classname, THREAD)) 9.160 + != (InstanceKlass*)NULL)))) 9.161 + { 9.162 // overriding, so no new entry 9.163 allocate_new = false; 9.164 9.165 @@ -347,7 +408,7 @@ 9.166 "%s used in the signature"; 9.167 char* sig = target_method()->name_and_sig_as_C_string(); 9.168 const char* loader1 = SystemDictionary::loader_name(target_loader()); 9.169 - char* current = _klass->name()->as_C_string(); 9.170 + char* current = target_klass->name()->as_C_string(); 9.171 const char* loader2 = SystemDictionary::loader_name(super_loader()); 9.172 char* failed_type_name = failed_type_symbol->as_C_string(); 9.173 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + 9.174 @@ -360,16 +421,39 @@ 9.175 } 9.176 } 9.177 9.178 - put_method_at(target_method(), i); 9.179 - target_method()->set_vtable_index(i); 9.180 + put_method_at(target_method(), i); 9.181 + if (!is_default) { 9.182 + target_method()->set_vtable_index(i); 9.183 + } else { 9.184 + if (def_vtable_indices != NULL) { 9.185 + def_vtable_indices->at_put(default_index, i); 9.186 + } 9.187 + assert(super_method->is_default_method() || super_method->is_overpass() 9.188 + || super_method->is_abstract(), "default override error"); 9.189 + } 9.190 + 9.191 + 9.192 #ifndef PRODUCT 9.193 if (PrintVtables && Verbose) { 9.194 + ResourceMark rm(THREAD); 9.195 + char* sig = target_method()->name_and_sig_as_C_string(); 9.196 tty->print("overriding with %s::%s index %d, original flags: ", 9.197 - _klass->internal_name(), (target_method() != NULL) ? 9.198 - target_method()->name()->as_C_string() : "<NULL>", i); 9.199 + target_klass->internal_name(), sig, i); 9.200 super_method->access_flags().print_on(tty); 9.201 + if (super_method->is_default_method()) { 9.202 + tty->print("default"); 9.203 + } 9.204 + if (super_method->is_overpass()) { 9.205 + tty->print("overpass"); 9.206 + } 9.207 tty->print("overriders flags: "); 9.208 target_method->access_flags().print_on(tty); 9.209 + if (target_method->is_default_method()) { 9.210 + tty->print("default"); 9.211 + } 9.212 + if (target_method->is_overpass()) { 9.213 + tty->print("overpass"); 9.214 + } 9.215 tty->cr(); 9.216 } 9.217 #endif /*PRODUCT*/ 9.218 @@ -378,12 +462,25 @@ 9.219 // but not override another. Once we override one, not need new 9.220 #ifndef PRODUCT 9.221 if (PrintVtables && Verbose) { 9.222 + ResourceMark rm(THREAD); 9.223 + char* sig = target_method()->name_and_sig_as_C_string(); 9.224 tty->print("NOT overriding with %s::%s index %d, original flags: ", 9.225 - _klass->internal_name(), (target_method() != NULL) ? 9.226 - target_method()->name()->as_C_string() : "<NULL>", i); 9.227 + target_klass->internal_name(), sig,i); 9.228 super_method->access_flags().print_on(tty); 9.229 + if (super_method->is_default_method()) { 9.230 + tty->print("default"); 9.231 + } 9.232 + if (super_method->is_overpass()) { 9.233 + tty->print("overpass"); 9.234 + } 9.235 tty->print("overriders flags: "); 9.236 target_method->access_flags().print_on(tty); 9.237 + if (target_method->is_default_method()) { 9.238 + tty->print("default"); 9.239 + } 9.240 + if (target_method->is_overpass()) { 9.241 + tty->print("overpass"); 9.242 + } 9.243 tty->cr(); 9.244 } 9.245 #endif /*PRODUCT*/ 9.246 @@ -438,6 +535,14 @@ 9.247 return false; 9.248 } 9.249 9.250 + // Concrete interface methods do not need new entries, they override 9.251 + // abstract method entries using default inheritance rules 9.252 + if (target_method()->method_holder() != NULL && 9.253 + target_method()->method_holder()->is_interface() && 9.254 + !target_method()->is_abstract() ) { 9.255 + return false; 9.256 + } 9.257 + 9.258 // we need a new entry if there is no superclass 9.259 if (super == NULL) { 9.260 return true; 9.261 @@ -446,7 +551,7 @@ 9.262 // private methods in classes always have a new entry in the vtable 9.263 // specification interpretation since classic has 9.264 // private methods not overriding 9.265 - // JDK8 adds private methods in interfaces which require invokespecial 9.266 + // JDK8 adds private methods in interfaces which require invokespecial 9.267 if (target_method()->is_private()) { 9.268 return true; 9.269 } 9.270 @@ -526,35 +631,40 @@ 9.271 if (mhk->is_interface()) { 9.272 assert(m->is_public(), "should be public"); 9.273 assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); 9.274 - assert(is_miranda(m, ik()->methods(), ik()->super()), "should be a miranda_method"); 9.275 + assert(is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super()), "should be a miranda_method"); 9.276 return true; 9.277 } 9.278 return false; 9.279 } 9.280 9.281 -// check if a method is a miranda method, given a class's methods table and its super 9.282 -// "miranda" means not static, not defined by this class, and not defined 9.283 -// in super unless it is private and therefore inaccessible to this class. 9.284 +// check if a method is a miranda method, given a class's methods table, 9.285 +// its default_method table and its super 9.286 +// "miranda" means not static, not defined by this class. 9.287 +// private methods in interfaces do not belong in the miranda list. 9.288 // the caller must make sure that the method belongs to an interface implemented by the class 9.289 // Miranda methods only include public interface instance methods 9.290 -// Not private methods, not static methods, not default = concrete abstract 9.291 -bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, Klass* super) { 9.292 - if (m->is_static()) { 9.293 +// Not private methods, not static methods, not default == concrete abstract 9.294 +bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, 9.295 + Array<Method*>* default_methods, Klass* super) { 9.296 + if (m->is_static() || m->is_private()) { 9.297 return false; 9.298 } 9.299 Symbol* name = m->name(); 9.300 Symbol* signature = m->signature(); 9.301 if (InstanceKlass::find_method(class_methods, name, signature) == NULL) { 9.302 // did not find it in the method table of the current class 9.303 - if (super == NULL) { 9.304 - // super doesn't exist 9.305 - return true; 9.306 - } 9.307 + if ((default_methods == NULL) || 9.308 + InstanceKlass::find_method(default_methods, name, signature) == NULL) { 9.309 + if (super == NULL) { 9.310 + // super doesn't exist 9.311 + return true; 9.312 + } 9.313 9.314 - Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); 9.315 - if (mo == NULL || mo->access_flags().is_private() ) { 9.316 - // super class hierarchy does not implement it or protection is different 9.317 - return true; 9.318 + Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); 9.319 + if (mo == NULL || mo->access_flags().is_private() ) { 9.320 + // super class hierarchy does not implement it or protection is different 9.321 + return true; 9.322 + } 9.323 } 9.324 } 9.325 9.326 @@ -562,7 +672,7 @@ 9.327 } 9.328 9.329 // Scans current_interface_methods for miranda methods that do not 9.330 -// already appear in new_mirandas and are also not defined-and-non-private 9.331 +// already appear in new_mirandas, or default methods, and are also not defined-and-non-private 9.332 // in super (superclass). These mirandas are added to all_mirandas if it is 9.333 // not null; in addition, those that are not duplicates of miranda methods 9.334 // inherited by super from its interfaces are added to new_mirandas. 9.335 @@ -572,7 +682,8 @@ 9.336 void klassVtable::add_new_mirandas_to_lists( 9.337 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas, 9.338 Array<Method*>* current_interface_methods, Array<Method*>* class_methods, 9.339 - Klass* super) { 9.340 + Array<Method*>* default_methods, Klass* super) { 9.341 + 9.342 // iterate thru the current interface's method to see if it a miranda 9.343 int num_methods = current_interface_methods->length(); 9.344 for (int i = 0; i < num_methods; i++) { 9.345 @@ -590,7 +701,7 @@ 9.346 } 9.347 9.348 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable 9.349 - if (is_miranda(im, class_methods, super)) { // is it a miranda at all? 9.350 + if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all? 9.351 InstanceKlass *sk = InstanceKlass::cast(super); 9.352 // check if it is a duplicate of a super's miranda 9.353 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) { 9.354 @@ -607,6 +718,7 @@ 9.355 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas, 9.356 GrowableArray<Method*>* all_mirandas, 9.357 Klass* super, Array<Method*>* class_methods, 9.358 + Array<Method*>* default_methods, 9.359 Array<Klass*>* local_interfaces) { 9.360 assert((new_mirandas->length() == 0) , "current mirandas must be 0"); 9.361 9.362 @@ -615,14 +727,16 @@ 9.363 for (int i = 0; i < num_local_ifs; i++) { 9.364 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i)); 9.365 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 9.366 - ik->methods(), class_methods, super); 9.367 + ik->methods(), class_methods, 9.368 + default_methods, super); 9.369 // iterate thru each local's super interfaces 9.370 Array<Klass*>* super_ifs = ik->transitive_interfaces(); 9.371 int num_super_ifs = super_ifs->length(); 9.372 for (int j = 0; j < num_super_ifs; j++) { 9.373 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j)); 9.374 add_new_mirandas_to_lists(new_mirandas, all_mirandas, 9.375 - sik->methods(), class_methods, super); 9.376 + sik->methods(), class_methods, 9.377 + default_methods, super); 9.378 } 9.379 } 9.380 } 9.381 @@ -633,8 +747,22 @@ 9.382 int klassVtable::fill_in_mirandas(int initialized) { 9.383 GrowableArray<Method*> mirandas(20); 9.384 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), 9.385 - ik()->local_interfaces()); 9.386 + ik()->default_methods(), ik()->local_interfaces()); 9.387 for (int i = 0; i < mirandas.length(); i++) { 9.388 + if (PrintVtables && Verbose) { 9.389 + Method* meth = mirandas.at(i); 9.390 + ResourceMark rm(Thread::current()); 9.391 + if (meth != NULL) { 9.392 + char* sig = meth->name_and_sig_as_C_string(); 9.393 + tty->print("fill in mirandas with %s index %d, flags: ", 9.394 + sig, initialized); 9.395 + meth->access_flags().print_on(tty); 9.396 + if (meth->is_default_method()) { 9.397 + tty->print("default"); 9.398 + } 9.399 + tty->cr(); 9.400 + } 9.401 + } 9.402 put_method_at(mirandas.at(i), initialized); 9.403 ++initialized; 9.404 } 9.405 @@ -648,6 +776,26 @@ 9.406 } 9.407 9.408 #if INCLUDE_JVMTI 9.409 +bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) { 9.410 + // If old_method is default, find this vtable index in default_vtable_indices 9.411 + // and replace that method in the _default_methods list 9.412 + bool updated = false; 9.413 + 9.414 + Array<Method*>* default_methods = ik()->default_methods(); 9.415 + if (default_methods != NULL) { 9.416 + int len = default_methods->length(); 9.417 + for (int idx = 0; idx < len; idx++) { 9.418 + if (vtable_index == ik()->default_vtable_indices()->at(idx)) { 9.419 + if (default_methods->at(idx) == old_method) { 9.420 + default_methods->at_put(idx, new_method); 9.421 + updated = true; 9.422 + } 9.423 + break; 9.424 + } 9.425 + } 9.426 + } 9.427 + return updated; 9.428 +} 9.429 void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods, 9.430 int methods_length, bool * trace_name_printed) { 9.431 // search the vtable for uses of either obsolete or EMCP methods 9.432 @@ -663,18 +811,26 @@ 9.433 for (int index = 0; index < length(); index++) { 9.434 if (unchecked_method_at(index) == old_method) { 9.435 put_method_at(new_method, index); 9.436 + // For default methods, need to update the _default_methods array 9.437 + // which can only have one method entry for a given signature 9.438 + bool updated_default = false; 9.439 + if (old_method->is_default_method()) { 9.440 + updated_default = adjust_default_method(index, old_method, new_method); 9.441 + } 9.442 9.443 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { 9.444 if (!(*trace_name_printed)) { 9.445 // RC_TRACE_MESG macro has an embedded ResourceMark 9.446 - RC_TRACE_MESG(("adjust: name=%s", 9.447 + RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s", 9.448 + klass()->external_name(), 9.449 old_method->method_holder()->external_name())); 9.450 *trace_name_printed = true; 9.451 } 9.452 // RC_TRACE macro has an embedded ResourceMark 9.453 - RC_TRACE(0x00100000, ("vtable method update: %s(%s)", 9.454 + RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s", 9.455 new_method->name()->as_C_string(), 9.456 - new_method->signature()->as_C_string())); 9.457 + new_method->signature()->as_C_string(), 9.458 + updated_default ? "true" : "false")); 9.459 } 9.460 // cannot 'break' here; see for-loop comment above. 9.461 } 9.462 @@ -701,6 +857,12 @@ 9.463 if (m != NULL) { 9.464 tty->print(" (%5d) ", i); 9.465 m->access_flags().print_on(tty); 9.466 + if (m->is_default_method()) { 9.467 + tty->print("default"); 9.468 + } 9.469 + if (m->is_overpass()) { 9.470 + tty->print("overpass"); 9.471 + } 9.472 tty->print(" -- "); 9.473 m->print_name(tty); 9.474 tty->cr(); 9.475 @@ -757,9 +919,9 @@ 9.476 // Initialization 9.477 void klassItable::initialize_itable(bool checkconstraints, TRAPS) { 9.478 if (_klass->is_interface()) { 9.479 - // This needs to go after vtable indexes are assigned but 9.480 - // before implementors need to know the number of itable indexes. 9.481 - assign_itable_indexes_for_interface(_klass()); 9.482 + // This needs to go after vtable indices are assigned but 9.483 + // before implementors need to know the number of itable indices. 9.484 + assign_itable_indices_for_interface(_klass()); 9.485 } 9.486 9.487 // Cannot be setup doing bootstrapping, interfaces don't have 9.488 @@ -803,7 +965,7 @@ 9.489 return true; 9.490 } 9.491 9.492 -int klassItable::assign_itable_indexes_for_interface(Klass* klass) { 9.493 +int klassItable::assign_itable_indices_for_interface(Klass* klass) { 9.494 // an interface does not have an itable, but its methods need to be numbered 9.495 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count, 9.496 klass->name()->as_C_string()); 9.497 @@ -846,7 +1008,7 @@ 9.498 } 9.499 nof_methods -= 1; 9.500 } 9.501 - // no methods have itable indexes 9.502 + // no methods have itable indices 9.503 return 0; 9.504 } 9.505 9.506 @@ -907,6 +1069,21 @@ 9.507 int ime_num = m->itable_index(); 9.508 assert(ime_num < ime_count, "oob"); 9.509 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); 9.510 + if (TraceItables && Verbose) { 9.511 + ResourceMark rm(THREAD); 9.512 + if (target() != NULL) { 9.513 + char* sig = target()->name_and_sig_as_C_string(); 9.514 + tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ", 9.515 + interf_h()->internal_name(), ime_num, sig, 9.516 + target()->method_holder()->internal_name()); 9.517 + tty->print("target_method flags: "); 9.518 + target()->access_flags().print_on(tty); 9.519 + if (target()->is_default_method()) { 9.520 + tty->print("default"); 9.521 + } 9.522 + tty->cr(); 9.523 + } 9.524 + } 9.525 } 9.526 } 9.527 } 9.528 @@ -980,6 +1157,9 @@ 9.529 if (m != NULL) { 9.530 tty->print(" (%5d) ", i); 9.531 m->access_flags().print_on(tty); 9.532 + if (m->is_default_method()) { 9.533 + tty->print("default"); 9.534 + } 9.535 tty->print(" -- "); 9.536 m->print_name(tty); 9.537 tty->cr(); 9.538 @@ -1116,7 +1296,7 @@ 9.539 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); 9.540 9.541 if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) 9.542 - return NULL; // help caller defend against bad indexes 9.543 + return NULL; // help caller defend against bad indices 9.544 9.545 int index = itable_index; 9.546 Method* m = methods->at(index);
10.1 --- a/src/share/vm/oops/klassVtable.hpp Sun Oct 06 16:13:50 2013 +0200 10.2 +++ b/src/share/vm/oops/klassVtable.hpp Mon Oct 07 12:20:28 2013 -0400 10.3 @@ -97,6 +97,7 @@ 10.4 // trace_name_printed is set to true if the current call has 10.5 // printed the klass name so that other routines in the adjust_* 10.6 // group don't print the klass name. 10.7 + bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method); 10.8 void adjust_method_entries(Method** old_methods, Method** new_methods, 10.9 int methods_length, bool * trace_name_printed); 10.10 bool check_no_old_or_obsolete_entries(); 10.11 @@ -118,24 +119,28 @@ 10.12 void put_method_at(Method* m, int index); 10.13 static bool needs_new_vtable_entry(methodHandle m, Klass* super, Handle classloader, Symbol* classname, AccessFlags access_flags, TRAPS); 10.14 10.15 - bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, bool checkconstraints, TRAPS); 10.16 + bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS); 10.17 InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index, 10.18 Handle target_loader, Symbol* target_classname, Thread* THREAD); 10.19 10.20 // support for miranda methods 10.21 bool is_miranda_entry_at(int i); 10.22 int fill_in_mirandas(int initialized); 10.23 - static bool is_miranda(Method* m, Array<Method*>* class_methods, Klass* super); 10.24 + static bool is_miranda(Method* m, Array<Method*>* class_methods, 10.25 + Array<Method*>* default_methods, Klass* super); 10.26 static void add_new_mirandas_to_lists( 10.27 GrowableArray<Method*>* new_mirandas, 10.28 GrowableArray<Method*>* all_mirandas, 10.29 - Array<Method*>* current_interface_methods, Array<Method*>* class_methods, 10.30 + Array<Method*>* current_interface_methods, 10.31 + Array<Method*>* class_methods, 10.32 + Array<Method*>* default_methods, 10.33 Klass* super); 10.34 static void get_mirandas( 10.35 GrowableArray<Method*>* new_mirandas, 10.36 GrowableArray<Method*>* all_mirandas, Klass* super, 10.37 - Array<Method*>* class_methods, Array<Klass*>* local_interfaces); 10.38 - 10.39 + Array<Method*>* class_methods, 10.40 + Array<Method*>* default_methods, 10.41 + Array<Klass*>* local_interfaces); 10.42 void verify_against(outputStream* st, klassVtable* vt, int index); 10.43 inline InstanceKlass* ik() const; 10.44 }; 10.45 @@ -290,7 +295,7 @@ 10.46 #endif // INCLUDE_JVMTI 10.47 10.48 // Setup of itable 10.49 - static int assign_itable_indexes_for_interface(Klass* klass); 10.50 + static int assign_itable_indices_for_interface(Klass* klass); 10.51 static int method_count_for_interface(Klass* klass); 10.52 static int compute_itable_size(Array<Klass*>* transitive_interfaces); 10.53 static void setup_itable_offset_table(instanceKlassHandle klass);
11.1 --- a/src/share/vm/oops/method.cpp Sun Oct 06 16:13:50 2013 +0200 11.2 +++ b/src/share/vm/oops/method.cpp Mon Oct 07 12:20:28 2013 -0400 11.3 @@ -511,9 +511,9 @@ 11.4 11.5 bool Method::is_final_method(AccessFlags class_access_flags) const { 11.6 // or "does_not_require_vtable_entry" 11.7 - // overpass can occur, is not final (reuses vtable entry) 11.8 + // default method or overpass can occur, is not final (reuses vtable entry) 11.9 // private methods get vtable entries for backward class compatibility. 11.10 - if (is_overpass()) return false; 11.11 + if (is_overpass() || is_default_method()) return false; 11.12 return is_final() || class_access_flags.is_final(); 11.13 } 11.14 11.15 @@ -521,11 +521,24 @@ 11.16 return is_final_method(method_holder()->access_flags()); 11.17 } 11.18 11.19 +bool Method::is_default_method() const { 11.20 + if (method_holder() != NULL && 11.21 + method_holder()->is_interface() && 11.22 + !is_abstract()) { 11.23 + return true; 11.24 + } else { 11.25 + return false; 11.26 + } 11.27 +} 11.28 + 11.29 bool Method::can_be_statically_bound(AccessFlags class_access_flags) const { 11.30 if (is_final_method(class_access_flags)) return true; 11.31 #ifdef ASSERT 11.32 + ResourceMark rm; 11.33 bool is_nonv = (vtable_index() == nonvirtual_vtable_index); 11.34 - if (class_access_flags.is_interface()) assert(is_nonv == is_static(), err_msg("is_nonv=%s", is_nonv)); 11.35 + if (class_access_flags.is_interface()) { 11.36 + assert(is_nonv == is_static(), err_msg("is_nonv=%s", name_and_sig_as_C_string())); 11.37 + } 11.38 #endif 11.39 assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question"); 11.40 return vtable_index() == nonvirtual_vtable_index; 11.41 @@ -1371,7 +1384,8 @@ 11.42 } 11.43 11.44 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 11.45 -void Method::sort_methods(Array<Method*>* methods, bool idempotent) { 11.46 +// default_methods also uses this without the ordering for fast find_method 11.47 +void Method::sort_methods(Array<Method*>* methods, bool idempotent, bool set_idnums) { 11.48 int length = methods->length(); 11.49 if (length > 1) { 11.50 { 11.51 @@ -1379,14 +1393,15 @@ 11.52 QuickSort::sort<Method*>(methods->data(), length, method_comparator, idempotent); 11.53 } 11.54 // Reset method ordering 11.55 - for (int i = 0; i < length; i++) { 11.56 - Method* m = methods->at(i); 11.57 - m->set_method_idnum(i); 11.58 + if (set_idnums) { 11.59 + for (int i = 0; i < length; i++) { 11.60 + Method* m = methods->at(i); 11.61 + m->set_method_idnum(i); 11.62 + } 11.63 } 11.64 } 11.65 } 11.66 11.67 - 11.68 //----------------------------------------------------------------------------------- 11.69 // Non-product code unless JVM/TI needs it 11.70
12.1 --- a/src/share/vm/oops/method.hpp Sun Oct 06 16:13:50 2013 +0200 12.2 +++ b/src/share/vm/oops/method.hpp Mon Oct 07 12:20:28 2013 -0400 12.3 @@ -567,6 +567,7 @@ 12.4 // checks method and its method holder 12.5 bool is_final_method() const; 12.6 bool is_final_method(AccessFlags class_access_flags) const; 12.7 + bool is_default_method() const; 12.8 12.9 // true if method needs no dynamic dispatch (final and/or no vtable entry) 12.10 bool can_be_statically_bound() const; 12.11 @@ -846,7 +847,7 @@ 12.12 #endif 12.13 12.14 // Helper routine used for method sorting 12.15 - static void sort_methods(Array<Method*>* methods, bool idempotent = false); 12.16 + static void sort_methods(Array<Method*>* methods, bool idempotent = false, bool set_idnums = true); 12.17 12.18 // Deallocation function for redefine classes or if an error occurs 12.19 void deallocate_contents(ClassLoaderData* loader_data);
13.1 --- a/src/share/vm/prims/jni.cpp Sun Oct 06 16:13:50 2013 +0200 13.2 +++ b/src/share/vm/prims/jni.cpp Mon Oct 07 12:20:28 2013 -0400 13.3 @@ -1591,10 +1591,8 @@ 13.4 } 13.5 } else { 13.6 m = klass->lookup_method(name, signature); 13.7 - // Look up interfaces 13.8 - if (m == NULL && klass->oop_is_instance()) { 13.9 - m = InstanceKlass::cast(klass())->lookup_method_in_all_interfaces(name, 13.10 - signature); 13.11 + if (m == NULL && klass->oop_is_instance()) { 13.12 + m = InstanceKlass::cast(klass())->lookup_method_in_ordered_interfaces(name, signature); 13.13 } 13.14 } 13.15 if (m == NULL || (m->is_static() != is_static)) {
14.1 --- a/src/share/vm/prims/jvmtiRedefineClasses.cpp Sun Oct 06 16:13:50 2013 +0200 14.2 +++ b/src/share/vm/prims/jvmtiRedefineClasses.cpp Mon Oct 07 12:20:28 2013 -0400 14.3 @@ -2755,13 +2755,26 @@ 14.4 // InstanceKlass around to hold obsolete methods so we don't have 14.5 // any other InstanceKlass embedded vtables to update. The vtable 14.6 // holds the Method*s for virtual (but not final) methods. 14.7 - if (ik->vtable_length() > 0 && ik->is_subtype_of(_the_class_oop)) { 14.8 + // Default methods, or concrete methods in interfaces are stored 14.9 + // in the vtable, so if an interface changes we need to check 14.10 + // adjust_method_entries() for every InstanceKlass, which will also 14.11 + // adjust the default method vtable indices. 14.12 + // We also need to adjust any default method entries that are 14.13 + // not yet in the vtable, because the vtable setup is in progress. 14.14 + // This must be done after we adjust the default_methods and 14.15 + // default_vtable_indices for methods already in the vtable. 14.16 + if (ik->vtable_length() > 0 && (_the_class_oop->is_interface() 14.17 + || ik->is_subtype_of(_the_class_oop))) { 14.18 // ik->vtable() creates a wrapper object; rm cleans it up 14.19 ResourceMark rm(_thread); 14.20 ik->vtable()->adjust_method_entries(_matching_old_methods, 14.21 _matching_new_methods, 14.22 _matching_methods_length, 14.23 &trace_name_printed); 14.24 + ik->adjust_default_methods(_matching_old_methods, 14.25 + _matching_new_methods, 14.26 + _matching_methods_length, 14.27 + &trace_name_printed); 14.28 } 14.29 14.30 // If the current class has an itable and we are either redefining an
15.1 --- a/src/share/vm/prims/methodHandles.cpp Sun Oct 06 16:13:50 2013 +0200 15.2 +++ b/src/share/vm/prims/methodHandles.cpp Mon Oct 07 12:20:28 2013 -0400 15.3 @@ -187,12 +187,34 @@ 15.4 receiver_limit = m->method_holder(); 15.5 assert(receiver_limit->verify_itable_index(vmindex), ""); 15.6 flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); 15.7 + if (TraceInvokeDynamic) { 15.8 + ResourceMark rm; 15.9 + tty->print_cr("memberName: invokeinterface method_holder::method: %s, receiver: %s, itableindex: %d, access_flags:", 15.10 + Method::name_and_sig_as_C_string(receiver_limit(), m->name(), m->signature()), 15.11 + receiver_limit()->internal_name(), vmindex); 15.12 + m->access_flags().print_on(tty); 15.13 + if (!m->is_abstract()) { 15.14 + tty->print("default"); 15.15 + } 15.16 + tty->cr(); 15.17 + } 15.18 break; 15.19 15.20 case CallInfo::vtable_call: 15.21 vmindex = info.vtable_index(); 15.22 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); 15.23 assert(receiver_limit->is_subtype_of(m->method_holder()), "virtual call must be type-safe"); 15.24 + if (TraceInvokeDynamic) { 15.25 + ResourceMark rm; 15.26 + tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:", 15.27 + Method::name_and_sig_as_C_string(receiver_limit(), m->name(), m->signature()), 15.28 + receiver_limit()->internal_name(), vmindex); 15.29 + m->access_flags().print_on(tty); 15.30 + if (m->is_default_method()) { 15.31 + tty->print("default"); 15.32 + } 15.33 + tty->cr(); 15.34 + } 15.35 break; 15.36 15.37 case CallInfo::direct_call:
16.1 --- a/src/share/vm/runtime/reflectionUtils.cpp Sun Oct 06 16:13:50 2013 +0200 16.2 +++ b/src/share/vm/runtime/reflectionUtils.cpp Mon Oct 07 12:20:28 2013 -0400 16.3 @@ -1,5 +1,5 @@ 16.4 /* 16.5 - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 16.6 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 16.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 16.8 * 16.9 * This code is free software; you can redistribute it and/or modify it 16.10 @@ -27,8 +27,11 @@ 16.11 #include "memory/universe.inline.hpp" 16.12 #include "runtime/reflectionUtils.hpp" 16.13 16.14 -KlassStream::KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only) { 16.15 - _klass = klass; 16.16 +KlassStream::KlassStream(instanceKlassHandle klass, bool local_only, 16.17 + bool classes_only, bool walk_defaults) { 16.18 + _klass = _base_klass = klass; 16.19 + _base_class_search_defaults = false; 16.20 + _defaults_checked = false; 16.21 if (classes_only) { 16.22 _interfaces = Universe::the_empty_klass_array(); 16.23 } else { 16.24 @@ -37,6 +40,7 @@ 16.25 _interface_index = _interfaces->length(); 16.26 _local_only = local_only; 16.27 _classes_only = classes_only; 16.28 + _walk_defaults = walk_defaults; 16.29 } 16.30 16.31 bool KlassStream::eos() { 16.32 @@ -45,7 +49,13 @@ 16.33 if (!_klass->is_interface() && _klass->super() != NULL) { 16.34 // go up superclass chain (not for interfaces) 16.35 _klass = _klass->super(); 16.36 + // Next for method walks, walk default methods 16.37 + } else if (_walk_defaults && (_defaults_checked == false) && (_base_klass->default_methods() != NULL)) { 16.38 + _base_class_search_defaults = true; 16.39 + _klass = _base_klass; 16.40 + _defaults_checked = true; 16.41 } else { 16.42 + // Next walk transitive interfaces 16.43 if (_interface_index > 0) { 16.44 _klass = _interfaces->at(--_interface_index); 16.45 } else {
17.1 --- a/src/share/vm/runtime/reflectionUtils.hpp Sun Oct 06 16:13:50 2013 +0200 17.2 +++ b/src/share/vm/runtime/reflectionUtils.hpp Mon Oct 07 12:20:28 2013 -0400 17.3 @@ -38,7 +38,7 @@ 17.4 // and (super)interfaces. Streaming is done in reverse order (subclasses first, 17.5 // interfaces last). 17.6 // 17.7 -// for (KlassStream st(k, false, false); !st.eos(); st.next()) { 17.8 +// for (KlassStream st(k, false, false, false); !st.eos(); st.next()) { 17.9 // Klass* k = st.klass(); 17.10 // ... 17.11 // } 17.12 @@ -46,17 +46,21 @@ 17.13 class KlassStream VALUE_OBJ_CLASS_SPEC { 17.14 protected: 17.15 instanceKlassHandle _klass; // current klass/interface iterated over 17.16 - Array<Klass*>* _interfaces; // transitive interfaces for initial class 17.17 + instanceKlassHandle _base_klass; // initial klass/interface to iterate over 17.18 + Array<Klass*>* _interfaces; // transitive interfaces for initial class 17.19 int _interface_index; // current interface being processed 17.20 bool _local_only; // process initial class/interface only 17.21 bool _classes_only; // process classes only (no interfaces) 17.22 + bool _walk_defaults; // process default methods 17.23 + bool _base_class_search_defaults; // time to process default methods 17.24 + bool _defaults_checked; // already checked for default methods 17.25 int _index; 17.26 17.27 - virtual int length() const = 0; 17.28 + virtual int length() = 0; 17.29 17.30 public: 17.31 // constructor 17.32 - KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only); 17.33 + KlassStream(instanceKlassHandle klass, bool local_only, bool classes_only, bool walk_defaults); 17.34 17.35 // testing 17.36 bool eos(); 17.37 @@ -67,6 +71,8 @@ 17.38 // accessors 17.39 instanceKlassHandle klass() const { return _klass; } 17.40 int index() const { return _index; } 17.41 + bool base_class_search_defaults() const { return _base_class_search_defaults; } 17.42 + void base_class_search_defaults(bool b) { _base_class_search_defaults = b; } 17.43 }; 17.44 17.45 17.46 @@ -81,17 +87,24 @@ 17.47 17.48 class MethodStream : public KlassStream { 17.49 private: 17.50 - int length() const { return methods()->length(); } 17.51 - Array<Method*>* methods() const { return _klass->methods(); } 17.52 + int length() { return methods()->length(); } 17.53 + Array<Method*>* methods() { 17.54 + if (base_class_search_defaults()) { 17.55 + base_class_search_defaults(false); 17.56 + return _klass->default_methods(); 17.57 + } else { 17.58 + return _klass->methods(); 17.59 + } 17.60 + } 17.61 public: 17.62 MethodStream(instanceKlassHandle klass, bool local_only, bool classes_only) 17.63 - : KlassStream(klass, local_only, classes_only) { 17.64 + : KlassStream(klass, local_only, classes_only, true) { 17.65 _index = length(); 17.66 next(); 17.67 } 17.68 17.69 void next() { _index--; } 17.70 - Method* method() const { return methods()->at(index()); } 17.71 + Method* method() { return methods()->at(index()); } 17.72 }; 17.73 17.74 17.75 @@ -107,13 +120,13 @@ 17.76 17.77 class FieldStream : public KlassStream { 17.78 private: 17.79 - int length() const { return _klass->java_fields_count(); } 17.80 + int length() { return _klass->java_fields_count(); } 17.81 17.82 fieldDescriptor _fd_buf; 17.83 17.84 public: 17.85 FieldStream(instanceKlassHandle klass, bool local_only, bool classes_only) 17.86 - : KlassStream(klass, local_only, classes_only) { 17.87 + : KlassStream(klass, local_only, classes_only, false) { 17.88 _index = length(); 17.89 next(); 17.90 }
18.1 --- a/src/share/vm/runtime/vmStructs.cpp Sun Oct 06 16:13:50 2013 +0200 18.2 +++ b/src/share/vm/runtime/vmStructs.cpp Mon Oct 07 12:20:28 2013 -0400 18.3 @@ -289,6 +289,7 @@ 18.4 nonstatic_field(ConstantPoolCache, _constant_pool, ConstantPool*) \ 18.5 nonstatic_field(InstanceKlass, _array_klasses, Klass*) \ 18.6 nonstatic_field(InstanceKlass, _methods, Array<Method*>*) \ 18.7 + nonstatic_field(InstanceKlass, _default_methods, Array<Method*>*) \ 18.8 nonstatic_field(InstanceKlass, _local_interfaces, Array<Klass*>*) \ 18.9 nonstatic_field(InstanceKlass, _transitive_interfaces, Array<Klass*>*) \ 18.10 nonstatic_field(InstanceKlass, _fields, Array<u2>*) \ 18.11 @@ -323,6 +324,7 @@ 18.12 nonstatic_field(nmethodBucket, _count, int) \ 18.13 nonstatic_field(nmethodBucket, _next, nmethodBucket*) \ 18.14 nonstatic_field(InstanceKlass, _method_ordering, Array<int>*) \ 18.15 + nonstatic_field(InstanceKlass, _default_vtable_indices, Array<int>*) \ 18.16 nonstatic_field(Klass, _super_check_offset, juint) \ 18.17 nonstatic_field(Klass, _secondary_super_cache, Klass*) \ 18.18 nonstatic_field(Klass, _secondary_supers, Array<Klass*>*) \