Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/classfile/ClassLoaderData.java
41161 views
/*1* Copyright (c) 2012, 2020, 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*/2324package sun.jvm.hotspot.classfile;2526import sun.jvm.hotspot.debugger.*;27import sun.jvm.hotspot.memory.*;28import sun.jvm.hotspot.runtime.*;29import sun.jvm.hotspot.oops.*;30import sun.jvm.hotspot.types.*;31import sun.jvm.hotspot.utilities.Observable;32import sun.jvm.hotspot.utilities.Observer;3334public class ClassLoaderData extends VMObject {35static {36VM.registerVMInitializedObserver(new Observer() {37public void update(Observable o, Object data) {38initialize(VM.getVM().getTypeDataBase());39}40});41}4243private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {44Type type = db.lookupType("ClassLoaderData");45classLoaderFieldOffset = type.getAddressField("_class_loader").getOffset();46nextField = type.getAddressField("_next");47klassesField = new MetadataField(type.getAddressField("_klasses"), 0);48hasClassMirrorHolderField = new CIntField(type.getCIntegerField("_has_class_mirror_holder"), 0);49dictionaryField = type.getAddressField("_dictionary");50}5152private static long classLoaderFieldOffset;53private static AddressField nextField;54private static MetadataField klassesField;55private static CIntField hasClassMirrorHolderField;56private static AddressField dictionaryField;5758public ClassLoaderData(Address addr) {59super(addr);60}6162public Dictionary dictionary() {63Address tmp = dictionaryField.getValue();64return (Dictionary) VMObjectFactory.newObject(Dictionary.class, tmp);65}6667public static ClassLoaderData instantiateWrapperFor(Address addr) {68if (addr == null) {69return null;70}71return new ClassLoaderData(addr);72}7374public Oop getClassLoader() {75Address addr = getAddress().addOffsetTo(classLoaderFieldOffset);76VMOopHandle vmOopHandle = VMObjectFactory.newObject(VMOopHandle.class, addr);77return vmOopHandle.resolve();78}7980public boolean gethasClassMirrorHolder() {81return hasClassMirrorHolderField.getValue(this) != 0;82}8384public ClassLoaderData next() {85return instantiateWrapperFor(nextField.getValue(getAddress()));86}8788public Klass getKlasses() { return (Klass)klassesField.getValue(this); }8990/** Lookup an already loaded class. If not found null is returned. */91public Klass find(String className) {92for (Klass l = getKlasses(); l != null; l = l.getNextLinkKlass()) {93if (l.getName().equals(className)) {94if (l instanceof InstanceKlass && !((InstanceKlass)l).isLoaded()) {95return null; // don't return partially loaded classes96} else {97return l;98}99}100}101return null;102}103104/** Iterate over all klasses - including object, primitive105array klasses */106public void classesDo(ClassLoaderDataGraph.ClassVisitor v) {107for (Klass l = getKlasses(); l != null; l = l.getNextLinkKlass()) {108// Only visit InstanceKlasses that are at least in the "loaded" init_state. Otherwise109// the InstanceKlass won't have some required fields initialized, which can cause problems.110if (l instanceof InstanceKlass && !((InstanceKlass)l).isLoaded()) {111continue;112}113v.visit(l);114}115}116117/** Iterate over all klasses in the dictionary, including initiating loader. */118public void allEntriesDo(ClassLoaderDataGraph.ClassAndLoaderVisitor v) {119dictionary().allEntriesDo(v, getClassLoader());120}121}122123124