Path: blob/master/src/hotspot/share/oops/arrayKlass.cpp
40951 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaClasses.hpp"26#include "classfile/moduleEntry.hpp"27#include "classfile/vmClasses.hpp"28#include "classfile/vmSymbols.hpp"29#include "gc/shared/collectedHeap.inline.hpp"30#include "jvmtifiles/jvmti.h"31#include "memory/metaspaceClosure.hpp"32#include "memory/resourceArea.hpp"33#include "memory/universe.hpp"34#include "oops/arrayKlass.hpp"35#include "oops/arrayOop.hpp"36#include "oops/instanceKlass.hpp"37#include "oops/klass.inline.hpp"38#include "oops/objArrayOop.hpp"39#include "oops/oop.inline.hpp"40#include "runtime/handles.inline.hpp"4142int ArrayKlass::static_size(int header_size) {43// size of an array klass object44assert(header_size <= InstanceKlass::header_size(), "bad header size");45// If this assert fails, see comments in base_create_array_klass.46header_size = InstanceKlass::header_size();47int vtable_len = Universe::base_vtable_size();48int size = header_size + vtable_len;49return align_metadata_size(size);50}515253InstanceKlass* ArrayKlass::java_super() const {54if (super() == NULL) return NULL; // bootstrap case55// Array klasses have primary supertypes which are not reported to Java.56// Example super chain: String[][] -> Object[][] -> Object[] -> Object57return vmClasses::Object_klass();58}596061oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {62ShouldNotReachHere();63return NULL;64}6566// find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined67Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {68// There are no fields in an array klass but look to the super class (Object)69assert(super(), "super klass must be present");70return super()->find_field(name, sig, fd);71}7273Method* ArrayKlass::uncached_lookup_method(const Symbol* name,74const Symbol* signature,75OverpassLookupMode overpass_mode,76PrivateLookupMode private_mode) const {77// There are no methods in an array klass but the super class (Object) has some78assert(super(), "super klass must be present");79// Always ignore overpass methods in superclasses, although technically the80// super klass of an array, (j.l.Object) should not have81// any overpass methods present.82return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode);83}8485ArrayKlass::ArrayKlass(Symbol* name, KlassID id) :86Klass(id),87_dimension(1),88_higher_dimension(NULL),89_lower_dimension(NULL) {90// Arrays don't add any new methods, so their vtable is the same size as91// the vtable of klass Object.92set_vtable_length(Universe::base_vtable_size());93set_name(name);94set_super(Universe::is_bootstrapping() ? NULL : vmClasses::Object_klass());95set_layout_helper(Klass::_lh_neutral_value);96set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)97JFR_ONLY(INIT_ID(this);)98}99100101// Initialization of vtables and mirror object is done separatly from base_create_array_klass,102// since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.103void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {104k->initialize_supers(super_klass, NULL, CHECK);105k->vtable().initialize_vtable();106107// During bootstrapping, before java.base is defined, the module_entry may not be present yet.108// These classes will be put on a fixup list and their module fields will be patched once109// java.base is defined.110assert((module_entry != NULL) || ((module_entry == NULL) && !ModuleEntryTable::javabase_defined()),111"module entry not available post " JAVA_BASE_NAME " definition");112oop module = (module_entry != NULL) ? module_entry->module() : (oop)NULL;113java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module), Handle(), Handle(), CHECK);114}115116GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,117Array<InstanceKlass*>* transitive_interfaces) {118// interfaces = { cloneable_klass, serializable_klass };119assert(num_extra_slots == 0, "sanity of primitive array type");120assert(transitive_interfaces == NULL, "sanity");121// Must share this for correct bootstrapping!122set_secondary_supers(Universe::the_array_interfaces_array());123return NULL;124}125126objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {127check_array_allocation_length(length, arrayOopDesc::max_array_length(T_ARRAY), CHECK_NULL);128int size = objArrayOopDesc::object_size(length);129Klass* k = array_klass(n+dimension(), CHECK_NULL);130ArrayKlass* ak = ArrayKlass::cast(k);131objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,132/* do_zero */ true, CHECK_NULL);133// initialization to NULL not necessary, area already cleared134return o;135}136137void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {138Klass* k = this;139// Iterate over this array klass and all higher dimensions140while (k != NULL) {141f(k, CHECK);142k = ArrayKlass::cast(k)->higher_dimension();143}144}145146void ArrayKlass::array_klasses_do(void f(Klass* k)) {147Klass* k = this;148// Iterate over this array klass and all higher dimensions149while (k != NULL) {150f(k);151k = ArrayKlass::cast(k)->higher_dimension();152}153}154155jint ArrayKlass::compute_modifier_flags() const {156return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;157}158159// JVMTI support160161jint ArrayKlass::jvmti_class_status() const {162return JVMTI_CLASS_STATUS_ARRAY;163}164165void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {166Klass::metaspace_pointers_do(it);167168ResourceMark rm;169log_trace(cds)("Iter(ArrayKlass): %p (%s)", this, external_name());170171// need to cast away volatile172it->push((Klass**)&_higher_dimension);173it->push((Klass**)&_lower_dimension);174}175176void ArrayKlass::remove_unshareable_info() {177Klass::remove_unshareable_info();178if (_higher_dimension != NULL) {179ArrayKlass *ak = ArrayKlass::cast(higher_dimension());180ak->remove_unshareable_info();181}182}183184void ArrayKlass::remove_java_mirror() {185Klass::remove_java_mirror();186if (_higher_dimension != NULL) {187ArrayKlass *ak = ArrayKlass::cast(higher_dimension());188ak->remove_java_mirror();189}190}191192void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {193assert(loader_data == ClassLoaderData::the_null_class_loader_data(), "array classes belong to null loader");194Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);195// Klass recreates the component mirror also196197if (_higher_dimension != NULL) {198ArrayKlass *ak = ArrayKlass::cast(higher_dimension());199ak->restore_unshareable_info(loader_data, protection_domain, CHECK);200}201}202203// Printing204205void ArrayKlass::print_on(outputStream* st) const {206assert(is_klass(), "must be klass");207Klass::print_on(st);208}209210void ArrayKlass::print_value_on(outputStream* st) const {211assert(is_klass(), "must be klass");212for(int index = 0; index < dimension(); index++) {213st->print("[]");214}215}216217void ArrayKlass::oop_print_on(oop obj, outputStream* st) {218assert(obj->is_array(), "must be array");219Klass::oop_print_on(obj, st);220st->print_cr(" - length: %d", arrayOop(obj)->length());221}222223224// Verification225226void ArrayKlass::verify_on(outputStream* st) {227Klass::verify_on(st);228}229230void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {231guarantee(obj->is_array(), "must be array");232arrayOop a = arrayOop(obj);233guarantee(a->length() >= 0, "array with negative length?");234}235236237