001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.jpda.models;
043:
044: import com.sun.jdi.AbsentInformationException;
045: import com.sun.jdi.ClassNotLoadedException;
046: import com.sun.jdi.Field;
047: import com.sun.jdi.InvalidTypeException;
048: import com.sun.jdi.ObjectReference;
049: import com.sun.jdi.PrimitiveValue;
050: import com.sun.jdi.Value;
051: import org.netbeans.api.debugger.Watch;
052: import org.netbeans.api.debugger.jpda.InvalidExpressionException;
053: import org.netbeans.api.debugger.jpda.JPDAWatch;
054: import org.netbeans.api.debugger.jpda.LocalVariable;
055: import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
056:
057: /**
058: * Represents watch in JPDA debugger.
059: *
060: * @author Jan Jancura
061: */
062:
063: class JPDAWatchImpl extends AbstractVariable implements JPDAWatch {
064:
065: private JPDADebuggerImpl debugger;
066: private Watch watch;
067: private String exceptionDescription;
068: private java.lang.ref.Reference<Object> nodeRef;
069:
070: JPDAWatchImpl(JPDADebuggerImpl debugger, Watch watch,
071: PrimitiveValue v, Object node) {
072: super (debugger, v, "" + watch
073: + (v instanceof ObjectReference ? "^" : ""));
074: this .debugger = debugger;
075: this .watch = watch;
076: this .nodeRef = new java.lang.ref.WeakReference<Object>(node);
077: }
078:
079: JPDAWatchImpl(JPDADebuggerImpl debugger, Watch watch,
080: Exception exception, Object node) {
081: super (debugger, null, "" + watch);
082: this .debugger = debugger;
083: this .watch = watch;
084: this .exceptionDescription = exception.getLocalizedMessage();
085: if (exceptionDescription == null)
086: exceptionDescription = exception.getMessage();
087: this .nodeRef = new java.lang.ref.WeakReference<Object>(node);
088: }
089:
090: /**
091: * Watched expression.
092: *
093: * @return watched expression
094: */
095: public String getExpression() {
096: return watch.getExpression();
097: }
098:
099: /**
100: * Sets watched expression.
101: *
102: * @param expression a expression to be watched
103: */
104: public void setExpression(String expression) {
105: watch.setExpression(expression);
106: }
107:
108: /**
109: * Remove the watch from the list of all watches in the system.
110: */
111: public void remove() {
112: watch.remove();
113: }
114:
115: /**
116: * Returns description of problem is this watch can not be evaluated
117: * in current context.
118: *
119: * @return description of problem
120: */
121: public String getExceptionDescription() {
122: return exceptionDescription;
123: }
124:
125: /**
126: * Sets string representation of value of this variable.
127: *
128: * @param value string representation of value of this variable.
129: *
130: public void setValue (String expression) throws InvalidExpressionException {
131: // evaluate expression to Value
132: Value value = model.getDebugger ().evaluateIn (expression);
133: // set new value to remote veriable
134: setValue (value);
135: // set new value to this model
136: setInnerValue (value);
137: // refresh tree
138: Object node = nodeRef.get();
139: if (node != null) {
140: model.fireTableValueChangedChanged (node, null);
141: }
142: }
143: */
144:
145: protected void setInnerValue(Value v) {
146: super .setInnerValue(v);
147: exceptionDescription = null;
148: }
149:
150: protected void setValue(Value value)
151: throws InvalidExpressionException {
152: CallStackFrameImpl frame = (CallStackFrameImpl) debugger
153: .getCurrentCallStackFrame();
154: if (frame == null)
155: throw new InvalidExpressionException("No curent frame.");
156: LocalVariable local = null;
157: try {
158: local = frame.getLocalVariable(getExpression());
159: } catch (AbsentInformationException ex) {
160: throw new InvalidExpressionException(
161: "Can not set value to expression.");
162: }
163: if (local != null) {
164: if (local instanceof Local) {
165: ((Local) local).setValue(value);
166: } else {
167: ((ObjectLocalVariable) local).setValue(value);
168: }
169: return;
170: }
171: // try to set as a field
172: ObjectReference this Object = frame.getStackFrame().this Object();
173: if (this Object == null)
174: throw new InvalidExpressionException(
175: "Can not set value to expression.");
176: Field field = this Object.referenceType().fieldByName(
177: getExpression());
178: if (field == null)
179: throw new InvalidExpressionException(
180: "Can not set value to expression.");
181: try {
182: this Object.setValue(field, value);
183: } catch (InvalidTypeException ex) {
184: throw new InvalidExpressionException(ex);
185: } catch (ClassNotLoadedException ex) {
186: throw new InvalidExpressionException(ex);
187: }
188: }
189:
190: public String getToStringValue() throws InvalidExpressionException {
191: return AbstractObjectVariable.getToStringValue(getInnerValue(),
192: getDebugger());
193: }
194:
195: void setException(String exceptionDescription) {
196: setInnerValue(null);
197: this .exceptionDescription = exceptionDescription;
198: }
199:
200: boolean isPrimitive() {
201: return !(getInnerValue() instanceof ObjectReference);
202: }
203:
204: public JPDAWatchImpl clone() {
205: JPDAWatchImpl clon;
206: if (exceptionDescription == null) {
207: clon = new JPDAWatchImpl(getDebugger(), watch,
208: (PrimitiveValue) getJDIValue(), nodeRef.get());
209: } else {
210: clon = new JPDAWatchImpl(getDebugger(), watch,
211: new Exception(exceptionDescription), nodeRef.get());
212: }
213: return clon;
214: }
215:
216: }
|