KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > models > JPDAObjectWatchImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.models;
21
22 import com.sun.jdi.AbsentInformationException;
23 import com.sun.jdi.ClassNotLoadedException;
24 import com.sun.jdi.Field;
25 import com.sun.jdi.InvalidTypeException;
26 import com.sun.jdi.LocalVariable;
27 import com.sun.jdi.ObjectReference;
28 import com.sun.jdi.Value;
29
30 import org.netbeans.api.debugger.Watch;
31 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
32 import org.netbeans.api.debugger.jpda.JPDAWatch;
33 import org.netbeans.api.debugger.jpda.ObjectVariable;
34 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
35
36
37 /**
38  * Represents watch in JPDA debugger.
39  *
40  * @author Jan Jancura
41  */

42
43 class JPDAObjectWatchImpl extends AbstractVariable implements JPDAWatch,
44 ObjectVariable {
45
46     private JPDADebuggerImpl debugger;
47     private Watch watch;
48     private String JavaDoc exceptionDescription;
49     
50     
51     JPDAObjectWatchImpl (JPDADebuggerImpl debugger, Watch watch, ObjectReference v) {
52         super (
53             debugger,
54             v,
55             "" + watch +
56                 (v instanceof ObjectReference ? "^" : "")
57         );
58         this.debugger = debugger;
59         this.watch = watch;
60     }
61     
62     JPDAObjectWatchImpl (JPDADebuggerImpl debugger, Watch watch, String JavaDoc exceptionDescription) {
63         super (
64             debugger,
65             null,
66             "" + watch
67         );
68         this.debugger = debugger;
69         this.watch = watch;
70         this.exceptionDescription = exceptionDescription;
71     }
72     
73     /**
74      * Watched expression.
75      *
76      * @return watched expression
77      */

78     public String JavaDoc getExpression () {
79         return watch.getExpression ();
80     }
81
82     /**
83      * Sets watched expression.
84      *
85      * @param expression a expression to be watched
86      */

87     public void setExpression (String JavaDoc expression) {
88         watch.setExpression (expression);
89     }
90     
91     /**
92      * Remove the watch from the list of all watches in the system.
93      */

94     public void remove () {
95         watch.remove ();
96     }
97     
98     /**
99      * Returns description of problem is this watch can not be evaluated
100      * in current context.
101      *
102      * @return description of problem
103      */

104     public String JavaDoc getExceptionDescription () {
105         return exceptionDescription;
106     }
107
108     /**
109     * Sets string representation of value of this variable.
110     *
111     * @param value string representation of value of this variable.
112     *
113     public void setValue (String expression) throws InvalidExpressionException {
114         // evaluate expression to Value
115         Value value = model.getDebugger ().evaluateIn (expression);
116         // set new value to remote veriable
117         setValue (value);
118         // set new value to this model
119         setInnerValue (value);
120         // refresh tree
121         model.fireTableValueChangedChanged (this, null);
122     }
123      */

124     
125     protected void setValue (final Value value)
126     throws InvalidExpressionException {
127         
128         // 1) get frame
129
CallStackFrameImpl frame = (CallStackFrameImpl) debugger.
130             getCurrentCallStackFrame ();
131         if (frame == null)
132             throw new InvalidExpressionException ("No curent frame.");
133         
134         // 2) try to set as a local variable value
135
try {
136             LocalVariable local = frame.getStackFrame ().visibleVariableByName
137                 (getExpression ());
138             if (local != null)
139                 try {
140                     frame.getStackFrame ().setValue (local, value);
141                     return;
142                 } catch (InvalidTypeException ex) {
143                     throw new InvalidExpressionException (ex);
144                 } catch (ClassNotLoadedException ex) {
145                     throw new InvalidExpressionException (ex);
146                 }
147         } catch (AbsentInformationException ex) {
148             // no local variable visible in this case
149
}
150         
151         // 3) try tu set as a field
152
ObjectReference thisObject = frame.getStackFrame ().thisObject ();
153         if (thisObject == null)
154             throw new InvalidExpressionException
155                 ("Can not set value to expression.");
156         Field field = thisObject.referenceType ().fieldByName
157             (getExpression ());
158         if (field == null)
159             throw new InvalidExpressionException
160                 ("Can not set value to expression.");
161         try {
162             thisObject.setValue (field, value);
163         } catch (InvalidTypeException ex) {
164             throw new InvalidExpressionException (ex);
165         } catch (ClassNotLoadedException ex) {
166             throw new InvalidExpressionException (ex);
167         }
168     }
169     
170     protected void setInnerValue (Value v) {
171         super.setInnerValue (v);
172         exceptionDescription = null;
173     }
174     
175     void setException (String JavaDoc exceptionDescription) {
176         super.setInnerValue (null);
177         this.exceptionDescription = exceptionDescription;
178     }
179     
180     boolean isPrimitive () {
181         return !(getInnerValue () instanceof ObjectReference);
182     }
183     
184     public JPDAObjectWatchImpl clone() {
185         JPDAObjectWatchImpl clon;
186         if (exceptionDescription == null) {
187             clon = new JPDAObjectWatchImpl(getDebugger(), watch, (ObjectReference) getJDIValue());
188         } else {
189             clon = new JPDAObjectWatchImpl(getDebugger(), watch, exceptionDescription);
190         }
191         return clon;
192     }
193     
194 }
195
196
Popular Tags