1 11 package org.eclipse.jdt.internal.debug.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.debug.core.DebugException; 23 import org.eclipse.debug.core.ILaunch; 24 import org.eclipse.debug.core.model.ISourceLocator; 25 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; 26 import org.eclipse.jdt.core.IClassFile; 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IType; 31 import org.eclipse.jdt.core.JavaCore; 32 import org.eclipse.jdt.debug.core.IJavaReferenceType; 33 import org.eclipse.jdt.debug.core.IJavaStackFrame; 34 import org.eclipse.jdt.debug.core.IJavaThread; 35 import org.eclipse.jdt.debug.core.IJavaType; 36 import org.eclipse.jdt.debug.core.IJavaValue; 37 38 import com.sun.jdi.VMDisconnectedException; 39 40 45 public class JavaDebugUtils { 46 47 56 public static IType resolveDeclaringType(IJavaStackFrame frame) throws CoreException { 57 IJavaElement javaElement = resolveJavaElement(frame, frame.getLaunch()); 58 if (javaElement != null) { 59 return resolveType(frame.getDeclaringTypeName(), javaElement); 60 } 61 return null; 62 } 63 64 72 public static IType resolveType(IJavaValue value) throws CoreException { 73 IJavaElement javaElement = resolveJavaElement(value, value.getLaunch()); 74 if (javaElement != null) { 75 return resolveType(value.getJavaType().getName(), javaElement); 76 } 77 return null; 78 } 79 80 88 public static IType resolveType(IJavaType type) throws CoreException { 89 IJavaElement element = resolveJavaElement(type, type.getLaunch()); 90 if (element != null ) { 91 return resolveType(type.getName(), element); 92 } 93 return null; 94 } 95 96 106 public static String getSourceName(Object object) throws CoreException { 107 if (object instanceof String ) { 108 return (String )object; 110 } 111 IJavaStackFrame frame = null; 112 if (object instanceof IAdaptable) { 113 frame = (IJavaStackFrame) ((IAdaptable)object).getAdapter(IJavaStackFrame.class); 114 } 115 String typeName = null; 116 try { 117 if (frame != null) { 118 if (frame.isObsolete()) { 119 return null; 120 } 121 String sourceName = frame.getSourcePath(); 122 if (sourceName == null) { 124 typeName = frame.getDeclaringTypeName(); 126 } else { 127 return sourceName; 128 } 129 } else { 130 if (object instanceof IJavaValue) { 131 object = ((IJavaValue)object).getJavaType(); 133 } 134 if (object instanceof IJavaReferenceType) { 135 IJavaReferenceType refType = (IJavaReferenceType)object; 136 String [] sourcePaths = refType.getSourcePaths(null); 137 if (sourcePaths != null && sourcePaths.length > 0) { 138 return sourcePaths[0]; 139 } 140 } 141 if (object instanceof IJavaType) { 142 typeName = ((IJavaType)object).getName(); 143 } 144 } 145 } catch (DebugException e) { 146 int code = e.getStatus().getCode(); 147 if (code == IJavaThread.ERR_THREAD_NOT_SUSPENDED || code == IJavaStackFrame.ERR_INVALID_STACK_FRAME || 148 e.getStatus().getException() instanceof VMDisconnectedException) { 149 return null; 150 } 151 throw e; 152 } 153 if (typeName != null) { 154 return generateSourceName(typeName); 155 } 156 return null; 157 } 158 159 168 public static String generateSourceName(String qualifiedTypeName) { 169 int index = qualifiedTypeName.lastIndexOf('.'); 170 if (index < 0) { 171 index = 0; 172 } 173 qualifiedTypeName = qualifiedTypeName.replace('.', File.separatorChar); 174 index = qualifiedTypeName.indexOf('$'); 175 if (index >= 0) { 176 qualifiedTypeName = qualifiedTypeName.substring(0, index); 177 } 178 if (qualifiedTypeName.length() == 0) { 179 qualifiedTypeName = null; 181 } else { 182 qualifiedTypeName = qualifiedTypeName + ".java"; } 184 return qualifiedTypeName; 185 } 186 187 196 private static IType resolveType(String qualifiedName, IJavaElement javaElement) throws CoreException { 197 IType type = null; 198 String [] typeNames = getNestedTypeNames(qualifiedName); 199 if (javaElement instanceof IClassFile) { 200 type = ((IClassFile)javaElement).getType(); 201 } else if (javaElement instanceof ICompilationUnit) { 202 type = ((ICompilationUnit)javaElement).getType(typeNames[0]); 203 } else if (javaElement instanceof IType) { 204 type = (IType)javaElement; 205 } 206 if (type != null) { 207 for (int i = 1; i < typeNames.length; i++) { 208 String innerTypeName= typeNames[i]; 209 try { 210 Integer.parseInt(innerTypeName); 211 return type; 212 } catch (NumberFormatException e) { 213 } 214 type = type.getType(innerTypeName); 215 } 216 } 217 return type; 218 } 219 220 229 private static IJavaElement resolveJavaElement(Object object, ILaunch launch) throws CoreException { 230 Object sourceElement = resolveSourceElement(object, launch); 231 IJavaElement javaElement = null; 232 if (sourceElement instanceof IJavaElement) { 233 javaElement = (IJavaElement) sourceElement; 234 } else if (sourceElement instanceof IAdaptable) { 235 javaElement = (IJavaElement) ((IAdaptable)sourceElement).getAdapter(IJavaElement.class); 236 } 237 if (javaElement == null && sourceElement instanceof IResource) { 238 javaElement= JavaCore.create((IResource)sourceElement); 239 } 240 if (javaElement == null) { 241 return null; 242 } 243 if (!javaElement.exists()) { 244 return null; 245 } 246 return javaElement; 247 } 248 249 258 public static Object resolveSourceElement(Object object, ILaunch launch) throws CoreException { 259 ISourceLocator sourceLocator = launch.getSourceLocator(); 260 if (sourceLocator instanceof ISourceLookupDirector) { 261 ISourceLookupDirector director = (ISourceLookupDirector) sourceLocator; 262 Object [] objects = director.findSourceElements(object); 263 if (objects.length > 0) { 264 return objects[0]; 265 } 266 } 267 return null; 268 } 269 270 279 private static String [] getNestedTypeNames(String typeName) { 280 int index = typeName.lastIndexOf('.'); 281 if (index >= 0) { 282 typeName= typeName.substring(index + 1); 283 } 284 index = typeName.indexOf('$'); 285 List list = new ArrayList (1); 286 while (index >= 0) { 287 list.add(typeName.substring(0, index)); 288 typeName = typeName.substring(index + 1); 289 index = typeName.indexOf('$'); 290 } 291 list.add(typeName); 292 return (String [])list.toArray(new String [list.size()]); 293 } 294 295 303 public static IJavaElement findElement(String qualifiedTypeName, IJavaProject project) throws CoreException{ 304 String [] javaLikeExtensions = JavaCore.getJavaLikeExtensions(); 305 String path = qualifiedTypeName; 306 int pos = path.indexOf('$'); 307 if (pos != -1) { 308 path= path.substring(0, pos); 309 } 310 path = path.replace('.', IPath.SEPARATOR); 311 path += "."; for (int i = 0; i < javaLikeExtensions.length; i++) { 313 String ext = javaLikeExtensions[i]; 314 IJavaElement element = project.findElement(new Path(path + ext)); 315 if (element != null) { 316 return element; 317 } 318 } 319 return null; 320 } 321 } 322 | Popular Tags |