|
Java example source code file (klassVtable.cpp)
The klassVtable.cpp Java example source code/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "classfile/systemDictionary.hpp" #include "classfile/vmSymbols.hpp" #include "gc_implementation/shared/markSweep.inline.hpp" #include "memory/gcLocker.hpp" #include "memory/resourceArea.hpp" #include "memory/universe.inline.hpp" #include "oops/instanceKlass.hpp" #include "oops/klassVtable.hpp" #include "oops/method.hpp" #include "oops/objArrayOop.hpp" #include "oops/oop.inline.hpp" #include "prims/jvmtiRedefineClassesTrace.hpp" #include "runtime/arguments.hpp" #include "runtime/handles.inline.hpp" #include "utilities/copy.hpp" inline InstanceKlass* klassVtable::ik() const { Klass* k = _klass(); assert(k->oop_is_instance(), "not an InstanceKlass"); return (InstanceKlass*)k; } // this function computes the vtable size (including the size needed for miranda // methods) and the number of miranda methods in this class. // Note on Miranda methods: Let's say there is a class C that implements // interface I, and none of C's superclasses implements I. // Let's say there is an abstract method m in I that neither C // nor any of its super classes implement (i.e there is no method of any access, // with the same name and signature as m), then m is a Miranda method which is // entered as a public abstract method in C's vtable. From then on it should // treated as any other public method in C for method over-ride purposes. void klassVtable::compute_vtable_size_and_num_mirandas( int* vtable_length_ret, int* num_new_mirandas, GrowableArray<Method*>* all_mirandas, Klass* super, Array<Method*>* methods, AccessFlags class_flags, Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces, TRAPS) { No_Safepoint_Verifier nsv; // set up default result values int vtable_length = 0; // start off with super's vtable length InstanceKlass* sk = (InstanceKlass*)super; vtable_length = super == NULL ? 0 : sk->vtable_length(); // go thru each method in the methods table to see if it needs a new entry int len = methods->length(); for (int i = 0; i < len; i++) { assert(methods->at(i)->is_method(), "must be a Method*"); methodHandle mh(THREAD, methods->at(i)); if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) { vtable_length += vtableEntry::size(); // we need a new entry } } GrowableArray<Method*> new_mirandas(20); // compute the number of mirandas methods that must be added to the end get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); *num_new_mirandas = new_mirandas.length(); // Interfaces do not need interface methods in their vtables // This includes miranda methods and during later processing, default methods if (!class_flags.is_interface()) { vtable_length += *num_new_mirandas * vtableEntry::size(); } if (Universe::is_bootstrapping() && vtable_length == 0) { // array classes don't have their superclass set correctly during // bootstrapping vtable_length = Universe::base_vtable_size(); } if (super == NULL && !Universe::is_bootstrapping() && vtable_length != Universe::base_vtable_size()) { // Someone is attempting to redefine java.lang.Object incorrectly. The // only way this should happen is from // SystemDictionary::resolve_from_stream(), which will detect this later // and throw a security exception. So don't assert here to let // the exception occur. vtable_length = Universe::base_vtable_size(); } assert(super != NULL || vtable_length == Universe::base_vtable_size(), "bad vtable size for class Object"); assert(vtable_length % vtableEntry::size() == 0, "bad vtable length"); assert(vtable_length >= Universe::base_vtable_size(), "vtable too small"); *vtable_length_ret = vtable_length; } int klassVtable::index_of(Method* m, int len) const { assert(m->has_vtable_index(), "do not ask this of non-vtable methods"); return m->vtable_index(); } // Copy super class's vtable to the first part (prefix) of this class's vtable, // and return the number of entries copied. Expects that 'super' is the Java // super class (arrays can have "array" super classes that must be skipped). int klassVtable::initialize_from_super(KlassHandle super) { if (super.is_null()) { return 0; } else { // copy methods from superKlass // can't inherit from array class, so must be InstanceKlass assert(super->oop_is_instance(), "must be instance klass"); InstanceKlass* sk = (InstanceKlass*)super(); klassVtable* superVtable = sk->vtable(); assert(superVtable->length() <= _length, "vtable too short"); #ifdef ASSERT superVtable->verify(tty, true); #endif superVtable->copy_vtable_to(table()); #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm; tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length); } #endif return superVtable->length(); } } // // Revised lookup semantics introduced 1.3 (Kestrel beta) void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) { // Note: Arrays can have intermediate array supers. Use java_super to skip them. KlassHandle super (THREAD, klass()->java_super()); int nofNewEntries = 0; if (PrintVtables && !klass()->oop_is_array()) { ResourceMark rm(THREAD); tty->print_cr("Initializing: %s", _klass->name()->as_C_string()); } #ifdef ASSERT oop* end_of_obj = (oop*)_klass() + _klass()->size(); oop* end_of_vtable = (oop*)&table()[_length]; assert(end_of_vtable <= end_of_obj, "vtable extends beyond end"); #endif if (Universe::is_bootstrapping()) { // just clear everything for (int i = 0; i < _length; i++) table()[i].clear(); return; } int super_vtable_len = initialize_from_super(super); if (klass()->oop_is_array()) { assert(super_vtable_len == _length, "arrays shouldn't introduce new methods"); } else { assert(_klass->oop_is_instance(), "must be InstanceKlass"); Array<Method*>* methods = ik()->methods(); int len = methods->length(); int initialized = super_vtable_len; // Check each of this class's methods against super; // if override, replace in copy of super vtable, otherwise append to end for (int i = 0; i < len; i++) { // update_inherited_vtable can stop for gc - ensure using handles HandleMark hm(THREAD); assert(methods->at(i)->is_method(), "must be a Method*"); methodHandle mh(THREAD, methods->at(i)); bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK); if (needs_new_entry) { put_method_at(mh(), initialized); mh()->set_vtable_index(initialized); // set primary vtable index initialized++; } } // update vtable with default_methods Array<Method*>* default_methods = ik()->default_methods(); if (default_methods != NULL) { len = default_methods->length(); if (len > 0) { Array<int>* def_vtable_indices = NULL; if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) { def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK); } else { assert(def_vtable_indices->length() == len, "reinit vtable len?"); } for (int i = 0; i < len; i++) { HandleMark hm(THREAD); assert(default_methods->at(i)->is_method(), "must be a Method*"); methodHandle mh(THREAD, default_methods->at(i)); bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK); // needs new entry if (needs_new_entry) { put_method_at(mh(), initialized); def_vtable_indices->at_put(i, initialized); //set vtable index initialized++; } } } } // add miranda methods; it will also return the updated initialized // Interfaces do not need interface methods in their vtables // This includes miranda methods and during later processing, default methods if (!ik()->is_interface()) { initialized = fill_in_mirandas(initialized); } // In class hierarchies where the accessibility is not increasing (i.e., going from private -> // package_private -> public/protected), the vtable might actually be smaller than our initial // calculation. assert(initialized <= _length, "vtable initialization failed"); for(;initialized < _length; initialized++) { put_method_at(NULL, initialized); } NOT_PRODUCT(verify(tty, true)); } } // Called for cases where a method does not override its superclass' vtable entry // For bytecodes not produced by javac together it is possible that a method does not override // the superclass's method, but might indirectly override a super-super class's vtable entry // If none found, return a null superk, else return the superk of the method this does override InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) { InstanceKlass* superk = initialsuper; while (superk != NULL && superk->super() != NULL) { InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super()); klassVtable* ssVtable = supersuperklass->vtable(); if (vtable_index < ssVtable->length()) { Method* super_method = ssVtable->method_at(vtable_index); #ifndef PRODUCT Symbol* name= target_method()->name(); Symbol* signature = target_method()->signature(); assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch"); #endif if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) { #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm(THREAD); char* sig = target_method()->name_and_sig_as_C_string(); tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ", supersuperklass->internal_name(), _klass->internal_name(), sig, vtable_index); super_method->access_flags().print_on(tty); if (super_method->is_default_method()) { tty->print("default "); } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); if (target_method->is_default_method()) { tty->print("default "); } } #endif /*PRODUCT*/ break; // return found superk } } else { // super class has no vtable entry here, stop transitive search superk = (InstanceKlass*)NULL; break; } // if no override found yet, continue to search up superk = InstanceKlass::cast(superk->super()); } return superk; } // Update child's copy of super vtable for overrides // OR return true if a new vtable entry is required. // Only called for InstanceKlass's, i.e. not for arrays // If that changed, could not use _klass as handle for klass bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS) { ResourceMark rm; bool allocate_new = true; assert(klass->oop_is_instance(), "must be InstanceKlass"); Array<int>* def_vtable_indices = NULL; bool is_default = false; // default methods are concrete methods in superinterfaces which are added to the vtable // with their real method_holder // Since vtable and itable indices share the same storage, don't touch // the default method's real vtable/itable index // default_vtable_indices stores the vtable value relative to this inheritor if (default_index >= 0 ) { is_default = true; def_vtable_indices = klass->default_vtable_indices(); assert(def_vtable_indices != NULL, "def vtable alloc?"); assert(default_index <= def_vtable_indices->length(), "def vtable len?"); } else { assert(klass == target_method()->method_holder(), "caller resp."); // Initialize the method's vtable index to "nonvirtual". // If we allocate a vtable entry, we will update it to a non-negative number. target_method()->set_vtable_index(Method::nonvirtual_vtable_index); } // Static and <init> methods are never in if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { return false; } if (target_method->is_final_method(klass->access_flags())) { // a final method never needs a new entry; final methods can be statically // resolved and they have to be present in the vtable only if they override // a super's method, in which case they re-use its entry allocate_new = false; } else if (klass->is_interface()) { allocate_new = false; // see note below in needs_new_vtable_entry // An interface never allocates new vtable slots, only inherits old ones. // This method will either be assigned its own itable index later, // or be assigned an inherited vtable index in the loop below. // default methods inherited by classes store their vtable indices // in the inheritor's default_vtable_indices // default methods inherited by interfaces may already have a // valid itable index, if so, don't change it // overpass methods in an interface will be assigned an itable index later // by an inheriting class if (!is_default || !target_method()->has_itable_index()) { target_method()->set_vtable_index(Method::pending_itable_index); } } // we need a new entry if there is no superclass if (klass->super() == NULL) { return allocate_new; } // private methods in classes always have a new entry in the vtable // specification interpretation since classic has // private methods not overriding // JDK8 adds private methods in interfaces which require invokespecial if (target_method()->is_private()) { return allocate_new; } // search through the vtable and update overridden entries // Since check_signature_loaders acquires SystemDictionary_lock // which can block for gc, once we are in this loop, use handles // For classfiles built with >= jdk7, we now look for transitive overrides Symbol* name = target_method()->name(); Symbol* signature = target_method()->signature(); KlassHandle target_klass(THREAD, target_method()->method_holder()); if (target_klass == NULL) { target_klass = _klass; } Handle target_loader(THREAD, target_klass->class_loader()); Symbol* target_classname = target_klass->name(); for(int i = 0; i < super_vtable_len; i++) { Method* super_method = method_at(i); // Check if method name matches if (super_method->name() == name && super_method->signature() == signature) { // get super_klass for method_holder for the found method InstanceKlass* super_klass = super_method->method_holder(); if (is_default || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) && ((super_klass = find_transitive_override(super_klass, target_method, i, target_loader, target_classname, THREAD)) != (InstanceKlass*)NULL)))) { // overriding, so no new entry allocate_new = false; if (checkconstraints) { // Override vtable entry if passes loader constraint check // if loader constraint checking requested // No need to visit his super, since he and his super // have already made any needed loader constraints. // Since loader constraints are transitive, it is enough // to link to the first super, and we get all the others. Handle super_loader(THREAD, super_klass->class_loader()); if (target_loader() != super_loader()) { ResourceMark rm(THREAD); Symbol* failed_type_symbol = SystemDictionary::check_signature_loaders(signature, target_loader, super_loader, true, CHECK_(false)); if (failed_type_symbol != NULL) { const char* msg = "loader constraint violation: when resolving " "overridden method \"%s\" the class loader (instance" " of %s) of the current class, %s, and its superclass loader " "(instance of %s), have different Class objects for the type " "%s used in the signature"; char* sig = target_method()->name_and_sig_as_C_string(); const char* loader1 = SystemDictionary::loader_name(target_loader()); char* current = target_klass->name()->as_C_string(); const char* loader2 = SystemDictionary::loader_name(super_loader()); char* failed_type_name = failed_type_symbol->as_C_string(); size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + strlen(current) + strlen(loader2) + strlen(failed_type_name); char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, failed_type_name); THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false); } } } put_method_at(target_method(), i); if (!is_default) { target_method()->set_vtable_index(i); } else { if (def_vtable_indices != NULL) { def_vtable_indices->at_put(default_index, i); } assert(super_method->is_default_method() || super_method->is_overpass() || super_method->is_abstract(), "default override error"); } #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm(THREAD); char* sig = target_method()->name_and_sig_as_C_string(); tty->print("overriding with %s::%s index %d, original flags: ", target_klass->internal_name(), sig, i); super_method->access_flags().print_on(tty); if (super_method->is_default_method()) { tty->print("default "); } if (super_method->is_overpass()) { tty->print("overpass"); } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); if (target_method->is_default_method()) { tty->print("default "); } if (target_method->is_overpass()) { tty->print("overpass"); } tty->cr(); } #endif /*PRODUCT*/ } else { // allocate_new = true; default. We might override one entry, // but not override another. Once we override one, not need new #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm(THREAD); char* sig = target_method()->name_and_sig_as_C_string(); tty->print("NOT overriding with %s::%s index %d, original flags: ", target_klass->internal_name(), sig,i); super_method->access_flags().print_on(tty); if (super_method->is_default_method()) { tty->print("default "); } if (super_method->is_overpass()) { tty->print("overpass"); } tty->print("overriders flags: "); target_method->access_flags().print_on(tty); if (target_method->is_default_method()) { tty->print("default "); } if (target_method->is_overpass()) { tty->print("overpass"); } tty->cr(); } #endif /*PRODUCT*/ } } } return allocate_new; } void klassVtable::put_method_at(Method* m, int index) { #ifndef PRODUCT if (PrintVtables && Verbose) { ResourceMark rm; const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>"; tty->print("adding %s at index %d, flags: ", sig, index); if (m != NULL) { m->access_flags().print_on(tty); if (m->is_default_method()) { tty->print("default "); } if (m->is_overpass()) { tty->print("overpass"); } } tty->cr(); } #endif table()[index].set(m); } // Find out if a method "m" with superclass "super", loader "classloader" and // name "classname" needs a new vtable entry. Let P be a class package defined // by "classloader" and "classname". // NOTE: The logic used here is very similar to the one used for computing // the vtables indices for a method. We cannot directly use that function because, // we allocate the InstanceKlass at load time, and that requires that the // superclass has been loaded. // However, the vtable entries are filled in at link time, and therefore // the superclass' vtable may not yet have been filled in. bool klassVtable::needs_new_vtable_entry(methodHandle target_method, Klass* super, Handle classloader, Symbol* classname, AccessFlags class_flags, TRAPS) { if (class_flags.is_interface()) { // Interfaces do not use vtables, so there is no point to assigning // a vtable index to any of their methods. If we refrain from doing this, // we can use Method::_vtable_index to hold the itable index return false; } if (target_method->is_final_method(class_flags) || // a final method never needs a new entry; final methods can be statically // resolved and they have to be present in the vtable only if they override // a super's method, in which case they re-use its entry (target_method()->is_static()) || // static methods don't need to be in vtable (target_method()->name() == vmSymbols::object_initializer_name()) // <init> is never called dynamically-bound ) { return false; } // Concrete interface methods do not need new entries, they override // abstract method entries using default inheritance rules if (target_method()->method_holder() != NULL && target_method()->method_holder()->is_interface() && !target_method()->is_abstract() ) { return false; } // we need a new entry if there is no superclass if (super == NULL) { return true; } // private methods in classes always have a new entry in the vtable // specification interpretation since classic has // private methods not overriding // JDK8 adds private methods in interfaces which require invokespecial if (target_method()->is_private()) { return true; } // search through the super class hierarchy to see if we need // a new entry ResourceMark rm; Symbol* name = target_method()->name(); Symbol* signature = target_method()->signature(); Klass* k = super; Method* super_method = NULL; InstanceKlass *holder = NULL; Method* recheck_method = NULL; while (k != NULL) { // lookup through the hierarchy for a method with matching name and sign. super_method = InstanceKlass::cast(k)->lookup_method(name, signature); if (super_method == NULL) { break; // we still have to search for a matching miranda method } // get the class holding the matching method // make sure you use that class for is_override InstanceKlass* superk = super_method->method_holder(); // we want only instance method matches // pretend private methods are not in the super vtable // since we do override around them: e.g. a.m pub/b.m private/c.m pub, // ignore private, c.m pub does override a.m pub // For classes that were not javac'd together, we also do transitive overriding around // methods that have less accessibility if ((!super_method->is_static()) && (!super_method->is_private())) { if (superk->is_override(super_method, classloader, classname, THREAD)) { return false; // else keep looking for transitive overrides } } // Start with lookup result and continue to search up k = superk->super(); // haven't found an override match yet; continue to look } // if the target method is public or protected it may have a matching // miranda method in the super, whose entry it should re-use. // Actually, to handle cases that javac would not generate, we need // this check for all access permissions. InstanceKlass *sk = InstanceKlass::cast(super); if (sk->has_miranda_methods()) { if (sk->lookup_method_in_all_interfaces(name, signature, false) != NULL) { return false; // found a matching miranda; we do not need a new entry } } return true; // found no match; we need a new entry } // Support for miranda methods // get the vtable index of a miranda method with matching "name" and "signature" int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) { // search from the bottom, might be faster for (int i = (length() - 1); i >= 0; i--) { Method* m = table()[i].method(); if (is_miranda_entry_at(i) && m->name() == name && m->signature() == signature) { return i; } } return Method::invalid_vtable_index; } // check if an entry at an index is miranda // requires that method m at entry be declared ("held") by an interface. bool klassVtable::is_miranda_entry_at(int i) { Method* m = method_at(i); Klass* method_holder = m->method_holder(); InstanceKlass *mhk = InstanceKlass::cast(method_holder); // miranda methods are public abstract instance interface methods in a class's vtable if (mhk->is_interface()) { assert(m->is_public(), "should be public"); assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); // the search could find a miranda or a default method if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) { return true; } } return false; } // check if a method is a miranda method, given a class's methods table, // its default_method table and its super // Miranda methods are calculated twice: // first: before vtable size calculation: including abstract and default // This is seen by default method creation // Second: recalculated during vtable initialization: only abstract // This is seen by link resolution and selection. // "miranda" means not static, not defined by this class. // private methods in interfaces do not belong in the miranda list. // the caller must make sure that the method belongs to an interface implemented by the class // Miranda methods only include public interface instance methods // Not private methods, not static methods, not default == concrete abstract // Miranda methods also do not include overpass methods in interfaces bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, Array<Method*>* default_methods, Klass* super) { if (m->is_static() || m->is_private() || m->is_overpass()) { return false; } Symbol* name = m->name(); Symbol* signature = m->signature(); if (InstanceKlass::find_instance_method(class_methods, name, signature) == NULL) { // did not find it in the method table of the current class if ((default_methods == NULL) || InstanceKlass::find_method(default_methods, name, signature) == NULL) { if (super == NULL) { // super doesn't exist return true; } Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); while (mo != NULL && mo->access_flags().is_static() && mo->method_holder() != NULL && mo->method_holder()->super() != NULL) { mo = mo->method_holder()->super()->uncached_lookup_method(name, signature); } if (mo == NULL || mo->access_flags().is_private() ) { // super class hierarchy does not implement it or protection is different return true; } } } return false; } // Scans current_interface_methods for miranda methods that do not // already appear in new_mirandas, or default methods, and are also not defined-and-non-private // in super (superclass). These mirandas are added to all_mirandas if it is // not null; in addition, those that are not duplicates of miranda methods // inherited by super from its interfaces are added to new_mirandas. // Thus, new_mirandas will be the set of mirandas that this class introduces, // all_mirandas will be the set of all mirandas applicable to this class // including all defined in superclasses. void klassVtable::add_new_mirandas_to_lists( GrowableArray<Method*>* new_mirandas, GrowableArray Other Java examples (source code examples)Here is a short list of links related to this Java klassVtable.cpp source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.