Home | History | Annotate | Download | only in jdi
      1 /*
      2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package com.sun.tools.jdi;
     27 
     28 import com.sun.jdi.*;
     29 import java.util.*;
     30 
     31 public class ClassLoaderReferenceImpl extends ObjectReferenceImpl
     32                   implements ClassLoaderReference, VMListener  {
     33 
     34     // This is cached only while the VM is suspended
     35     private static class Cache extends ObjectReferenceImpl.Cache {
     36         List<ReferenceType> visibleClasses = null;
     37     }
     38 
     39     protected ObjectReferenceImpl.Cache newCache() {
     40         return new Cache();
     41     }
     42 
     43     ClassLoaderReferenceImpl(VirtualMachine aVm, long ref) {
     44         super(aVm, ref);
     45         vm.state().addListener(this);
     46     }
     47 
     48     protected String description() {
     49         return "ClassLoaderReference " + uniqueID();
     50     }
     51 
     52     public List<ReferenceType> definedClasses() {
     53         ArrayList<ReferenceType> definedClasses = new ArrayList<ReferenceType>();
     54         for (ReferenceType type :  vm.allClasses()) {
     55             if (type.isPrepared() &&
     56                 equals(type.classLoader())) {
     57                 definedClasses.add(type);
     58             }
     59         }
     60         return definedClasses;
     61     }
     62 
     63     public List<ReferenceType> visibleClasses() {
     64         List<ReferenceType> classes = null;
     65         try {
     66             Cache local = (Cache)getCache();
     67 
     68             if (local != null) {
     69                 classes = local.visibleClasses;
     70             }
     71             if (classes == null) {
     72                 JDWP.ClassLoaderReference.VisibleClasses.ClassInfo[]
     73                   jdwpClasses = JDWP.ClassLoaderReference.VisibleClasses.
     74                                             process(vm, this).classes;
     75                 classes = new ArrayList<ReferenceType>(jdwpClasses.length);
     76                 for (int i = 0; i < jdwpClasses.length; ++i) {
     77                     classes.add(vm.referenceType(jdwpClasses[i].typeID,
     78                                                  jdwpClasses[i].refTypeTag));
     79                 }
     80                 classes = Collections.unmodifiableList(classes);
     81                 if (local != null) {
     82                     local.visibleClasses = classes;
     83                     if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
     84                         vm.printTrace(description() +
     85                            " temporarily caching visible classes (count = " +
     86                                       classes.size() + ")");
     87                     }
     88                 }
     89             }
     90         } catch (JDWPException exc) {
     91             throw exc.toJDIException();
     92         }
     93         return classes;
     94     }
     95 
     96     Type findType(String signature) throws ClassNotLoadedException {
     97         List<ReferenceType> types = visibleClasses();
     98         Iterator<ReferenceType> iter = types.iterator();
     99         while (iter.hasNext()) {
    100             ReferenceType type = iter.next();
    101             if (type.signature().equals(signature)) {
    102                 return type;
    103             }
    104         }
    105         JNITypeParser parser = new JNITypeParser(signature);
    106         throw new ClassNotLoadedException(parser.typeName(),
    107                                          "Class " + parser.typeName() + " not loaded");
    108     }
    109 
    110     byte typeValueKey() {
    111         return JDWP.Tag.CLASS_LOADER;
    112     }
    113 }
    114