001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal;
011:
012: import java.io.DataInputStream;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.jdi.internal.jdwp.JdwpClassLoaderID;
019: import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
020: import org.eclipse.jdi.internal.jdwp.JdwpID;
021: import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket;
022:
023: import com.sun.jdi.ClassLoaderReference;
024: import com.sun.jdi.ClassNotPreparedException;
025: import com.sun.jdi.ReferenceType;
026:
027: /**
028: * this class implements the corresponding interfaces
029: * declared by the JDI specification. See the com.sun.jdi package
030: * for more information.
031: *
032: */
033: public class ClassLoaderReferenceImpl extends ObjectReferenceImpl
034: implements ClassLoaderReference {
035: /** JDWP Tag. */
036: public static final byte tag = JdwpID.CLASS_LOADER_TAG;
037:
038: /**
039: * Creates new ClassLoaderReferenceImpl.
040: */
041: public ClassLoaderReferenceImpl(VirtualMachineImpl vmImpl,
042: JdwpClassLoaderID classLoaderID) {
043: super ("ClassLoaderReference", vmImpl, classLoaderID); //$NON-NLS-1$
044: }
045:
046: /**
047: * @returns Value tag.
048: */
049: public byte getTag() {
050: return tag;
051: }
052:
053: /**
054: * @returns Returns a list of all loaded classes that were defined by this class loader.
055: */
056: public List definedClasses() {
057: // Note that this information should not be cached.
058: List visibleClasses = visibleClasses();
059: List result = new ArrayList(visibleClasses.size());
060: Iterator iter = visibleClasses.iterator();
061: while (iter.hasNext()) {
062: try {
063: ReferenceType type = (ReferenceType) iter.next();
064: // Note that classLoader() is null for the bootstrap classloader.
065: if (type.classLoader() != null
066: && type.classLoader().equals(this ))
067: result.add(type);
068: } catch (ClassNotPreparedException e) {
069: continue;
070: }
071: }
072: return result;
073: }
074:
075: /**
076: * @returns Returns a list of all loaded classes that are visible by this class loader.
077: */
078: public List visibleClasses() {
079: // Note that this information should not be cached.
080: initJdwpRequest();
081: try {
082: JdwpReplyPacket replyPacket = requestVM(
083: JdwpCommandPacket.CLR_VISIBLE_CLASSES, this );
084: defaultReplyErrorHandler(replyPacket.errorCode());
085: DataInputStream replyData = replyPacket.dataInStream();
086: int nrOfElements = readInt("elements", replyData); //$NON-NLS-1$
087: List elements = new ArrayList(nrOfElements);
088: for (int i = 0; i < nrOfElements; i++) {
089: ReferenceTypeImpl elt = ReferenceTypeImpl
090: .readWithTypeTag(this , replyData);
091: if (elt == null)
092: continue;
093: elements.add(elt);
094: }
095: return elements;
096: } catch (IOException e) {
097: defaultIOExceptionHandler(e);
098: return null;
099: } finally {
100: handledJdwpRequest();
101: }
102: }
103:
104: /**
105: * @return Reads JDWP representation and returns new instance.
106: */
107: public static ClassLoaderReferenceImpl read(MirrorImpl target,
108: DataInputStream in) throws IOException {
109: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
110: JdwpClassLoaderID ID = new JdwpClassLoaderID(vmImpl);
111: ID.read(in);
112: if (target.fVerboseWriter != null)
113: target.fVerboseWriter.println(
114: "classLoaderReference", ID.value()); //$NON-NLS-1$
115:
116: if (ID.isNull())
117: return null;
118:
119: ClassLoaderReferenceImpl mirror = new ClassLoaderReferenceImpl(
120: vmImpl, ID);
121: return mirror;
122: }
123: }
|