1 11 package org.eclipse.jdt.internal.debug.core.model; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 import java.util.Collections ; 16 import java.util.List ; 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 38 public JDIArrayValue(JDIDebugTarget target, ArrayReference value) { 39 super(target, value); 40 } 41 42 45 public IJavaValue[] getValues() throws DebugException { 46 List 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 61 public IJavaValue getValue(int index) throws DebugException { 62 Value v = getUnderlyingValue(index); 63 return JDIValue.createValue((JDIDebugTarget)getDebugTarget(), v); 64 } 65 66 69 public synchronized int getLength() throws DebugException { 70 if (fLength == -1) { 71 try { 72 fLength = getArrayReference().length(); 73 } catch (RuntimeException e) { 74 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_array_length, new String [] {e.toString()}), e); 75 } 76 } 77 return fLength; 78 } 79 80 83 public void setValue(int index, IJavaValue value) throws DebugException { 84 try { 85 getArrayReference().setValue(index, ((JDIValue)value).getUnderlyingValue()); 86 } catch (IndexOutOfBoundsException e) { 87 throw e; 88 } catch (InvalidTypeException e) { 89 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String [] {e.toString()}), e); 90 } catch (ClassNotLoadedException e) { 91 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String [] {e.toString()}), e); 92 } catch (RuntimeException e) { 93 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_setting_value_in_array, new String [] {e.toString()}), e); 94 } 95 } 96 97 103 protected ArrayReference getArrayReference() { 104 return (ArrayReference)getUnderlyingValue(); 105 } 106 107 119 protected Value getUnderlyingValue(int index) throws DebugException { 120 try { 121 return getArrayReference().getValue(index); 122 } catch (IndexOutOfBoundsException e) { 123 throw e; 124 } catch (RuntimeException e) { 125 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_value_from_array, new String [] {e.toString()}), e); 126 } 127 return null; 130 } 131 132 143 protected List getUnderlyingValues() throws DebugException { 144 try { 145 return getArrayReference().getValues(); 146 } catch (IndexOutOfBoundsException e) { 147 return Collections.EMPTY_LIST; 148 } catch (RuntimeException e) { 149 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIArrayValue_exception_while_retrieving_values_from_array, new String [] {e.toString()}), e); 150 } 151 return null; 154 } 155 156 159 public int getSize() throws DebugException { 160 return getLength(); 161 } 162 163 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 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 195 public int getInitialOffset() { 196 return 0; 197 } 198 199 202 public boolean hasVariables() throws DebugException { 203 return getLength() > 0; 204 } 205 206 } 207 208 | Popular Tags |