KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.IValue;
19
20 import com.sun.jdi.ArrayReference;
21 import com.sun.jdi.ArrayType;
22 import com.sun.jdi.ClassNotLoadedException;
23 import com.sun.jdi.InvalidTypeException;
24 import com.sun.jdi.ReferenceType;
25 import com.sun.jdi.Type;
26 import com.sun.jdi.Value;
27
28 /**
29  * An entry in an array.
30  */

31
32 public class JDIArrayEntryVariable extends JDIModificationVariable {
33         
34     /**
35      * The index of the variable entry
36      */

37     private int fIndex;
38     
39     /**
40      * The array object
41      */

42     private ArrayReference fArray;
43     
44     /**
45      * The reference type name of this variable. Cached lazily.
46      */

47     private String JavaDoc fReferenceTypeName= null;
48     
49     /**
50      * Constructs an array entry at the given index in an array.
51      */

52     public JDIArrayEntryVariable(JDIDebugTarget target, ArrayReference array, int index) {
53         super(target);
54         fArray= array;
55         fIndex= index;
56     }
57
58     /**
59      * Returns this variable's current underlying value.
60      */

61     protected Value retrieveValue() {
62         ArrayReference ar= getArrayReference();
63         if (ar != null) {
64             return ar.getValue(getIndex());
65         }
66         return null;
67     }
68
69     /**
70      * @see IVariable#getName()
71      */

72     public String JavaDoc getName() {
73         return "[" + getIndex() + "]"; //$NON-NLS-2$ //$NON-NLS-1$
74
}
75
76     protected void setJDIValue(Value value) throws DebugException {
77         ArrayReference ar= getArrayReference();
78         if (ar == null) {
79             requestFailed(JDIDebugModelMessages.JDIArrayEntryVariable_value_modification_failed, null);
80         }
81         try {
82             ar.setValue(getIndex(), value);
83             fireChangeEvent(DebugEvent.CONTENT);
84         } catch (ClassNotLoadedException e) {
85             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_modifying_variable_value, new String JavaDoc[] {e.toString()}), e);
86         } catch (InvalidTypeException e) {
87             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_modifying_variable_value, new String JavaDoc[] {e.toString()}), e);
88         } catch (RuntimeException JavaDoc e) {
89             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_modifying_variable_value, new String JavaDoc[] {e.toString()}), e);
90         }
91
92     }
93
94     protected ArrayReference getArrayReference() {
95         return fArray;
96     }
97     
98     protected int getIndex() {
99         return fIndex;
100     }
101     
102     /**
103      * @see IVariable#getReferenceTypeName()
104      */

105     public String JavaDoc getReferenceTypeName() throws DebugException {
106         try {
107             if (fReferenceTypeName == null) {
108                 fReferenceTypeName= stripBrackets(JDIReferenceType.getGenericName(getArrayReference().referenceType()));
109             }
110         } catch (RuntimeException JavaDoc e) {
111             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_retrieving_reference_type, new String JavaDoc[] {e.toString()}), e);
112             // execution will not reach this line, as
113
// #targetRequestFailed will thrown an exception
114
return null;
115         }
116         return fReferenceTypeName;
117     }
118     
119     /**
120      * Given a type name, strip out one set of array brackets and
121      * return the result. Example: "int[][][]" becomes "int[][]".
122      */

123     protected String JavaDoc stripBrackets(String JavaDoc typeName) {
124         int lastLeft= typeName.lastIndexOf("[]"); //$NON-NLS-1$
125
if (lastLeft < 0) {
126             return typeName;
127         }
128         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(typeName);
129         buffer.replace(lastLeft, lastLeft + 2, ""); //$NON-NLS-1$
130
return buffer.toString();
131     }
132     
133     /**
134      * @see IJavaVariable#getSignature()
135      */

136     public String JavaDoc getSignature() throws DebugException {
137         try {
138             return ((ArrayType) getArrayReference().type()).componentSignature();
139         } catch (RuntimeException JavaDoc e) {
140             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_retrieving_type_signature, new String JavaDoc[] {e.toString()}), e);
141             // execution will not reach this line, as
142
// #targetRequestFailed will thrown an exception
143
return null;
144         }
145     }
146     
147     /* (non-Javadoc)
148      * @see org.eclipse.jdt.debug.core.IJavaVariable#getGenericSignature()
149      */

150     public String JavaDoc getGenericSignature() throws DebugException {
151         try {
152             ReferenceType referenceType= getArrayReference().referenceType();
153             String JavaDoc genericSignature= referenceType.genericSignature();
154             if (genericSignature != null) {
155                 return genericSignature;
156             }
157             return referenceType.signature();
158         } catch (RuntimeException JavaDoc e) {
159             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_retrieving_type_signature, new String JavaDoc[] {e.toString()}), e);
160             // execution will not reach this line, as
161
// #targetRequestFailed will thrown an exception
162
return null;
163         }
164     }
165     
166     /**
167      * @see IValueModification#setValue(IValue)
168      */

169     public void setValue(IValue v) throws DebugException {
170         if (verifyValue(v)) {
171             JDIValue value = (JDIValue)v;
172             setJDIValue(value.getUnderlyingValue());
173         }
174     }
175     
176     /**
177      * @see JDIVariable#getUnderlyingType()
178      */

179     protected Type getUnderlyingType() throws DebugException {
180         try {
181             return ((ArrayType)getArrayReference().type()).componentType();
182         } catch (ClassNotLoadedException e) {
183             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_while_retrieving_type_of_array_entry, new String JavaDoc[]{e.toString()}), e);
184         } catch (RuntimeException JavaDoc e) {
185             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayEntryVariable_exception_while_retrieving_type_of_array_entry, new String JavaDoc[]{e.toString()}), e);
186         }
187         // this line will not be exceucted as an exception
188
// will be throw in type retrieval fails
189
return null;
190     }
191     /* (non-Javadoc)
192      * @see java.lang.Object#equals(java.lang.Object)
193      */

194     public boolean equals(Object JavaDoc obj) {
195         if (obj instanceof JDIArrayEntryVariable) {
196             JDIArrayEntryVariable entry = (JDIArrayEntryVariable)obj;
197             return entry.getArrayReference().equals(getArrayReference()) &&
198                 entry.getIndex() == getIndex();
199         }
200         return false;
201     }
202
203     /* (non-Javadoc)
204      * @see java.lang.Object#hashCode()
205      */

206     public int hashCode() {
207         return getArrayReference().hashCode() + getIndex();
208     }
209
210 }
211
212
Popular Tags