Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/ClassLoaderReferenceImpl.java
41161 views
/*1* Copyright (c) 1998, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import java.util.ArrayList;28import java.util.Collections;29import java.util.Iterator;30import java.util.List;3132import com.sun.jdi.ClassLoaderReference;33import com.sun.jdi.ClassNotLoadedException;34import com.sun.jdi.ReferenceType;35import com.sun.jdi.Type;36import com.sun.jdi.VirtualMachine;3738public class ClassLoaderReferenceImpl extends ObjectReferenceImpl39implements ClassLoaderReference40{41// This is cached only while the VM is suspended42private static class Cache extends ObjectReferenceImpl.Cache {43List<ReferenceType> visibleClasses = null;44}4546protected ObjectReferenceImpl.Cache newCache() {47return new Cache();48}4950ClassLoaderReferenceImpl(VirtualMachine aVm, long ref) {51super(aVm, ref);52vm.state().addListener(this);53}5455protected String description() {56return "ClassLoaderReference " + uniqueID();57}5859public List<ReferenceType> definedClasses() {60ArrayList<ReferenceType> definedClasses = new ArrayList<>();61vm.forEachClass(type -> {62if (type.isPrepared() &&63equals(type.classLoader())) {64definedClasses.add(type);65}66});67return definedClasses;68}6970public List<ReferenceType> visibleClasses() {71List<ReferenceType> classes = null;72try {73Cache local = (Cache)getCache();7475if (local != null) {76classes = local.visibleClasses;77}78if (classes == null) {79JDWP.ClassLoaderReference.VisibleClasses.ClassInfo[]80jdwpClasses = JDWP.ClassLoaderReference.VisibleClasses.81process(vm, this).classes;82classes = new ArrayList<>(jdwpClasses.length);83for (int i = 0; i < jdwpClasses.length; ++i) {84classes.add(vm.referenceType(jdwpClasses[i].typeID,85jdwpClasses[i].refTypeTag));86}87classes = Collections.unmodifiableList(classes);88if (local != null) {89local.visibleClasses = classes;90if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {91vm.printTrace(description() +92" temporarily caching visible classes (count = " +93classes.size() + ")");94}95}96}97} catch (JDWPException exc) {98throw exc.toJDIException();99}100return classes;101}102103Type findType(String signature) throws ClassNotLoadedException {104List<ReferenceType> types = visibleClasses();105106// first check already loaded classes and possibly avoid massive signature retrieval later107for (ReferenceType type : vm.classesBySignature(signature)) {108if (types.contains(type)) {109return type;110}111}112113for (ReferenceType type : types) {114if (type.signature().equals(signature)) {115return type;116}117}118119String typeName = new JNITypeParser(signature).typeName();120throw new ClassNotLoadedException(typeName, "Class " + typeName + " not loaded");121}122123byte typeValueKey() {124return JDWP.Tag.CLASS_LOADER;125}126}127128129