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.ByteArrayOutputStream;
013: import java.io.DataInputStream;
014: import java.io.DataOutputStream;
015: import java.io.IOException;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.eclipse.jdi.internal.jdwp.JdwpArrayID;
022: import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket;
023: import org.eclipse.jdi.internal.jdwp.JdwpID;
024: import org.eclipse.jdi.internal.jdwp.JdwpObjectID;
025: import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket;
026:
027: import com.sun.jdi.AbsentInformationException;
028: import com.sun.jdi.ArrayReference;
029: import com.sun.jdi.ArrayType;
030: import com.sun.jdi.ClassNotLoadedException;
031: import com.sun.jdi.Field;
032: import com.sun.jdi.Type;
033: import com.sun.jdi.Value;
034:
035: /**
036: * this class implements the corresponding interfaces
037: * declared by the JDI specification. See the com.sun.jdi package
038: * for more information.
039: *
040: */
041: public class ArrayTypeImpl extends ReferenceTypeImpl implements
042: ArrayType {
043: /** JDWP Tag. */
044: public static final byte typeTag = JdwpID.TYPE_TAG_ARRAY;
045: /** component type */
046: private Type fComponentType;
047: /** Component type name */
048: private String fComponentTypeName;
049:
050: /**
051: * Creates new ArrayTypeImpl.
052: */
053: public ArrayTypeImpl(VirtualMachineImpl vmImpl, JdwpArrayID arrayID) {
054: super ("ArrayType", vmImpl, arrayID); //$NON-NLS-1$
055: }
056:
057: /**
058: * Creates new ArrayTypeImpl.
059: */
060: public ArrayTypeImpl(VirtualMachineImpl vmImpl,
061: JdwpArrayID arrayID, String signature,
062: String genericSignature) {
063: super ("ArrayType", vmImpl, arrayID, signature, genericSignature); //$NON-NLS-1$
064: }
065:
066: /**
067: * @return Returns type tag.
068: */
069: public byte typeTag() {
070: return typeTag;
071: }
072:
073: /**
074: * @return Create a null value instance of the type.
075: */
076: public Value createNullValue() {
077: return new ArrayReferenceImpl(virtualMachineImpl(),
078: new JdwpObjectID(virtualMachineImpl()));
079: }
080:
081: /**
082: * @return Returns the JNI signature of the components of this array class.
083: */
084: public String componentSignature() {
085: return signature().substring(1);
086: }
087:
088: /**
089: * @return Returns the type of the array components.
090: */
091: public Type componentType() throws ClassNotLoadedException {
092: if (fComponentType == null) {
093: fComponentType = TypeImpl.create(virtualMachineImpl(),
094: componentSignature(), classLoader());
095: }
096: return fComponentType;
097: }
098:
099: /**
100: * @return Returns a text representation of the component type.
101: */
102: public String componentTypeName() {
103: if (fComponentTypeName == null) {
104: fComponentTypeName = signatureToName(componentSignature());
105: }
106: return fComponentTypeName;
107: }
108:
109: /**
110: * @return Creates and returns a new instance of this array class in the target VM.
111: */
112: public ArrayReference newInstance(int length) {
113: // Note that this information should not be cached.
114: initJdwpRequest();
115: try {
116: ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
117: DataOutputStream outData = new DataOutputStream(outBytes);
118: write(this , outData);
119: writeInt(length, "length", outData); //$NON-NLS-1$
120:
121: JdwpReplyPacket replyPacket = requestVM(
122: JdwpCommandPacket.AT_NEW_INSTANCE, outBytes);
123: defaultReplyErrorHandler(replyPacket.errorCode());
124:
125: DataInputStream replyData = replyPacket.dataInStream();
126: ArrayReferenceImpl arrayRef = (ArrayReferenceImpl) ObjectReferenceImpl
127: .readObjectRefWithTag(this , replyData);
128: return arrayRef;
129: } catch (IOException e) {
130: defaultIOExceptionHandler(e);
131: return null;
132: } finally {
133: handledJdwpRequest();
134: }
135: }
136:
137: /**
138: * @return Returns a List filled with all Location objects that map to the given line number.
139: */
140: public List locationsOfLine(int line) {
141: // If this reference type is an ArrayType, the returned list is always empty.
142: return Collections.EMPTY_LIST;
143: }
144:
145: /**
146: * @return Reads JDWP representation and returns new instance.
147: */
148: public static ArrayTypeImpl read(MirrorImpl target,
149: DataInputStream in) throws IOException {
150: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
151: JdwpArrayID ID = new JdwpArrayID(vmImpl);
152: ID.read(in);
153: if (target.fVerboseWriter != null)
154: target.fVerboseWriter.println("arrayType", ID.value()); //$NON-NLS-1$
155:
156: if (ID.isNull())
157: return null;
158:
159: ArrayTypeImpl mirror = (ArrayTypeImpl) vmImpl
160: .getCachedMirror(ID);
161: if (mirror == null) {
162: mirror = new ArrayTypeImpl(vmImpl, ID);
163: vmImpl.addCachedMirror(mirror);
164: }
165: return mirror;
166: }
167:
168: /**
169: * @return Returns modifier bits.
170: */
171: public int modifiers() {
172: return MODIFIER_ACC_PUBLIC | MODIFIER_ACC_FINAL;
173: }
174:
175: /**
176: * @return Returns a list containing each Field declared in this type.
177: */
178: public List fields() {
179: return Collections.EMPTY_LIST;
180: }
181:
182: /**
183: * @return Returns a list containing each Method declared in this type.
184: */
185: public List methods() {
186: return Collections.EMPTY_LIST;
187: }
188:
189: /**
190: * @return a Map of the requested static Field objects with their Value.
191: */
192: public Map getValues(List fields) {
193: if (fields.isEmpty()) {
194: return new HashMap();
195: }
196:
197: throw new IllegalArgumentException(
198: JDIMessages.ArrayTypeImpl_getValues_not_allowed_on_array_1);
199: }
200:
201: /**
202: * @return Returns a List containing each ReferenceType declared within this type.
203: */
204: public List nestedTypes() {
205: return Collections.EMPTY_LIST;
206: }
207:
208: /**
209: * @return Returns status of class/interface.
210: */
211: protected int status() {
212: return ReferenceTypeImpl.JDWP_CLASS_STATUS_INITIALIZED
213: | ReferenceTypeImpl.JDWP_CLASS_STATUS_PREPARED
214: | ReferenceTypeImpl.JDWP_CLASS_STATUS_VERIFIED;
215: }
216:
217: /**
218: * @return Reads JDWP representation and returns new instance.
219: */
220: public static ArrayTypeImpl readWithSignature(MirrorImpl target,
221: boolean withGenericSignature, DataInputStream in)
222: throws IOException {
223: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
224: JdwpArrayID ID = new JdwpArrayID(vmImpl);
225: ID.read(in);
226: if (target.fVerboseWriter != null)
227: target.fVerboseWriter.println("arrayType", ID.value()); //$NON-NLS-1$
228:
229: String signature = target.readString("signature", in); //$NON-NLS-1$
230: String genericSignature = null;
231: if (withGenericSignature) {
232: genericSignature = target.readString(
233: "generic signature", in); //$NON-NLS-1$
234: }
235: if (ID.isNull())
236: return null;
237:
238: ArrayTypeImpl mirror = (ArrayTypeImpl) vmImpl
239: .getCachedMirror(ID);
240: if (mirror == null) {
241: mirror = new ArrayTypeImpl(vmImpl, ID);
242: vmImpl.addCachedMirror(mirror);
243: }
244: mirror.setSignature(signature);
245: mirror.setGenericSignature(genericSignature);
246: return mirror;
247: }
248:
249: /**
250: * @see com.sun.jdi.ReferenceType#allLineLocations()
251: */
252: public List allLineLocations() {
253: // If this reference type is an ArrayType, the returned list is always empty.
254: return Collections.EMPTY_LIST;
255: }
256:
257: /**
258: * @see com.sun.jdi.ReferenceType#allMethods()
259: */
260: public List allMethods() {
261: return Collections.EMPTY_LIST;
262: }
263:
264: /**
265: * @see com.sun.jdi.ReferenceType#allFields()
266: */
267: public List allFields() {
268: return Collections.EMPTY_LIST;
269: }
270:
271: /**
272: * @return Returns an identifying name for the source corresponding to the declaration of this type.
273: */
274: public String sourceName() throws AbsentInformationException {
275: throw new AbsentInformationException(
276: JDIMessages.ArrayTypeImpl_No_source_name_for_Arrays_1);
277: }
278:
279: /**
280: * @see com.sun.jdi.ReferenceType#visibleFields()
281: */
282: public List visibleFields() {
283: return Collections.EMPTY_LIST;
284: }
285:
286: /**
287: * @see com.sun.jdi.ReferenceType#visibleMethods()
288: */
289: public List visibleMethods() {
290: return Collections.EMPTY_LIST;
291: }
292:
293: /**
294: * @see com.sun.jdi.ReferenceType#fieldByName(String)
295: */
296: public Field fieldByName(String arg1) {
297: return null;
298: }
299:
300: /**
301: * @see com.sun.jdi.ReferenceType#methodsByName(String)
302: */
303: public List methodsByName(String arg1) {
304: return Collections.EMPTY_LIST;
305: }
306:
307: /**
308: * @see com.sun.jdi.ReferenceType#methodsByName(String, String)
309: */
310: public List methodsByName(String arg1, String arg2) {
311: return Collections.EMPTY_LIST;
312: }
313: }
|