KickJava   Java API By Example, From Geeks To Geeks.

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


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.InvalidTypeException;
25 import com.sun.jdi.LocalVariable;
26 import com.sun.jdi.ObjectReference;
27 import com.sun.jdi.Value;
28 import org.netbeans.api.debugger.Watch;
29 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
30 import org.netbeans.api.debugger.jpda.JPDAWatch;
31 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
32
33 /**
34  * Represents watch in JPDA debugger.
35  *
36  * @author Jan Jancura
37  */

38
39 class JPDAWatchImpl extends AbstractVariable implements JPDAWatch {
40
41     private JPDADebuggerImpl debugger;
42     private Watch watch;
43     private String JavaDoc exceptionDescription;
44     private java.lang.ref.Reference JavaDoc<Object JavaDoc> nodeRef;
45     
46     
47     JPDAWatchImpl (JPDADebuggerImpl debugger, Watch watch, Value v, Object JavaDoc node) {
48         super (
49             debugger,
50             v,
51             "" + watch +
52                 (v instanceof ObjectReference ? "^" : "")
53         );
54         this.debugger = debugger;
55         this.watch = watch;
56         this.nodeRef = new java.lang.ref.WeakReference JavaDoc<Object JavaDoc>(node);
57     }
58     
59     JPDAWatchImpl (
60         JPDADebuggerImpl debugger,
61         Watch watch,
62         Exception JavaDoc exception,
63         Object JavaDoc node
64     ) {
65         super (
66             debugger,
67             null,
68             "" + watch
69         );
70         this.debugger = debugger;
71         this.watch = watch;
72         this.exceptionDescription = exception.getLocalizedMessage ();
73         if (exceptionDescription == null)
74             exceptionDescription = exception.getMessage ();
75         this.nodeRef = new java.lang.ref.WeakReference JavaDoc<Object JavaDoc>(node);
76     }
77     
78     /**
79      * Watched expression.
80      *
81      * @return watched expression
82      */

83     public String JavaDoc getExpression () {
84         return watch.getExpression ();
85     }
86
87     /**
88      * Sets watched expression.
89      *
90      * @param expression a expression to be watched
91      */

92     public void setExpression (String JavaDoc expression) {
93         watch.setExpression (expression);
94     }
95     
96     /**
97      * Remove the watch from the list of all watches in the system.
98      */

99     public void remove () {
100         watch.remove ();
101     }
102     
103     /**
104      * Returns description of problem is this watch can not be evaluated
105      * in current context.
106      *
107      * @return description of problem
108      */

109     public String JavaDoc getExceptionDescription () {
110         return exceptionDescription;
111     }
112
113     /**
114     * Sets string representation of value of this variable.
115     *
116     * @param value string representation of value of this variable.
117     *
118     public void setValue (String expression) throws InvalidExpressionException {
119         // evaluate expression to Value
120         Value value = model.getDebugger ().evaluateIn (expression);
121         // set new value to remote veriable
122         setValue (value);
123         // set new value to this model
124         setInnerValue (value);
125         // refresh tree
126         Object node = nodeRef.get();
127         if (node != null) {
128             model.fireTableValueChangedChanged (node, null);
129         }
130     }
131      */

132     
133     protected void setInnerValue (Value v) {
134         super.setInnerValue (v);
135         exceptionDescription = null;
136     }
137     
138     protected void setValue (Value value) throws InvalidExpressionException {
139         CallStackFrameImpl frame = (CallStackFrameImpl) debugger.
140             getCurrentCallStackFrame ();
141         if (frame == null)
142             throw new InvalidExpressionException ("No curent frame.");
143         LocalVariable local = null;
144         try {
145             local = frame.getStackFrame ().visibleVariableByName
146                 (getExpression ());
147         } catch (AbsentInformationException ex) {
148             throw new InvalidExpressionException ("Can not set value to expression.");
149         }
150         if (local == null)
151             throw new InvalidExpressionException ("Can not set value to expression.");
152         try {
153             frame.getStackFrame ().setValue (local, value);
154         } catch (InvalidTypeException ex) {
155             throw new InvalidExpressionException (ex);
156         } catch (ClassNotLoadedException ex) {
157             throw new InvalidExpressionException (ex);
158         }
159     }
160     
161     void setException (String JavaDoc exceptionDescription) {
162         setInnerValue (null);
163         this.exceptionDescription = exceptionDescription;
164     }
165     
166     boolean isPrimitive () {
167         return !(getInnerValue () instanceof ObjectReference);
168     }
169
170     public JPDAWatchImpl clone() {
171         JPDAWatchImpl clon;
172         if (exceptionDescription == null) {
173             clon = new JPDAWatchImpl(getDebugger(), watch, getJDIValue(), nodeRef.get());
174         } else {
175             clon = new JPDAWatchImpl(getDebugger(), watch, new Exception JavaDoc(exceptionDescription), nodeRef.get());
176         }
177         return clon;
178     }
179     
180 }
181
182
Popular Tags