KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > breakpoints > JavaTargetPatternBreakpoint


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.breakpoints;
12
13  
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRunnable;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.debug.core.DebugException;
25 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
26 import org.eclipse.jdt.debug.core.IJavaTargetPatternBreakpoint;
27 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
28 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
29
30 import com.sun.jdi.AbsentInformationException;
31 import com.sun.jdi.ReferenceType;
32 import com.sun.jdi.VMDisconnectedException;
33 import com.sun.jdi.VirtualMachine;
34
35 public class JavaTargetPatternBreakpoint extends JavaLineBreakpoint implements IJavaTargetPatternBreakpoint {
36
37     private static final String JavaDoc TARGET_PATTERN_BREAKPOINT = "org.eclipse.jdt.debug.javaTargetPatternBreakpointMarker"; //$NON-NLS-1$
38

39     /**
40      * Table of targets to patterns
41      */

42     private HashMap JavaDoc fPatterns;
43     
44     public JavaTargetPatternBreakpoint() {
45     }
46     
47     /**
48      * @see JDIDebugModel#createTargetPatternBreakpoint(IResource, String, int, int, int, int, boolean, Map)
49      */

50     public JavaTargetPatternBreakpoint(IResource resource, String JavaDoc sourceName, int lineNumber, int charStart, int charEnd, int hitCount, boolean add, Map JavaDoc attributes) throws DebugException {
51         this(resource, sourceName, lineNumber, charStart, charEnd, hitCount, add, attributes, TARGET_PATTERN_BREAKPOINT);
52     }
53     
54     public JavaTargetPatternBreakpoint(final IResource resource, final String JavaDoc sourceName, final int lineNumber, final int charStart, final int charEnd, final int hitCount, final boolean add, final Map JavaDoc attributes, final String JavaDoc markerType) throws DebugException {
55         IWorkspaceRunnable wr= new IWorkspaceRunnable() {
56             public void run(IProgressMonitor monitor) throws CoreException {
57     
58                 // create the marker
59
setMarker(resource.createMarker(markerType));
60                 
61                 // add attributes
62
addLineBreakpointAttributes(attributes, getModelIdentifier(), true, lineNumber, charStart, charEnd);
63                 addSourceNameAndHitCount(attributes, sourceName, hitCount);
64                 attributes.put(SUSPEND_POLICY, new Integer JavaDoc(getDefaultSuspendPolicy()));
65                 // set attributes
66
ensureMarker().setAttributes(attributes);
67                 
68                 register(add);
69             }
70         };
71         run(getMarkerRule(resource), wr);
72     }
73     
74     /**
75      * Creates the event requests to:<ul>
76      * <li>Listen to class loads related to the breakpoint</li>
77      * <li>Respond to the breakpoint being hit</li>
78      * </ul>
79      */

80     public void addToTarget(JDIDebugTarget target) throws CoreException {
81         
82         // pre-notification
83
fireAdding(target);
84                 
85         String JavaDoc referenceTypeName= getPattern(target);
86         if (referenceTypeName == null) {
87             return;
88         }
89         
90         String JavaDoc classPrepareTypeName= referenceTypeName;
91         // create request to listen to class loads
92
//name may only be partially resolved
93
if (!referenceTypeName.endsWith("*")) { //$NON-NLS-1$
94
classPrepareTypeName= classPrepareTypeName + '*';
95         }
96         registerRequest(target.createClassPrepareRequest(classPrepareTypeName), target);
97         
98         // create breakpoint requests for each class currently loaded
99
VirtualMachine vm = target.getVM();
100         if (vm == null) {
101             target.requestFailed(JDIDebugBreakpointMessages.JavaTargetPatternBreakpoint_Unable_to_add_breakpoint___VM_disconnected__1, null);
102         }
103         List JavaDoc classes= vm.allClasses();
104         if (classes != null) {
105             Iterator JavaDoc iter = classes.iterator();
106             String JavaDoc typeName= null;
107             ReferenceType type= null;
108             while (iter.hasNext()) {
109                 type= (ReferenceType) iter.next();
110                 typeName= type.name();
111                 if (typeName != null && typeName.startsWith(referenceTypeName)) {
112                     createRequest(target, type);
113                 }
114             }
115         }
116     }
117     
118     /**
119      * @see JavaBreakpoint#getReferenceTypeName()
120      */

121     protected String JavaDoc getReferenceTypeName() {
122         String JavaDoc name= "*"; //$NON-NLS-1$
123
try {
124             name= getSourceName();
125         } catch (CoreException ce) {
126             JDIDebugPlugin.log(ce);
127         }
128         return name;
129     }
130     
131     /**
132      * @see JavaBreakpoint#installableReferenceType(ReferenceType)
133      */

134     protected boolean installableReferenceType(ReferenceType type, JDIDebugTarget target) throws CoreException {
135         // if the source name attribute is specified, check for a match with the
136
// debug attribute (if available)
137
if (getSourceName() != null) {
138             String JavaDoc sourceName = null;
139             try {
140                 sourceName = type.sourceName();
141             } catch (AbsentInformationException e) {
142                 // unable to compare
143
} catch (VMDisconnectedException e) {
144                 if (!target.isAvailable()) {
145                     return false;
146                 }
147                 target.targetRequestFailed(MessageFormat.format(JDIDebugBreakpointMessages.JavaPatternBreakpoint_exception_source_name,new String JavaDoc[] {e.toString(), type.name()}) ,e);
148                 // execution will not reach this line, as
149
// #targetRequestFailed will throw an exception
150
return false;
151             } catch (RuntimeException JavaDoc e) {
152                 target.targetRequestFailed(MessageFormat.format(JDIDebugBreakpointMessages.JavaPatternBreakpoint_exception_source_name,new String JavaDoc[] {e.toString(), type.name()}) ,e);
153                 // execution will not reach this line, as
154
// #targetRequestFailed will throw an exception
155
return false;
156             }
157             
158             // if the debug attribute matches the source name, attempt installion
159
if (sourceName != null) {
160                 if (!getSourceName().equalsIgnoreCase(sourceName)) {
161                     return false;
162                 }
163             }
164         }
165         
166         String JavaDoc pattern= getPattern(target);
167         String JavaDoc queriedType= type.name();
168         if (pattern == null || queriedType == null) {
169             return false;
170         }
171         if (queriedType.startsWith(pattern)) {
172             // query registered listeners to see if this pattern breakpoint should
173
// be installed in the given target
174
return queryInstallListeners(target, type);
175         }
176         return false;
177     }
178     
179     /**
180      * Adds the source name and hit count attributes to the given map.
181      */

182     protected void addSourceNameAndHitCount(Map JavaDoc attributes, String JavaDoc sourceName, int hitCount) {
183         if (sourceName != null) {
184             attributes.put(SOURCE_NAME, sourceName);
185         }
186         if (hitCount > 0) {
187             attributes.put(HIT_COUNT, new Integer JavaDoc(hitCount));
188             attributes.put(EXPIRED, Boolean.FALSE);
189         }
190     }
191     
192     /**
193      * @see IJavaTargetPatternBreakpoint#getPattern(IJavaDebugTarget)
194      */

195     public String JavaDoc getPattern(IJavaDebugTarget target) {
196         if (fPatterns != null) {
197             return (String JavaDoc)fPatterns.get(target);
198         }
199         return null;
200     }
201     
202     /**
203      * @see IJavaTargetPatternBreakpoint#setPattern(IJavaDebugTarget, String)
204      */

205     public void setPattern(IJavaDebugTarget target, String JavaDoc pattern) throws CoreException {
206         if (fPatterns == null) {
207             fPatterns = new HashMap JavaDoc(2);
208         }
209         // if pattern is changing then remove and re-add
210
String JavaDoc oldPattern = getPattern(target);
211         fPatterns.put(target, pattern);
212         if (oldPattern != null && !oldPattern.equals(pattern)) {
213             recreate((JDIDebugTarget)target);
214             fireChanged();
215         }
216     }
217     
218     /**
219      * @see IJavaTargetPatternBreakpoint#getSourceName()
220      */

221     public String JavaDoc getSourceName() throws CoreException {
222         return (String JavaDoc) ensureMarker().getAttribute(SOURCE_NAME);
223     }
224             
225     /**
226      * @see org.eclipse.jdt.internal.debug.core.breakpoints.JavaBreakpoint#removeFromTarget(JDIDebugTarget)
227      */

228     public void removeFromTarget(JDIDebugTarget target) throws CoreException {
229         fPatterns.remove(target);
230         super.removeFromTarget(target);
231     }
232 }
233
234
Popular Tags