KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaWatchExpressionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
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 /**
38  *
39  */

40 public class JavaWatchExpressionDelegate implements IWatchExpressionDelegate {
41     
42     private String JavaDoc fExpressionText;
43     private IWatchExpressionListener fListener;
44
45     /**
46      * @see org.eclipse.debug.core.model.IWatchExpressionDelegate#getValue(java.lang.String, org.eclipse.debug.core.model.IDebugElement)
47      */

48     public void evaluateExpression(String JavaDoc expression, IDebugElement context, IWatchExpressionListener listener) {
49         fExpressionText= expression;
50         fListener= listener;
51         // find a stack frame context if possible.
52
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             // consult the adapter in case of a wrappered debug model
65
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     /**
75      * Ask to evaluate the expression in the context of the given stack frame.
76      *
77      * The evaluation is performed asynchronously. A change debug event, with
78      * this as the source, is fired when the evaluation is completed.
79      *
80      * @param javaStackFrame the stack frame in the context of which performed
81      * the evaluation.
82      */

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     /**
103      * Runnable used to evaluate the expression.
104      */

105     private final class EvaluationRunnable implements Runnable JavaDoc {
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             // the evaluation listener
121
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 JavaDoc[] getErrorMessages() {
131                             return JavaInspectExpression.getErrorMessages(result);
132                         }
133                         public String JavaDoc 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     /**
153      * Return the project associated with the given stack frame.
154      */

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 JavaDoc 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