1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*******************************************************************************
* Copyright (c) 2003, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.debug.ui.actions;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
import org.eclipse.jdt.debug.core.JDIDebugModel;
import org.eclipse.jdt.internal.debug.core.breakpoints.ValidBreakpointLocationLocator;
import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.IEditorStatusLine;
import com.ibm.icu.text.MessageFormat;
/**
* Job used to verify the position of a breakpoint
*/
public class BreakpointLocationVerifierJob extends Job {
/**
* The temporary breakpoint that has been set. Can be <code>null</code> if the callee was not able
* to check if a breakpoint was already set at this position.
*/
private IJavaLineBreakpoint fBreakpoint;
/**
* The number of the line where the breakpoint has been requested.
*/
private int fLineNumber;
/**
* The qualified type name of the class where the temporary breakpoint as been set.
* Can be <code>null</code> if fBreakpoint is null.
*/
private String fTypeName;
/**
* The type in which should be set the breakpoint.
*/
private IType fType;
/**
* The current IEditorPart
*/
private IEditorPart fEditorPart;
/**
* The parsed {@link CompilationUnit}
*/
CompilationUnit fCunit = null;
/**
* The document context
*/
private IDocument fDocument = null;
/**
* The status line to use to display errors
*/
private IEditorStatusLine fStatusLine;
/**
* If a best guess should be made at the breakpoint location
*/
private boolean fBestMatch = false;
/**
* Constructor
* @param document
* @param cunit
* @param breakpoint
* @param lineNumber
* @param typeName
* @param type
* @param editorPart
* @param bestmatch
*/
public BreakpointLocationVerifierJob(IDocument document, CompilationUnit cunit, IJavaLineBreakpoint breakpoint, int lineNumber, String typeName, IType type, IEditorPart editorPart, boolean bestmatch) {
super(ActionMessages.BreakpointLocationVerifierJob_breakpoint_location);
fCunit = cunit;
fDocument = document;
fBreakpoint = breakpoint;
fLineNumber = lineNumber;
fTypeName = typeName;
fType = type;
fEditorPart = editorPart;
fBestMatch = bestmatch;
fStatusLine = (IEditorStatusLine) editorPart.getAdapter(IEditorStatusLine.class);
setSystem(true);
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public IStatus run(IProgressMonitor monitor) {
ValidBreakpointLocationLocator locator = new ValidBreakpointLocationLocator(fCunit, fLineNumber, true, fBestMatch);
fCunit.accept(locator);
int lineNumber = locator.getLineLocation();
String typeName = locator.getFullyQualifiedTypeName();
try {
switch (locator.getLocationType()) {
case ValidBreakpointLocationLocator.LOCATION_LINE:
return manageLineBreakpoint(typeName, lineNumber);
case ValidBreakpointLocationLocator.LOCATION_METHOD:
if (fBreakpoint != null) {
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(fBreakpoint, true);
}
new ToggleBreakpointAdapter().toggleMethodBreakpoints(fEditorPart, new TextSelection(locator.getMemberOffset(), 0));
break;
case ValidBreakpointLocationLocator.LOCATION_FIELD:
if (fBreakpoint != null) {
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(fBreakpoint, true);
}
new ToggleBreakpointAdapter().toggleWatchpoints(fEditorPart, new TextSelection(locator.getMemberOffset(), 0));
break;
default:
// cannot find a valid location
report(ActionMessages.BreakpointLocationVerifierJob_not_valid_location);
if (fBreakpoint != null) {
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(fBreakpoint, true);
}
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, ActionMessages.BreakpointLocationVerifierJob_not_valid_location, null);
}
} catch (CoreException e) {
JDIDebugUIPlugin.log(e);
}
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.OK, ActionMessages.BreakpointLocationVerifierJob_breakpoint_set, null);
}
/**
* Determines the placement of the line breakpoint, and ensures that duplicates are not created
* and that notification is sent in the event of collisions
* @param typeName the fully qualified name of the type to add the line breakpoint to
* @param lineNumber the number we wish to put the breakpoint on
* @return the status of the line breakpoint placement
*/
public IStatus manageLineBreakpoint(String typeName, int lineNumber) {
try {
boolean differentLineNumber= lineNumber != fLineNumber;
IJavaLineBreakpoint breakpoint= JDIDebugModel.lineBreakpointExists(fBreakpoint.getMarker().getResource(), typeName, lineNumber);
boolean breakpointExist= breakpoint != null;
if (fBreakpoint == null) {
if (breakpointExist) {
if (differentLineNumber) {
// There is already a breakpoint on the valid line.
report(MessageFormat.format(ActionMessages.BreakpointLocationVerifierJob_0, new String[]{Integer.toString(lineNumber)}));
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, ActionMessages.BreakpointLocationVerifierJob_not_valid_location, null);
}
// There is already a breakpoint on the valid line, but it's also the requested line.
// Removing the existing breakpoint.
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(breakpoint, true);
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.OK, ActionMessages.BreakpointLocationVerifierJob_breakpointRemoved, null);
}
createNewBreakpoint(lineNumber, typeName);
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.OK, ActionMessages.BreakpointLocationVerifierJob_breakpoint_set, null);
}
if (differentLineNumber) {
if (breakpointExist) {
// there is already a breakpoint on the valid line.
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(fBreakpoint, true);
report(MessageFormat.format(ActionMessages.BreakpointLocationVerifierJob_0, new String[]{Integer.toString(lineNumber)}));
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, ActionMessages.BreakpointLocationVerifierJob_not_valid_location, null);
}
replaceBreakpoint(lineNumber, typeName);
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.WARNING, ActionMessages.BreakpointLocationVerifierJob_breakpointMovedToValidPosition, null);
}
if (!typeName.equals(fTypeName)) {
replaceBreakpoint(lineNumber, typeName);
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.WARNING, ActionMessages.BreakpointLocationVerifierJob_breakpointSetToRightType, null);
}
} catch (CoreException e) {
JDIDebugUIPlugin.log(e);
}
return new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.OK, ActionMessages.BreakpointLocationVerifierJob_breakpoint_set, null);
}
/**
* Remove the temporary breakpoint and create a new breakpoint at the right position.
*/
private void replaceBreakpoint(int lineNumber, String typeName) throws CoreException {
createNewBreakpoint(lineNumber, typeName);
DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(fBreakpoint, true);
}
/**
* Create a new breakpoint at the right position.
*/
private void createNewBreakpoint(int lineNumber, String typeName) throws CoreException {
Map newAttributes = new HashMap(10);
int start = -1, end = -1;
if (fType != null) {
try {
IRegion line = fDocument.getLineInformation(lineNumber - 1);
start = line.getOffset();
end = start + line.getLength();
} catch (BadLocationException ble) {
JDIDebugUIPlugin.log(ble);
}
BreakpointUtils.addJavaBreakpointAttributes(newAttributes, fType);
}
JDIDebugModel.createLineBreakpoint(fBreakpoint.getMarker().getResource(), typeName, lineNumber, start, end, 0, true, newAttributes);
}
/**
* Reports any status to the current active workbench shell
* @param message the message to display
*/
protected void report(final String message) {
JDIDebugUIPlugin.getStandardDisplay().asyncExec(new Runnable() {
public void run() {
if (fStatusLine != null) {
fStatusLine.setMessage(true, message, null);
}
if (message != null && JDIDebugUIPlugin.getActiveWorkbenchShell() != null) {
Display.getCurrent().beep();
}
}
});
}
}
|