KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIArrayValue


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.debug.core.model;
12
13  
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.model.IIndexedValue;
20 import org.eclipse.debug.core.model.IVariable;
21 import org.eclipse.jdt.debug.core.IJavaArray;
22 import org.eclipse.jdt.debug.core.IJavaValue;
23
24 import com.sun.jdi.ArrayReference;
25 import com.sun.jdi.ClassNotLoadedException;
26 import com.sun.jdi.InvalidTypeException;
27 import com.sun.jdi.Value;
28
29 public class JDIArrayValue extends JDIObjectValue implements IJavaArray, IIndexedValue{
30     
31     private int fLength = -1;
32
33     /**
34      * Constructs a value which is a reference to an array.
35      * @param target debug target on which the array exists
36      * @param value the reference to the array
37      */

38     public JDIArrayValue(JDIDebugTarget target, ArrayReference value) {
39         super(target, value);
40     }
41
42     /**
43      * @see IJavaArray#getValues()
44      */

45     public IJavaValue[] getValues() throws DebugException {
46         List JavaDoc list = getUnderlyingValues();
47
48         int count = list.size();
49         IJavaValue[] values = new IJavaValue[count];
50         JDIDebugTarget target = (JDIDebugTarget) getDebugTarget();
51         for (int i = 0; i < count; i++) {
52             Value value = (Value)list.get(i);
53             values[i] = JDIValue.createValue(target, value);
54         }
55         return values;
56     }
57
58     /**
59      * @see IJavaArray#getValue(int)
60      */

61     public IJavaValue getValue(int index) throws DebugException {
62         Value v = getUnderlyingValue(index);
63         return JDIValue.createValue((JDIDebugTarget)getDebugTarget(), v);
64     }
65
66     /**
67      * @see IJavaArray#getLength()
68      */

69     public synchronized int getLength() throws DebugException {
70         if (fLength == -1) {
71             try {
72                 fLength = getArrayReference().length();
73             } catch (RuntimeException JavaDoc e) {
74                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_array_length, new String JavaDoc[] {e.toString()}), e);
75             }
76         }
77         return fLength;
78     }
79
80     /**
81      * @see IJavaArray#setValue(int, IJavaValue)
82      */

83     public void setValue(int index, IJavaValue value) throws DebugException {
84         try {
85             getArrayReference().setValue(index, ((JDIValue)value).getUnderlyingValue());
86         } catch (IndexOutOfBoundsException JavaDoc e) {
87             throw e;
88         } catch (InvalidTypeException e) {
89             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String JavaDoc[] {e.toString()}), e);
90         } catch (ClassNotLoadedException e) {
91             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String JavaDoc[] {e.toString()}), e);
92         } catch (RuntimeException JavaDoc e) {
93             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String JavaDoc[] {e.toString()}), e);
94         }
95     }
96
97     /**
98      * Returns the underlying array reference for this
99      * array.
100      *
101      * @return underlying array reference
102      */

103     protected ArrayReference getArrayReference() {
104         return (ArrayReference)getUnderlyingValue();
105     }
106     
107     /**
108      * Returns the underlying value at the given index
109      * from the underlying array reference.
110      *
111      * @param index the index at which to retrieve a value
112      * @return value
113      * @exception DebugException if this method fails. Reasons include:<ul>
114      * <li>Failure communicating with the VM. The DebugException's
115      * status code contains the underlying exception responsible for
116      * the failure.</li>
117      * </ul>
118      */

119     protected Value getUnderlyingValue(int index) throws DebugException {
120         try {
121             return getArrayReference().getValue(index);
122         } catch (IndexOutOfBoundsException JavaDoc e) {
123             throw e;
124         } catch (RuntimeException JavaDoc e) {
125             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_value_from_array, new String JavaDoc[] {e.toString()}), e);
126         }
127         // execution will not reach this line as
128
// an exception will be thrown
129
return null;
130     }
131     
132     /**
133      * Returns the underlying values
134      * from the underlying array reference.
135      *
136      * @return list of values
137      * @exception DebugException if this method fails. Reasons include:<ul>
138      * <li>Failure communicating with the VM. The DebugException's
139      * status code contains the underlying exception responsible for
140      * the failure.</li>
141      * </ul>
142      */

143     protected List JavaDoc getUnderlyingValues() throws DebugException {
144         try {
145             return getArrayReference().getValues();
146         } catch (IndexOutOfBoundsException JavaDoc e) {
147             return Collections.EMPTY_LIST;
148         } catch (RuntimeException JavaDoc e) {
149             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_values_from_array, new String JavaDoc[] {e.toString()}), e);
150         }
151         // execution will not reach this line as
152
// an exception will be thrown
153
return null;
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.debug.core.model.IIndexedValue#getSize()
158      */

159     public int getSize() throws DebugException {
160         return getLength();
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.debug.core.model.IIndexedValue#getVariable(int)
165      */

166     public IVariable getVariable(int offset) throws DebugException {
167         if (offset >= getLength()) {
168             requestFailed(JDIDebugModelMessages.JDIArrayValue_6, null);
169         }
170         return new JDIArrayEntryVariable(getJavaDebugTarget(), getArrayReference(), offset);
171     }
172
173     /* (non-Javadoc)
174      * @see org.eclipse.debug.core.model.IIndexedValue#getVariables(int, int)
175      */

176     public IVariable[] getVariables(int offset, int length) throws DebugException {
177         if (offset >= getLength()) {
178             requestFailed(JDIDebugModelMessages.JDIArrayValue_6, null);
179         }
180         if ((offset + length - 1) >= getLength()) {
181             requestFailed(JDIDebugModelMessages.JDIArrayValue_8, null);
182         }
183         IVariable[] variables = new IVariable[length];
184         int index = offset;
185         for (int i = 0; i < length; i++) {
186             variables[i] = new JDIArrayEntryVariable(getJavaDebugTarget(), getArrayReference(), index);
187             index++;
188         }
189         return variables;
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.debug.core.model.IIndexedValue#getInitialOffset()
194      */

195     public int getInitialOffset() {
196         return 0;
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.debug.core.model.IValue#hasVariables()
201      */

202     public boolean hasVariables() throws DebugException {
203         return getLength() > 0;
204     }
205
206 }
207
208
Popular Tags