KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > BreakpointUtils


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui;
12
13
14 import java.util.Map JavaDoc;
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 /**
34  * Utility class for Java breakpoints
35  */

36 public class BreakpointUtils {
37     
38     /**
39      * Marker attribute storing the handle id of the
40      * Java element associated with a Java breakpoint
41      */

42     private static final String JavaDoc HANDLE_ID = JDIDebugUIPlugin.getUniqueIdentifier() + ".JAVA_ELEMENT_HANDLE_ID"; //$NON-NLS-1$
43

44     /**
45      * Marker attribute used to denote a run to line breakpoint
46      */

47     private static final String JavaDoc RUN_TO_LINE = JDIDebugUIPlugin.getUniqueIdentifier() + ".run_to_line"; //$NON-NLS-1$
48

49     /**
50      * Marker attribute used to denote the start of the region within a Java
51      * member that the breakpoint is located within.
52      */

53     private static final String JavaDoc MEMBER_START = JDIDebugUIPlugin.getUniqueIdentifier() + ".member_start"; //$NON-NLS-1$
54

55     /**
56      * Marker attribute used to denote the end of the region within a Java
57      * member that the breakpoint is located within.
58      */

59     private static final String JavaDoc MEMBER_END = JDIDebugUIPlugin.getUniqueIdentifier() + ".member_end"; //$NON-NLS-1$
60

61     /**
62      * Returns the resource on which a breakpoint marker should
63      * be created for the given member. The resource returned is the
64      * associated file, or workspace root in the case of a binary in
65      * an external archive.
66      *
67      * @param member member in which a breakpoint is being created
68      * @return resource the resource on which a breakpoint marker
69      * should be created
70      */

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     /**
87      * Returns the type that the given Java breakpoint refers to
88      *
89      * @param breakpoint Java breakpoint
90      * @return the type the breakpoint is associated with
91      */

92     public static IType getType(IJavaBreakpoint breakpoint) {
93         String JavaDoc 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     /**
109      * Returns the member associated with the line number of
110      * the given breakpoint.
111      *
112      * @param breakpoint Java line breakpoint
113      * @return member at the given line number in the type
114      * associated with the breakpoint
115      * @exception CoreException if an exception occurs accessing
116      * the breakpoint
117      */

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     /**
147      * Searches the given source range of the container for a member that is
148      * not the same as the given type.
149      */

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     /**
173      * Returns the given Java element if it is an
174      * <code>IMember</code>, otherwise <code>null</code>.
175      *
176      * @param element Java element
177      * @return the given element if it is a type member,
178      * otherwise <code>null</code>
179      */

180     private static IMember asMember(IJavaElement element) {
181         if (element instanceof IMember) {
182             return (IMember)element;
183         }
184         return null;
185     }
186     
187     /**
188      * Returns the element at the given position in the given type
189      */

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     /**
198      * Adds attributes to the given attribute map:<ul>
199      * <li>Java element handle id</li>
200      * <li>Attributes defined by <code>JavaCore</code></li>
201      * </ul>
202      *
203      * @param attributes the attribute map to use
204      * @param element the Java element associated with the breakpoint
205      * @exception CoreException if an exception occurs configuring
206      * the marker
207      */

208     public static void addJavaBreakpointAttributes(Map JavaDoc attributes, IJavaElement element) {
209         String JavaDoc handleId = element.getHandleIdentifier();
210         attributes.put(HANDLE_ID, handleId);
211         JavaCore.addJavaElementMarkerAttributes(attributes, element);
212     }
213     
214     /**
215      * Adds attributes to the given attribute map:<ul>
216      * <li>Java element handle id</li>
217      * <li>Member start position</li>
218      * <li>Member end position</li>
219      * <li>Attributes defined by <code>JavaCore</code></li>
220      * </ul>
221      *
222      * @param attributes the attribute map to use
223      * @param element the Java element associated with the breakpoint
224      * @param memberStart the start position of the Java member that the breakpoint is positioned within
225      * @param memberEnd the end position of the Java member that the breakpoint is positioned within
226      * @exception CoreException if an exception occurs configuring
227      * the marker
228      */

229     public static void addJavaBreakpointAttributesWithMemberDetails(Map JavaDoc attributes, IJavaElement element, int memberStart, int memberEnd) {
230         addJavaBreakpointAttributes(attributes, element);
231         attributes.put(MEMBER_START, new Integer JavaDoc(memberStart));
232         attributes.put(MEMBER_END, new Integer JavaDoc(memberEnd));
233     }
234     
235     /**
236      * Adds attributes to the given attribute map to make the
237      * breakpoint a run-to-line breakpoint:<ul>
238      * <li>PERSISTED = false</li>
239      * <li>RUN_TO_LINE = true</li>
240      * </ul>
241      *
242      * @param attributes the attribute map to use
243      * @param element the Java element associated with the breakpoint
244      * @exception CoreException if an exception occurs configuring
245      * the marker
246      */

247     public static void addRunToLineAttributes(Map JavaDoc attributes) {
248         attributes.put(IBreakpoint.PERSISTED, Boolean.FALSE);
249         attributes.put(RUN_TO_LINE, Boolean.TRUE);
250     }
251     
252     /**
253      * Returns the method associated with the method entry
254      * breakpoint.
255      *
256      * @param breakpoint Java method entry breakpoint
257      * @return method
258      */

259     public static IMethod getMethod(IJavaMethodBreakpoint breakpoint) {
260         String JavaDoc 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     /**
273      * Returns the field associated with the watchpoint.
274      *
275      * @param breakpoint Java watchpoint
276      * @return field
277      */

278     public static IField getField(IJavaWatchpoint breakpoint) {
279         String JavaDoc 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     /**
292      * Returns whether the given breakpoint is a run to line
293      * breakpoint
294      *
295      * @param breakpoint line breakpoint
296      * @return whether the given breakpoint is a run to line
297      * breakpoint
298      */

299     public static boolean isRunToLineBreakpoint(IJavaLineBreakpoint breakpoint) {
300         return breakpoint.getMarker().getAttribute(RUN_TO_LINE, false);
301     }
302     
303     /**
304      * Returns whether the given breakpoint is a compilation
305      * problem breakpoint or uncaught exception breakpoint
306      *
307      * @param breakpoint breakpoint
308      * @return whether the given breakpoint is a compilation error breakpoint or
309      * uncaught exception breakpoint
310      */

311     public static boolean isProblemBreakpoint(IBreakpoint breakpoint) {
312         return breakpoint == JavaDebugOptionsManager.getDefault().getSuspendOnCompilationErrorBreakpoint() ||
313             breakpoint == JavaDebugOptionsManager.getDefault().getSuspendOnUncaughtExceptionBreakpoint();
314     }
315 }
316
Popular Tags