KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > display > JavaInspectExpression


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.ui.display;
12
13  
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.core.runtime.PlatformObject;
17 import org.eclipse.debug.core.DebugEvent;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.IDebugEventSetListener;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.model.IDebugElement;
23 import org.eclipse.debug.core.model.IDebugTarget;
24 import org.eclipse.debug.core.model.IErrorReportingExpression;
25 import org.eclipse.debug.core.model.IValue;
26 import org.eclipse.jdt.debug.core.IJavaValue;
27 import org.eclipse.jdt.debug.eval.IEvaluationResult;
28
29 import com.sun.jdi.InvocationException;
30
31 /**
32  * An implementation of an expression produced from the
33  * inspect action. An inspect expression removes
34  * itself from the expression manager when its debug
35  * target terminates.
36  */

37 public class JavaInspectExpression extends PlatformObject implements IErrorReportingExpression, IDebugEventSetListener {
38     
39     /**
40      * The value of this expression
41      */

42     private IJavaValue fValue;
43     
44     /**
45      * The code snippet for this expression.
46      */

47     private String JavaDoc fExpression;
48     
49     private IEvaluationResult fResult;
50
51     /**
52      * Constructs a new inspect result for the given
53      * expression and resulting value. Starts listening
54      * to debug events such that this element will remove
55      * itself from the expression manager when its debug
56      * target terminates.
57      *
58      * @param expression code snippet
59      * @param value value of the expression
60      */

61     public JavaInspectExpression(String JavaDoc expression, IJavaValue value) {
62         fValue = value;
63         fExpression = expression;
64         DebugPlugin.getDefault().addDebugEventListener(this);
65     }
66     
67     /**
68      * Constructs a new inspect result for the given
69      * evaluation result, which provides a snippet, value,
70      * and error messages, if any.
71      *
72      * @param result the evaluation result
73      */

74     public JavaInspectExpression(IEvaluationResult result) {
75         this(result.getSnippet(), result.getValue());
76         fResult= result;
77     }
78     
79     /**
80      * @see IExpression#getExpressionText()
81      */

82     public String JavaDoc getExpressionText() {
83         return fExpression;
84     }
85
86     /**
87      * @see IExpression#getValue()
88      */

89     public IValue getValue() {
90         return fValue;
91     }
92
93     /**
94      * @see IDebugElement#getDebugTarget()
95      */

96     public IDebugTarget getDebugTarget() {
97         IValue value= getValue();
98         if (value != null) {
99             return getValue().getDebugTarget();
100         }
101         if (fResult != null) {
102             return fResult.getThread().getDebugTarget();
103         }
104         // An expression should never be created with a null value *and*
105
// a null result.
106
return null;
107     }
108
109     /**
110      * @see IDebugElement#getModelIdentifier()
111      */

112     public String JavaDoc getModelIdentifier() {
113         return getDebugTarget().getModelIdentifier();
114     }
115
116     /**
117      * @see IDebugElement#getLaunch()
118      */

119     public ILaunch getLaunch() {
120         return getDebugTarget().getLaunch();
121     }
122
123     /**
124      * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
125      */

126     public void handleDebugEvents(DebugEvent[] events) {
127         for (int i = 0; i < events.length; i++) {
128             DebugEvent event = events[i];
129             switch (event.getKind()) {
130                 case DebugEvent.TERMINATE:
131                     if (event.getSource().equals(getDebugTarget())) {
132                         DebugPlugin.getDefault().getExpressionManager().removeExpression(this);
133                     }
134                     break;
135                 case DebugEvent.SUSPEND:
136                     if (event.getDetail() != DebugEvent.EVALUATION_IMPLICIT) {
137                         if (event.getSource() instanceof IDebugElement) {
138                             IDebugElement source = (IDebugElement) event.getSource();
139                             if (source.getDebugTarget().equals(getDebugTarget())) {
140                                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[]{new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT)});
141                             }
142                         }
143                     }
144                     break;
145             }
146         }
147     }
148
149     /**
150      * @see IExpression#dispose()
151      */

152     public void dispose() {
153         DebugPlugin.getDefault().removeDebugEventListener(this);
154     }
155
156     /**
157      * @see org.eclipse.debug.core.model.IErrorReportingExpression#hasErrors()
158      */

159     public boolean hasErrors() {
160         return fResult != null && fResult.hasErrors();
161     }
162
163     /**
164      * @see org.eclipse.debug.core.model.IErrorReportingExpression#getErrorMessages()
165      */

166     public String JavaDoc[] getErrorMessages() {
167         return getErrorMessages(fResult);
168     }
169     
170     public static String JavaDoc[] getErrorMessages(IEvaluationResult result) {
171         if (result == null) {
172             return new String JavaDoc[0];
173         }
174         String JavaDoc messages[]= result.getErrorMessages();
175         if (messages.length > 0) {
176             return messages;
177         }
178         DebugException exception= result.getException();
179         if (exception != null ) {
180             Throwable JavaDoc cause= exception.getStatus().getException();
181             if (cause instanceof InvocationException) {
182                 String JavaDoc nestedMessage= ((InvocationException) cause).exception().referenceType().name();
183                 return new String JavaDoc[] { MessageFormat.format(DisplayMessages.JavaInspectExpression_0, new String JavaDoc[] {nestedMessage}) };
184             }
185             return new String JavaDoc[] { exception.getMessage() };
186         }
187         return new String JavaDoc[0];
188     }
189 }
190
Popular Tags