KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.jdt.debug.core.IJavaPatternBreakpoint;
25 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
26 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
27
28 import com.sun.jdi.AbsentInformationException;
29 import com.sun.jdi.ReferenceType;
30 import com.sun.jdi.VMDisconnectedException;
31 import com.sun.jdi.VirtualMachine;
32
33 public class JavaPatternBreakpoint extends JavaLineBreakpoint implements IJavaPatternBreakpoint {
34
35     private static final String JavaDoc PATTERN_BREAKPOINT = "org.eclipse.jdt.debug.javaPatternBreakpointMarker"; //$NON-NLS-1$
36

37     /**
38      * Breakpoint attribute storing the pattern identifier of the source
39      * file in which a breakpoint is created
40      * (value <code>"org.eclipse.jdt.debug.core.pattern"</code>). This attribute is a <code>String</code>.
41      */

42     protected static final String JavaDoc PATTERN = "org.eclipse.jdt.debug.core.pattern"; //$NON-NLS-1$
43

44     public JavaPatternBreakpoint() {
45     }
46     
47     /**
48      * @see JDIDebugModel#createPatternBreakpoint(IResource, String, int, int, int, int, boolean, Map)
49      */

50     public JavaPatternBreakpoint(IResource resource, String JavaDoc sourceName, String JavaDoc pattern, int lineNumber, int charStart, int charEnd, int hitCount, boolean add, Map JavaDoc attributes) throws DebugException {
51         this(resource, sourceName, pattern, lineNumber, charStart, charEnd, hitCount, add, attributes, PATTERN_BREAKPOINT);
52     }
53     
54     public JavaPatternBreakpoint(final IResource resource, final String JavaDoc sourceName, final String JavaDoc pattern, 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                 addPatternAndHitCount(attributes, sourceName, pattern, hitCount);
64                 // set attributes
65
attributes.put(SUSPEND_POLICY, new Integer JavaDoc(getDefaultSuspendPolicy()));
66                 ensureMarker().setAttributes(attributes);
67                 
68                 register(add);
69             }
70         };
71         run(getMarkerRule(resource), wr);
72     }
73         
74     /**
75      * @see JavaBreakpoint#getReferenceTypeName()
76      */

77     protected String JavaDoc getReferenceTypeName() {
78         String JavaDoc name= ""; //$NON-NLS-1$
79
try {
80             name= getPattern();
81         } catch (CoreException ce) {
82             JDIDebugPlugin.log(ce);
83         }
84         return name;
85     }
86     
87     /**
88      * @see JavaBreakpoint#installableReferenceType(ReferenceType)
89      */

90     protected boolean installableReferenceType(ReferenceType type, JDIDebugTarget target) throws CoreException {
91         // if the source name attribute is specified, check for a match with the
92
// debug attribute (if available)
93
if (getSourceName() != null) {
94             String JavaDoc sourceName = null;
95             try {
96                 sourceName = type.sourceName();
97             } catch (AbsentInformationException e) {
98                 // unable to compare
99
} catch (VMDisconnectedException e) {
100                 if (!target.isAvailable()) {
101                     return false;
102                 }
103                 target.targetRequestFailed(MessageFormat.format(JDIDebugBreakpointMessages.JavaPatternBreakpoint_exception_source_name,new String JavaDoc[] {e.toString(), type.name()}) ,e);
104                 // execution will not reach this line, as
105
// #targetRequestFailed will throw an exception
106
return false;
107             } catch (RuntimeException JavaDoc e) {
108                 target.targetRequestFailed(MessageFormat.format(JDIDebugBreakpointMessages.JavaPatternBreakpoint_exception_source_name,new String JavaDoc[] {e.toString(), type.name()}) ,e);
109                 // execution will not reach this line, as
110
// #targetRequestFailed will throw an exception
111
return false;
112             }
113             
114             // if the debug attribute matches the source name, attempt installion
115
if (sourceName != null) {
116                 if (!getSourceName().equalsIgnoreCase(sourceName)) {
117                     return false;
118                 }
119             }
120         }
121         
122         String JavaDoc pattern= getPattern();
123         String JavaDoc queriedType= type.name();
124         if (pattern == null || queriedType == null) {
125             return false;
126         }
127         if (queriedType.startsWith(pattern)) {
128             // query registered listeners to see if this pattern breakpoint should
129
// be installed in the given target
130
return queryInstallListeners(target, type);
131         }
132         return false;
133     }
134     
135     /**
136      * Adds the class name pattern and hit count attributes to the given map.
137      */

138     protected void addPatternAndHitCount(Map JavaDoc attributes, String JavaDoc sourceName, String JavaDoc pattern, int hitCount) {
139         attributes.put(PATTERN, pattern);
140         if (sourceName != null) {
141             attributes.put(SOURCE_NAME, sourceName);
142         }
143         if (hitCount > 0) {
144             attributes.put(HIT_COUNT, new Integer JavaDoc(hitCount));
145             attributes.put(EXPIRED, Boolean.FALSE);
146         }
147     }
148     
149     /**
150      * @see IJavaPatternBreakpoint#getPattern()
151      */

152     public String JavaDoc getPattern() throws CoreException {
153         return (String JavaDoc) ensureMarker().getAttribute(PATTERN);
154     }
155     
156     /**
157      * @see IJavaPatternBreakpoint#getSourceName()
158      */

159     public String JavaDoc getSourceName() throws CoreException {
160         return (String JavaDoc) ensureMarker().getAttribute(SOURCE_NAME);
161     }
162
163     protected void createRequests(JDIDebugTarget target) throws CoreException {
164         if (target.isTerminated() || shouldSkipBreakpoint()) {
165             return;
166         }
167         String JavaDoc referenceTypeName= getReferenceTypeName();
168         if (referenceTypeName == null) {
169             return;
170         }
171         
172         String JavaDoc classPrepareTypeName= referenceTypeName;
173         // create request to listen to class loads
174
//name may only be partially resolved
175
if (!referenceTypeName.endsWith("*")) { //$NON-NLS-1$
176
classPrepareTypeName= classPrepareTypeName + '*';
177         }
178         registerRequest(target.createClassPrepareRequest(classPrepareTypeName), target);
179         
180         // create breakpoint requests for each class currently loaded
181
VirtualMachine vm = target.getVM();
182         if (vm == null) {
183             target.requestFailed(JDIDebugBreakpointMessages.JavaPatternBreakpoint_Unable_to_add_breakpoint___VM_disconnected__1, null);
184         }
185         List JavaDoc classes = null;
186         try {
187             classes= vm.allClasses();
188         } catch (RuntimeException JavaDoc e) {
189             target.targetRequestFailed(JDIDebugBreakpointMessages.JavaPatternBreakpoint_0, e);
190         }
191         if (classes != null) {
192             Iterator JavaDoc iter = classes.iterator();
193             String JavaDoc typeName= null;
194             ReferenceType type= null;
195             while (iter.hasNext()) {
196                 type= (ReferenceType) iter.next();
197                 typeName= type.name();
198                 if (typeName != null && typeName.startsWith(referenceTypeName)) {
199                     createRequest(target, type);
200                 }
201             }
202         }
203     }
204 }
205
206
Popular Tags