KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > ClassLoaderReferenceImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.jdi.internal.jdwp.JdwpClassLoaderID;
21 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
22 import org.eclipse.jdi.internal.jdwp.JdwpID;
23 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket;
24
25 import com.sun.jdi.ClassLoaderReference;
26 import com.sun.jdi.ClassNotPreparedException;
27 import com.sun.jdi.ReferenceType;
28
29 /**
30  * this class implements the corresponding interfaces
31  * declared by the JDI specification. See the com.sun.jdi package
32  * for more information.
33  *
34  */

35 public class ClassLoaderReferenceImpl extends ObjectReferenceImpl implements ClassLoaderReference {
36     /** JDWP Tag. */
37     public static final byte tag = JdwpID.CLASS_LOADER_TAG;
38
39     /**
40      * Creates new ClassLoaderReferenceImpl.
41      */

42     public ClassLoaderReferenceImpl(VirtualMachineImpl vmImpl, JdwpClassLoaderID classLoaderID) {
43         super("ClassLoaderReference", vmImpl, classLoaderID); //$NON-NLS-1$
44
}
45
46     /**
47      * @returns Value tag.
48      */

49     public byte getTag() {
50         return tag;
51     }
52     
53     /**
54      * @returns Returns a list of all loaded classes that were defined by this class loader.
55      */

56     public List JavaDoc definedClasses() {
57         // Note that this information should not be cached.
58
List JavaDoc visibleClasses= visibleClasses();
59         List JavaDoc result = new ArrayList JavaDoc(visibleClasses.size());
60         Iterator JavaDoc iter = visibleClasses.iterator();
61         while (iter.hasNext()) {
62             try {
63                 ReferenceType type = (ReferenceType)iter.next();
64                 // Note that classLoader() is null for the bootstrap classloader.
65
if (type.classLoader() != null && type.classLoader().equals(this))
66                     result.add(type);
67             } catch (ClassNotPreparedException e) {
68                 continue;
69             }
70         }
71         return result;
72     }
73
74     /**
75      * @returns Returns a list of all loaded classes that are visible by this class loader.
76      */

77     public List JavaDoc visibleClasses() {
78         // Note that this information should not be cached.
79
initJdwpRequest();
80         try {
81             JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.CLR_VISIBLE_CLASSES, this);
82             defaultReplyErrorHandler(replyPacket.errorCode());
83             DataInputStream JavaDoc replyData = replyPacket.dataInStream();
84             int nrOfElements = readInt("elements", replyData); //$NON-NLS-1$
85
List JavaDoc elements = new ArrayList JavaDoc(nrOfElements);
86             for (int i = 0; i < nrOfElements; i++) {
87                 ReferenceTypeImpl elt = ReferenceTypeImpl.readWithTypeTag(this, replyData);
88                 if (elt == null)
89                     continue;
90                 elements.add(elt);
91             }
92             return elements;
93         } catch (IOException JavaDoc e) {
94             defaultIOExceptionHandler(e);
95             return null;
96         } finally {
97             handledJdwpRequest();
98         }
99     }
100     
101     /**
102      * @return Reads JDWP representation and returns new instance.
103      */

104     public static ClassLoaderReferenceImpl read(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
105         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
106         JdwpClassLoaderID ID = new JdwpClassLoaderID(vmImpl);
107         ID.read(in);
108         if (target.fVerboseWriter != null)
109             target.fVerboseWriter.println("classLoaderReference", ID.value()); //$NON-NLS-1$
110

111         if (ID.isNull())
112             return null;
113
114         ClassLoaderReferenceImpl mirror = new ClassLoaderReferenceImpl(vmImpl, ID);
115         return mirror;
116     }
117 }
118
Popular Tags