1 11 package org.eclipse.jdt.internal.debug.core.model; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.debug.core.DebugException; 17 import org.eclipse.jdt.core.Signature; 18 import org.eclipse.jdt.debug.core.IJavaClassObject; 19 import org.eclipse.jdt.debug.core.IJavaFieldVariable; 20 import org.eclipse.jdt.debug.core.IJavaObject; 21 import org.eclipse.jdt.debug.core.IJavaReferenceType; 22 23 import com.ibm.icu.text.MessageFormat; 24 import com.sun.jdi.AbsentInformationException; 25 import com.sun.jdi.ArrayType; 26 import com.sun.jdi.ClassLoaderReference; 27 import com.sun.jdi.ClassNotLoadedException; 28 import com.sun.jdi.Field; 29 import com.sun.jdi.ReferenceType; 30 import com.sun.jdi.Type; 31 import com.sun.jdi.Value; 32 33 36 public abstract class JDIReferenceType extends JDIType implements IJavaReferenceType { 37 38 private String [] fDeclaredFields = null; 40 private String [] fAllFields = null; 42 43 49 public JDIReferenceType(JDIDebugTarget target, Type type) { 50 super(target, type); 51 } 52 53 56 public String [] getAvailableStrata() { 57 List strata = getReferenceType().availableStrata(); 58 return (String [])strata.toArray(new String [strata.size()]); 59 } 60 61 66 protected ReferenceType getReferenceType() { 67 return (ReferenceType)getUnderlyingType(); 68 } 69 70 71 74 public String getDefaultStratum() throws DebugException { 75 try { 76 return getReferenceType().defaultStratum(); 77 } catch (RuntimeException e) { 78 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_1, e); 79 } 80 return null; 82 } 83 84 87 public IJavaFieldVariable getField(String name) throws DebugException { 88 try { 89 ReferenceType type = (ReferenceType)getUnderlyingType(); 90 Field field = type.fieldByName(name); 91 if (field != null && field.isStatic()) { 92 return new JDIFieldVariable(getJavaDebugTarget(), field, type); 93 } 94 } catch (RuntimeException e) { 95 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_field, new String [] {e.toString(), name}), e); 96 } 97 return null; 99 } 100 101 104 public IJavaClassObject getClassObject() throws DebugException { 105 try { 106 ReferenceType type= (ReferenceType)getUnderlyingType(); 107 return (IJavaClassObject)JDIValue.createValue(getJavaDebugTarget(), type.classObject()); 108 } catch (RuntimeException e) { 109 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_class_object, new String [] {e.toString()}), e); 110 } 111 return null; 114 } 115 116 119 public String [] getAllFieldNames() throws DebugException { 120 if (fAllFields == null) { 121 try { 122 List fields = ((ReferenceType)getUnderlyingType()).allFields(); 123 fAllFields = new String [fields.size()]; 124 Iterator iterator = fields.iterator(); 125 int i = 0; 126 while (iterator.hasNext()) { 127 Field field = (Field)iterator.next(); 128 fAllFields[i] = field.name(); 129 i++; 130 } 131 } catch (RuntimeException e) { 132 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_2, e); 133 } 134 } 135 return fAllFields; 136 } 137 138 141 public String [] getDeclaredFieldNames() throws DebugException { 142 if (fDeclaredFields == null) { 143 try { 144 List fields = ((ReferenceType)getUnderlyingType()).fields(); 145 fDeclaredFields = new String [fields.size()]; 146 Iterator iterator = fields.iterator(); 147 int i = 0; 148 while (iterator.hasNext()) { 149 Field field = (Field)iterator.next(); 150 fDeclaredFields[i] = field.name(); 151 i++; 152 } 153 } catch (RuntimeException e) { 154 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_3, e); 155 } 156 } 157 return fDeclaredFields; 158 } 159 160 163 public String [] getSourcePaths(String stratum) throws DebugException { 164 try { 165 List sourcePaths= getReferenceType().sourcePaths(stratum); 166 return (String []) sourcePaths.toArray(new String [sourcePaths.size()]); 167 } catch (AbsentInformationException e) { 168 } catch (RuntimeException e) { 169 requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED); 170 } 171 return null; 172 } 173 174 177 public String getSourceName() throws DebugException { 178 try { 179 return getReferenceType().sourceName(); 180 } catch (AbsentInformationException e) { 181 } catch (RuntimeException e) { 182 requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED); 183 } 184 return null; 185 } 186 187 190 public String [] getSourceNames(String stratum) throws DebugException { 191 try { 192 List sourceNames= getReferenceType().sourceNames(stratum); 193 return (String []) sourceNames.toArray(new String [sourceNames.size()]); 194 } catch (AbsentInformationException e) { 195 } catch (RuntimeException e) { 196 requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED); 197 } 198 return null; 199 } 200 201 204 public IJavaObject getClassLoaderObject() throws DebugException { 205 try { 206 ReferenceType type= (ReferenceType)getUnderlyingType(); 207 ClassLoaderReference classLoader = type.classLoader(); 208 if (classLoader != null) { 209 return (IJavaObject)JDIValue.createValue(getJavaDebugTarget(), classLoader); 210 } 211 } catch (RuntimeException e) { 212 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIReferenceType_0, new String [] {e.toString()}), e); 213 } 214 return null; 215 } 216 217 218 static public String getGenericName(ReferenceType type) throws DebugException { 219 if (type instanceof ArrayType) { 220 try { 221 Type componentType; 222 componentType= ((ArrayType)type).componentType(); 223 if (componentType instanceof ReferenceType) { 224 return getGenericName((ReferenceType)componentType) + "[]"; } 226 return type.name(); 227 } catch (ClassNotLoadedException e) { 228 } 231 } 232 String signature= type.signature(); 233 StringBuffer res= new StringBuffer (getTypeName(signature)); 234 String genericSignature= type.genericSignature(); 235 if (genericSignature != null) { 236 String [] typeParameters= Signature.getTypeParameters(genericSignature); 237 if (typeParameters.length > 0) { 238 res.append('<').append(Signature.getTypeVariable(typeParameters[0])); 239 for (int i= 1; i < typeParameters.length; i++) { 240 res.append(',').append(Signature.getTypeVariable(typeParameters[i])); 241 } 242 res.append('>'); 243 } 244 } 245 return res.toString(); 246 } 247 248 252 static public String getTypeName(String genericTypeSignature) { 253 int arrayDimension= 0; 254 while (genericTypeSignature.charAt(arrayDimension) == '[') { 255 arrayDimension++; 256 } 257 int parameterStart= genericTypeSignature.indexOf('<'); 258 StringBuffer name= new StringBuffer (); 259 if (parameterStart < 0) { 260 name.append(genericTypeSignature.substring(arrayDimension + 1, genericTypeSignature.length() - 1).replace('/', '.')); 261 } else { 262 name.append(genericTypeSignature.substring(arrayDimension + 1, parameterStart).replace('/', '.')); 263 name.append(Signature.toString(genericTypeSignature).substring(parameterStart - 1 - arrayDimension).replace('/', '.')); 264 } 265 for (int i= 0; i < arrayDimension; i++) { 266 name.append("[]"); } 268 return name.toString(); 269 } 270 271 274 public String getGenericSignature() throws DebugException { 275 return getReferenceType().genericSignature(); 276 } 277 278 281 public IJavaObject[] getInstances(long max) throws DebugException { 282 try { 283 List list = getReferenceType().instances(max); 284 IJavaObject[] instances = new IJavaObject[list.size()]; 285 for (int i = 0; i < instances.length; i++) { 286 instances[i] = (IJavaObject) JDIValue.createValue(getJavaDebugTarget(), (Value) list.get(i)); 287 } 288 return instances; 289 } catch (RuntimeException e) { 290 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_5, e); 291 } 292 return null; 293 } 294 295 } 296 | Popular Tags |