|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 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.evaluation; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
|
| 15 |
import org.eclipse.core.commands.AbstractHandler; |
| 16 |
import org.eclipse.core.commands.ExecutionEvent; |
| 17 |
import org.eclipse.core.commands.ExecutionException; |
| 18 |
import org.eclipse.core.expressions.EvaluationContext; |
| 19 |
import org.eclipse.core.resources.IResource; |
| 20 |
import org.eclipse.core.runtime.CoreException; |
| 21 |
import org.eclipse.core.runtime.IAdaptable; |
| 22 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 23 |
import org.eclipse.core.runtime.IStatus; |
| 24 |
import org.eclipse.core.runtime.Status; |
| 25 |
import org.eclipse.debug.core.DebugEvent; |
| 26 |
import org.eclipse.debug.core.DebugException; |
| 27 |
import org.eclipse.debug.core.ILaunch; |
| 28 |
import org.eclipse.debug.core.model.ISourceLocator; |
| 29 |
import org.eclipse.debug.core.model.IStackFrame; |
| 30 |
import org.eclipse.debug.ui.DebugUITools; |
| 31 |
import org.eclipse.debug.ui.contexts.IDebugContextService; |
| 32 |
import org.eclipse.jdt.core.IJavaElement; |
| 33 |
import org.eclipse.jdt.core.IJavaProject; |
| 34 |
import org.eclipse.jdt.core.JavaCore; |
| 35 |
import org.eclipse.jdt.core.Signature; |
| 36 |
import org.eclipse.jdt.debug.core.IJavaDebugTarget; |
| 37 |
import org.eclipse.jdt.debug.core.IJavaStackFrame; |
| 38 |
import org.eclipse.jdt.debug.core.IJavaThread; |
| 39 |
import org.eclipse.jdt.debug.core.JDIDebugModel; |
| 40 |
import org.eclipse.jdt.debug.eval.IEvaluationEngine; |
| 41 |
import org.eclipse.jdt.debug.eval.IEvaluationListener; |
| 42 |
import org.eclipse.jdt.debug.eval.IEvaluationResult; |
| 43 |
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; |
| 44 |
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 45 |
import org.eclipse.jdt.internal.debug.ui.actions.ActionMessages; |
| 46 |
import org.eclipse.jdt.internal.debug.ui.actions.EvaluateAction; |
| 47 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 48 |
import org.eclipse.jface.text.ITextSelection; |
| 49 |
import org.eclipse.jface.viewers.ISelection; |
| 50 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 51 |
import org.eclipse.swt.widgets.Display; |
| 52 |
import org.eclipse.ui.ISources; |
| 53 |
import org.eclipse.ui.IWorkbench; |
| 54 |
import org.eclipse.ui.IWorkbenchWindow; |
| 55 |
import org.eclipse.ui.PlatformUI; |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 3.3 |
| 59 |
*/ |
| 60 |
public class ForceReturnHandler extends AbstractHandler implements IEvaluationListener { |
| 61 |
|
| 62 |
public ForceReturnHandler() { |
| 63 |
System.out.println("CREATED"); |
| 64 |
System.out.println(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
| 65 |
} |
| 66 |
|
| 67 |
/* (non-Javadoc) |
| 68 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 69 |
*/ |
| 70 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 71 |
ISelection selection = getSelection(event); |
| 72 |
if (selection instanceof ITextSelection) { |
| 73 |
ITextSelection textSelection = (ITextSelection) selection; |
| 74 |
Object debugContext = getDebugContext(event); |
| 75 |
if (debugContext instanceof IAdaptable) { |
| 76 |
IJavaStackFrame frame = (IJavaStackFrame) ((IAdaptable)debugContext).getAdapter(IJavaStackFrame.class); |
| 77 |
if (frame != null) { |
| 78 |
IJavaThread thread = (IJavaThread) frame.getThread(); |
| 79 |
IJavaDebugTarget target = (IJavaDebugTarget) frame.getDebugTarget(); |
| 80 |
try { |
| 81 |
String signature = frame.getSignature(); |
| 82 |
String returnType = Signature.getReturnType(signature); |
| 83 |
if (Signature.SIG_VOID.equals(returnType)) { |
| 84 |
thread.forceReturn(target.voidValue()); |
| 85 |
} else { |
| 86 |
evaluate(textSelection.getText(), frame, this); |
| 87 |
} |
| 88 |
} catch (DebugException e) { |
| 89 |
// TODO Auto-generated catch block |
| 90 |
e.printStackTrace(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
public void dispose() { |
| 99 |
System.out.println("DISPOSED"); |
| 100 |
super.dispose(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the active debug context for the given execution event. |
| 105 |
* |
| 106 |
* @param event execution event |
| 107 |
* @return active debug context or <code>null</code> |
| 108 |
*/ |
| 109 |
protected Object getDebugContext(ExecutionEvent event) { |
| 110 |
Object context = event.getApplicationContext(); |
| 111 |
if (context instanceof EvaluationContext) { |
| 112 |
EvaluationContext ec = (EvaluationContext) context; |
| 113 |
Object variable = ec.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME); |
| 114 |
if (variable instanceof IWorkbenchWindow) { |
| 115 |
IWorkbenchWindow window = (IWorkbenchWindow) variable; |
| 116 |
IDebugContextService contextService = DebugUITools.getDebugContextManager().getContextService(window); |
| 117 |
ISelection debugContext = contextService.getActiveContext(); |
| 118 |
if (debugContext instanceof IStructuredSelection) { |
| 119 |
IStructuredSelection ss = (IStructuredSelection) debugContext; |
| 120 |
return ss.getFirstElement(); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the current selection for the given execution event or <code>null</code>. |
| 129 |
* |
| 130 |
* @param event execution event |
| 131 |
* @return current selection or <code>null</code> |
| 132 |
*/ |
| 133 |
protected ISelection getSelection(ExecutionEvent event) { |
| 134 |
Object context = event.getApplicationContext(); |
| 135 |
if (context instanceof EvaluationContext) { |
| 136 |
EvaluationContext ec = (EvaluationContext) context; |
| 137 |
Object variable = ec.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME); |
| 138 |
if (variable instanceof ISelection) { |
| 139 |
return (ISelection)variable; |
| 140 |
} |
| 141 |
} |
| 142 |
return null; |
| 143 |
} |
| 144 |
|
| 145 |
protected void reportError(String message) { |
| 146 |
Status status= new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, message, null); |
| 147 |
JDIDebugUIPlugin.statusDialog(status); |
| 148 |
} |
| 149 |
|
| 150 |
protected void evaluate(final String expression, final IJavaStackFrame frame, final IEvaluationListener listener) { |
| 151 |
if (frame == null) { |
| 152 |
reportError(ActionMessages.Evaluate_error_message_stack_frame_context); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
// check for nested evaluation |
| 157 |
IJavaThread thread = (IJavaThread)frame.getThread(); |
| 158 |
if (thread.isPerformingEvaluation()) { |
| 159 |
reportError(ActionMessages.EvaluateAction_Cannot_perform_nested_evaluations__1); |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
IRunnableWithProgress runnable = new IRunnableWithProgress() { |
| 164 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| 165 |
if (frame.isSuspended()) { |
| 166 |
IJavaElement javaElement= getJavaElement(frame); |
| 167 |
if (javaElement != null) { |
| 168 |
IJavaProject project = javaElement.getJavaProject(); |
| 169 |
IEvaluationEngine engine = null; |
| 170 |
try { |
| 171 |
engine = JDIDebugPlugin.getDefault().getEvaluationEngine(project, (IJavaDebugTarget)frame.getDebugTarget()); |
| 172 |
boolean hitBreakpoints= JDIDebugModel.getPreferences().getBoolean(JDIDebugModel.PREF_SUSPEND_FOR_BREAKPOINTS_DURING_EVALUATION); |
| 173 |
engine.evaluate(expression, frame, listener, DebugEvent.EVALUATION, hitBreakpoints); |
| 174 |
return; |
| 175 |
} catch (CoreException e) { |
| 176 |
throw new InvocationTargetException(e, EvaluateAction.getExceptionMessage(e)); |
| 177 |
} |
| 178 |
} |
| 179 |
throw new InvocationTargetException(null, ActionMessages.Evaluate_error_message_src_context); |
| 180 |
} |
| 181 |
// thread not suspended |
| 182 |
throw new InvocationTargetException(null, ActionMessages.EvaluateAction_Thread_not_suspended___unable_to_perform_evaluation__1); |
| 183 |
} |
| 184 |
}; |
| 185 |
|
| 186 |
IWorkbench workbench = JDIDebugUIPlugin.getDefault().getWorkbench(); |
| 187 |
try { |
| 188 |
workbench.getProgressService().busyCursorWhile(runnable); |
| 189 |
} catch (InvocationTargetException e) { |
| 190 |
String message = e.getMessage(); |
| 191 |
if (message == null) { |
| 192 |
message = e.getClass().getName(); |
| 193 |
if (e.getCause() != null) { |
| 194 |
message = e.getCause().getClass().getName(); |
| 195 |
if (e.getCause().getMessage() != null) { |
| 196 |
message = e.getCause().getMessage(); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
reportError(message); |
| 201 |
} catch (InterruptedException e) { |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
protected IJavaElement getJavaElement(IStackFrame stackFrame) { |
| 206 |
|
| 207 |
// Get the corresponding element. |
| 208 |
ILaunch launch = stackFrame.getLaunch(); |
| 209 |
if (launch == null) { |
| 210 |
return null; |
| 211 |
} |
| 212 |
ISourceLocator locator= launch.getSourceLocator(); |
| 213 |
if (locator == null) |
| 214 |
return null; |
| 215 |
|
| 216 |
Object sourceElement = locator.getSourceElement(stackFrame); |
| 217 |
if (sourceElement instanceof IJavaElement) { |
| 218 |
return (IJavaElement) sourceElement; |
| 219 |
} else if (sourceElement instanceof IResource) { |
| 220 |
IJavaProject project = JavaCore.create(((IResource)sourceElement).getProject()); |
| 221 |
if (project.exists()) { |
| 222 |
return project; |
| 223 |
} |
| 224 |
} |
| 225 |
return null; |
| 226 |
} |
| 227 |
|
| 228 |
/* (non-Javadoc) |
| 229 |
* @see org.eclipse.jdt.debug.eval.IEvaluationListener#evaluationComplete(org.eclipse.jdt.debug.eval.IEvaluationResult) |
| 230 |
*/ |
| 231 |
public void evaluationComplete(IEvaluationResult result) { |
| 232 |
// if plug-in has shutdown, ignore - see bug# 8693 |
| 233 |
if (JDIDebugUIPlugin.getDefault() == null) { |
| 234 |
return; |
| 235 |
} |
| 236 |
final Display display= JDIDebugUIPlugin.getStandardDisplay(); |
| 237 |
if (display.isDisposed()) { |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
if (result.hasErrors()) { |
| 242 |
|
| 243 |
} else { |
| 244 |
try { |
| 245 |
result.getThread().forceReturn(result.getValue()); |
| 246 |
} catch (DebugException e) { |
| 247 |
// TODO Auto-generated catch block |
| 248 |
e.printStackTrace(); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |