1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 14 import java.util.Map ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.debug.core.model.IBreakpoint; 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.IField; 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IMember; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.IType; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.debug.core.IJavaBreakpoint; 29 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint; 30 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint; 31 import org.eclipse.jdt.debug.core.IJavaWatchpoint; 32 33 36 public class BreakpointUtils { 37 38 42 private static final String HANDLE_ID = JDIDebugUIPlugin.getUniqueIdentifier() + ".JAVA_ELEMENT_HANDLE_ID"; 44 47 private static final String RUN_TO_LINE = JDIDebugUIPlugin.getUniqueIdentifier() + ".run_to_line"; 49 53 private static final String MEMBER_START = JDIDebugUIPlugin.getUniqueIdentifier() + ".member_start"; 55 59 private static final String MEMBER_END = JDIDebugUIPlugin.getUniqueIdentifier() + ".member_end"; 61 71 public static IResource getBreakpointResource(IMember member) { 72 ICompilationUnit cu = member.getCompilationUnit(); 73 if (cu != null && cu.isWorkingCopy()) { 74 member = (IMember)member.getPrimaryElement(); 75 } 76 IResource res = member.getResource(); 77 if (res == null) { 78 res = ResourcesPlugin.getWorkspace().getRoot(); 79 } 80 else if(!res.getProject().exists()) { 81 res = ResourcesPlugin.getWorkspace().getRoot(); 82 } 83 return res; 84 } 85 86 92 public static IType getType(IJavaBreakpoint breakpoint) { 93 String handle = breakpoint.getMarker().getAttribute(HANDLE_ID, null); 94 if (handle != null) { 95 IJavaElement je = JavaCore.create(handle); 96 if (je != null) { 97 if (je instanceof IType) { 98 return (IType)je; 99 } 100 if (je instanceof IMember) { 101 return ((IMember)je).getDeclaringType(); 102 } 103 } 104 } 105 return null; 106 } 107 108 118 public static IMember getMember(IJavaLineBreakpoint breakpoint) throws CoreException { 119 if (breakpoint instanceof IJavaMethodBreakpoint) { 120 return getMethod((IJavaMethodBreakpoint)breakpoint); 121 } 122 if (breakpoint instanceof IJavaWatchpoint) { 123 return getField((IJavaWatchpoint)breakpoint); 124 } 125 126 int start = breakpoint.getCharStart(); 127 int end = breakpoint.getCharEnd(); 128 129 IType type = getType(breakpoint); 130 131 if (start == -1 && end == -1) { 132 start= breakpoint.getMarker().getAttribute(MEMBER_START, -1); 133 end= breakpoint.getMarker().getAttribute(MEMBER_END, -1); 134 } 135 136 IMember member = null; 137 if ((type != null && type.exists()) && (end >= start) && (start >= 0)) { 138 member= binSearch(type, start, end); 139 } 140 if (member == null) { 141 member= type; 142 } 143 return member; 144 } 145 146 150 protected static IMember binSearch(IType type, int start, int end) throws JavaModelException { 151 IJavaElement je = getElementAt(type, start); 152 if (je != null && !je.equals(type)) { 153 return asMember(je); 154 } 155 if (end > start) { 156 je = getElementAt(type, end); 157 if (je != null && !je.equals(type)) { 158 return asMember(je); 159 } 160 int mid = ((end - start) / 2) + start; 161 if (mid > start) { 162 je = binSearch(type, start + 1, mid); 163 if (je == null) { 164 je = binSearch(type, mid + 1, end - 1); 165 } 166 return asMember(je); 167 } 168 } 169 return null; 170 } 171 172 180 private static IMember asMember(IJavaElement element) { 181 if (element instanceof IMember) { 182 return (IMember)element; 183 } 184 return null; 185 } 186 187 190 protected static IJavaElement getElementAt(IType type, int pos) throws JavaModelException { 191 if (type.isBinary()) { 192 return type.getClassFile().getElementAt(pos); 193 } 194 return type.getCompilationUnit().getElementAt(pos); 195 } 196 197 208 public static void addJavaBreakpointAttributes(Map attributes, IJavaElement element) { 209 String handleId = element.getHandleIdentifier(); 210 attributes.put(HANDLE_ID, handleId); 211 JavaCore.addJavaElementMarkerAttributes(attributes, element); 212 } 213 214 229 public static void addJavaBreakpointAttributesWithMemberDetails(Map attributes, IJavaElement element, int memberStart, int memberEnd) { 230 addJavaBreakpointAttributes(attributes, element); 231 attributes.put(MEMBER_START, new Integer (memberStart)); 232 attributes.put(MEMBER_END, new Integer (memberEnd)); 233 } 234 235 247 public static void addRunToLineAttributes(Map attributes) { 248 attributes.put(IBreakpoint.PERSISTED, Boolean.FALSE); 249 attributes.put(RUN_TO_LINE, Boolean.TRUE); 250 } 251 252 259 public static IMethod getMethod(IJavaMethodBreakpoint breakpoint) { 260 String handle = breakpoint.getMarker().getAttribute(HANDLE_ID, null); 261 if (handle != null) { 262 IJavaElement je = JavaCore.create(handle); 263 if (je != null) { 264 if (je instanceof IMethod) { 265 return (IMethod)je; 266 } 267 } 268 } 269 return null; 270 } 271 272 278 public static IField getField(IJavaWatchpoint breakpoint) { 279 String handle = breakpoint.getMarker().getAttribute(HANDLE_ID, null); 280 if (handle != null) { 281 IJavaElement je = JavaCore.create(handle); 282 if (je != null) { 283 if (je instanceof IField) { 284 return (IField)je; 285 } 286 } 287 } 288 return null; 289 } 290 291 299 public static boolean isRunToLineBreakpoint(IJavaLineBreakpoint breakpoint) { 300 return breakpoint.getMarker().getAttribute(RUN_TO_LINE, false); 301 } 302 303 311 public static boolean isProblemBreakpoint(IBreakpoint breakpoint) { 312 return breakpoint == JavaDebugOptionsManager.getDefault().getSuspendOnCompilationErrorBreakpoint() || 313 breakpoint == JavaDebugOptionsManager.getDefault().getSuspendOnUncaughtExceptionBreakpoint(); 314 } 315 } 316 | Popular Tags |