1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.debug.core.DebugEvent; 15 import org.eclipse.debug.core.DebugException; 16 import org.eclipse.debug.core.ILaunch; 17 import org.eclipse.debug.core.model.IDebugElement; 18 import org.eclipse.debug.core.model.ISourceLocator; 19 import org.eclipse.debug.core.model.IStackFrame; 20 import org.eclipse.debug.core.model.IThread; 21 import org.eclipse.debug.core.model.IValue; 22 import org.eclipse.debug.core.model.IWatchExpressionDelegate; 23 import org.eclipse.debug.core.model.IWatchExpressionListener; 24 import org.eclipse.debug.core.model.IWatchExpressionResult; 25 import org.eclipse.jdt.core.IJavaElement; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 28 import org.eclipse.jdt.debug.core.IJavaStackFrame; 29 import org.eclipse.jdt.debug.core.IJavaThread; 30 import org.eclipse.jdt.debug.eval.IAstEvaluationEngine; 31 import org.eclipse.jdt.debug.eval.IEvaluationListener; 32 import org.eclipse.jdt.debug.eval.IEvaluationResult; 33 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 34 import org.eclipse.jdt.internal.debug.core.model.JDIThread; 35 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression; 36 37 40 public class JavaWatchExpressionDelegate implements IWatchExpressionDelegate { 41 42 private String fExpressionText; 43 private IWatchExpressionListener fListener; 44 45 48 public void evaluateExpression(String expression, IDebugElement context, IWatchExpressionListener listener) { 49 fExpressionText= expression; 50 fListener= listener; 51 IStackFrame frame = null; 53 if (context instanceof IStackFrame) { 54 frame = (IStackFrame)context; 55 } else if (context instanceof IThread) { 56 try { 57 frame = ((IThread)context).getTopStackFrame(); 58 } catch (DebugException e) { 59 } 60 } 61 if (frame == null) { 62 fListener.watchEvaluationFinished(null); 63 } else { 64 final IJavaStackFrame javaStackFrame =(IJavaStackFrame) ((IAdaptable)frame).getAdapter(IJavaStackFrame.class); 66 if (javaStackFrame != null) { 67 doEvaluation(javaStackFrame); 68 } else { 69 fListener.watchEvaluationFinished(null); 70 } 71 } 72 } 73 74 83 protected void doEvaluation(IJavaStackFrame javaStackFrame) { 84 IJavaThread thread = (IJavaThread)javaStackFrame.getThread(); 85 if (preEvaluationCheck(thread)) { 86 thread.queueRunnable(new EvaluationRunnable(javaStackFrame)); 87 } else { 88 fListener.watchEvaluationFinished(null); 89 } 90 } 91 92 private boolean preEvaluationCheck(IJavaThread javaThread) { 93 if (javaThread == null) { 94 return false; 95 } 96 if (javaThread.isSuspended() && ((JDIThread)javaThread).isInvokingMethod()) { 97 return false; 98 } 99 return true; 100 } 101 102 105 private final class EvaluationRunnable implements Runnable { 106 107 private final IJavaStackFrame fStackFrame; 108 109 private EvaluationRunnable(IJavaStackFrame frame) { 110 fStackFrame= frame; 111 } 112 113 public void run() { 114 IJavaProject project = getProject(fStackFrame); 115 if (project == null) { 116 fListener.watchEvaluationFinished(null); 117 return; 118 } 119 IAstEvaluationEngine evaluationEngine= JDIDebugPlugin.getDefault().getEvaluationEngine(project, (IJavaDebugTarget) fStackFrame.getDebugTarget()); 120 IEvaluationListener listener= new IEvaluationListener() { 122 public void evaluationComplete(final IEvaluationResult result) { 123 IWatchExpressionResult watchResult= new IWatchExpressionResult() { 124 public IValue getValue() { 125 return result.getValue(); 126 } 127 public boolean hasErrors() { 128 return result.hasErrors(); 129 } 130 public String [] getErrorMessages() { 131 return JavaInspectExpression.getErrorMessages(result); 132 } 133 public String getExpressionText() { 134 return result.getSnippet(); 135 } 136 public DebugException getException() { 137 return result.getException(); 138 } 139 }; 140 fListener.watchEvaluationFinished(watchResult); 141 } 142 }; 143 try { 144 evaluationEngine.evaluate(fExpressionText, fStackFrame, listener, DebugEvent.EVALUATION_IMPLICIT, false); 145 } catch (DebugException e) { 146 JDIDebugPlugin.log(e); 147 fListener.watchEvaluationFinished(null); 148 } 149 } 150 } 151 152 155 private IJavaProject getProject(IJavaStackFrame javaStackFrame) { 156 ILaunch launch = javaStackFrame.getLaunch(); 157 if (launch == null) { 158 return null; 159 } 160 ISourceLocator locator= launch.getSourceLocator(); 161 if (locator == null) { 162 return null; 163 } 164 165 Object sourceElement = locator.getSourceElement(javaStackFrame); 166 if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) { 167 sourceElement = ((IAdaptable)sourceElement).getAdapter(IJavaElement.class); 168 } 169 if (sourceElement instanceof IJavaElement) { 170 return ((IJavaElement) sourceElement).getJavaProject(); 171 } 172 return null; 173 } 174 } 175 | Popular Tags |