~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to debuggerjpda/src/org/netbeans/modules/debugger/jpda/models/AbstractVariable.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.debugger.jpda.models;
 
43
 
 
44
import com.sun.jdi.ArrayReference;
 
45
import com.sun.jdi.CharValue;
 
46
import com.sun.jdi.ClassObjectReference;
 
47
import com.sun.jdi.ClassType;
 
48
import com.sun.jdi.Field;
 
49
import com.sun.jdi.ObjectCollectedException;
 
50
import com.sun.jdi.ObjectReference;
 
51
import com.sun.jdi.PrimitiveValue;
 
52
import com.sun.jdi.ReferenceType;
 
53
import com.sun.jdi.StringReference;
 
54
import com.sun.jdi.Value;
 
55
import com.sun.jdi.VoidValue;
 
56
import com.sun.jdi.VMDisconnectedException;
 
57
import java.beans.Customizer;
 
58
import java.beans.PropertyChangeEvent;
 
59
 
 
60
import java.beans.PropertyChangeListener;
 
61
import java.util.HashSet;
 
62
import java.util.Set;
 
63
 
 
64
import org.netbeans.api.debugger.jpda.InvalidExpressionException;
 
65
import org.netbeans.api.debugger.jpda.JPDAClassType;
 
66
import org.netbeans.api.debugger.jpda.Variable;
 
67
import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
 
68
import org.netbeans.modules.debugger.jpda.expr.JDIVariable;
 
69
import org.openide.util.NbBundle;
 
70
 
 
71
 
 
72
/**
 
73
 * @author   Jan Jancura
 
74
 */
 
75
class AbstractVariable implements JDIVariable, Customizer, Cloneable {
 
76
    // Customized for add/removePropertyChangeListener
 
77
    // Cloneable for fixed watches
 
78
    
 
79
    private Value   value;
 
80
    private JPDADebuggerImpl debugger;
 
81
    private String          id;
 
82
    
 
83
    private Set<PropertyChangeListener> listeners = new HashSet<PropertyChangeListener>();
 
84
 
 
85
    
 
86
    AbstractVariable (
 
87
        JPDADebuggerImpl debugger,
 
88
        Value value,
 
89
        String id
 
90
    ) {
 
91
        this.debugger = debugger;
 
92
        this.value = value;
 
93
        this.id = id;
 
94
        if (this.id == null)
 
95
            this.id = Integer.toString(super.hashCode());
 
96
    }
 
97
 
 
98
    
 
99
    // public interface ........................................................
 
100
    
 
101
    /**
 
102
    * Returns string representation of type of this variable.
 
103
    *
 
104
    * @return string representation of type of this variable.
 
105
    */
 
106
    public String getValue () {
 
107
        Value v = getInnerValue ();
 
108
        return getValue(v);
 
109
    }
 
110
    
 
111
    static String getValue (Value v) {
 
112
        if (v == null) return "null";
 
113
        if (v instanceof VoidValue) return "void";
 
114
        if (v instanceof CharValue)
 
115
            return "\'" + v.toString () + "\'";
 
116
        if (v instanceof PrimitiveValue)
 
117
            return v.toString ();
 
118
        if (v instanceof StringReference)
 
119
            return "\"" +
 
120
                ((StringReference) v).value ()
 
121
                + "\"";
 
122
        if (v instanceof ClassObjectReference)
 
123
            return "class " + ((ClassObjectReference) v).reflectedType ().name ();
 
124
        if (v instanceof ArrayReference)
 
125
            return "#" + ((ArrayReference) v).uniqueID () + 
 
126
                "(length=" + ((ArrayReference) v).length () + ")";
 
127
        return "#" + ((ObjectReference) v).uniqueID ();
 
128
    }
 
129
 
 
130
    /**
 
131
    * Sets string representation of value of this variable.
 
132
    *
 
133
    * @param value string representation of value of this variable.
 
134
    */
 
135
    public void setValue (String expression) throws InvalidExpressionException {
 
136
        String oldValue = getValue();
 
137
        if (expression.equals(oldValue)) {
 
138
            return ; // Do nothing, since the values are identical
 
139
        }
 
140
        Value value;
 
141
        Value oldV = getInnerValue();
 
142
        if (oldV instanceof CharValue && expression.startsWith("'") && expression.endsWith("'") && expression.length() > 1) {
 
143
            value = oldV.virtualMachine().mirrorOf(expression.charAt(1));
 
144
        } else if ((oldV instanceof StringReference || oldV == null) &&
 
145
                   expression.startsWith("\"") && expression.endsWith("\"") && expression.length() > 1) {
 
146
            value = debugger.getVirtualMachine().mirrorOf(expression.substring(1, expression.length() - 1));
 
147
        } else if (oldV instanceof ObjectReference &&
 
148
                   ((ObjectReference) oldV).referenceType() instanceof ClassType &&
 
149
                   ((ClassType) ((ObjectReference) oldV).referenceType()).isEnum()) {
 
150
            ClassType enumType = (ClassType) ((ObjectReference) oldV).referenceType();
 
151
            Field enumValue = enumType.fieldByName(expression);
 
152
            if (enumValue != null) {
 
153
                value = enumType.getValue(enumValue);
 
154
            } else {
 
155
                throw new InvalidExpressionException(expression);
 
156
            }
 
157
        } else if ("null".equals(expression)) {
 
158
            value = null;
 
159
        } else {
 
160
            // evaluate expression to Value
 
161
            Value evaluatedValue = debugger.evaluateIn (expression);
 
162
            if (!(evaluatedValue instanceof PrimitiveValue)) {
 
163
                throw new InvalidExpressionException(expression);
 
164
            }
 
165
            value = (PrimitiveValue) evaluatedValue;
 
166
        }
 
167
        // set new value to remote veriable
 
168
        setValue (value);
 
169
        // set new value to this model
 
170
        setInnerValue (value);
 
171
        // refresh tree
 
172
        PropertyChangeEvent evt = new PropertyChangeEvent(this, "value", null, value);
 
173
        Object[] ls;
 
174
        synchronized (listeners) {
 
175
            ls = listeners.toArray();
 
176
        }
 
177
        for (int i = 0; i < ls.length; i++) {
 
178
            ((PropertyChangeListener) ls[i]).propertyChange(evt);
 
179
        }
 
180
        //pchs.firePropertyChange("value", null, value);
 
181
        //getModel ().fireTableValueChangedChanged (this, null);
 
182
    }
 
183
    
 
184
    /**
 
185
     * Override, but do not call directly!
 
186
     */
 
187
    protected void setValue (Value value) throws InvalidExpressionException {
 
188
        throw new InternalError ();
 
189
    }
 
190
    
 
191
    public void setObject(Object bean) {
 
192
        try {
 
193
            if (bean instanceof String) {
 
194
                setValue((String) bean);
 
195
            //} else if (bean instanceof Value) {
 
196
            //    setValue((Value) bean); -- do not call directly
 
197
            } else {
 
198
                throw new IllegalArgumentException(""+bean);
 
199
            }
 
200
        } catch (InvalidExpressionException ieex) {
 
201
            IllegalArgumentException iaex = new IllegalArgumentException(ieex.getLocalizedMessage());
 
202
            iaex.initCause(ieex);
 
203
            throw iaex;
 
204
        }
 
205
    }
 
206
 
 
207
    /**
 
208
     * Declared type of this local.
 
209
     *
 
210
     * @return declared type of this local
 
211
     */
 
212
    public String getType () {
 
213
        if (getInnerValue () == null) return "";
 
214
        try {
 
215
            return this.getInnerValue().type().name ();
 
216
        } catch (VMDisconnectedException vmdex) {
 
217
            // The session is gone.
 
218
            return NbBundle.getMessage(AbstractVariable.class, "MSG_Disconnected");
 
219
        } catch (ObjectCollectedException ocex) {
 
220
            // The object is gone.
 
221
            return NbBundle.getMessage(AbstractVariable.class, "MSG_ObjCollected");
 
222
        }
 
223
    }
 
224
    
 
225
    public JPDAClassType getClassType() {
 
226
        Value value = getInnerValue();
 
227
        if (value == null) return null;
 
228
        com.sun.jdi.Type type = value.type();
 
229
        if (type instanceof ReferenceType) {
 
230
            return new JPDAClassTypeImpl(debugger, (ReferenceType) type);
 
231
        } else {
 
232
            return null;
 
233
        }
 
234
    }
 
235
    
 
236
    public boolean equals (Object o) {
 
237
        return  (o instanceof AbstractVariable) &&
 
238
                (id.equals (((AbstractVariable) o).id));
 
239
    }
 
240
    
 
241
    public int hashCode () {
 
242
        return id.hashCode ();
 
243
    }
 
244
 
 
245
    
 
246
    // other methods............................................................
 
247
    
 
248
    protected Value getInnerValue () {
 
249
        return value;
 
250
    }
 
251
    
 
252
    protected void setInnerValue (Value v) {
 
253
        value = v;
 
254
    }
 
255
    
 
256
    public Value getJDIValue() {
 
257
        return value;
 
258
    }
 
259
    
 
260
    protected final JPDADebuggerImpl getDebugger() {
 
261
        return debugger;
 
262
    }
 
263
    
 
264
    protected final String getID () {
 
265
        return id;
 
266
    }
 
267
    
 
268
    private int cloneNumber = 1;
 
269
    
 
270
    public Variable clone() {
 
271
        AbstractVariable clon = new AbstractVariable(debugger, value, id + "_clone"+(cloneNumber++));
 
272
        return clon;
 
273
    }
 
274
    
 
275
    public void addPropertyChangeListener(PropertyChangeListener l) {
 
276
        listeners.add(l);
 
277
    }
 
278
    
 
279
    public void removePropertyChangeListener(PropertyChangeListener l) {
 
280
        listeners.remove(l);
 
281
    }
 
282
    
 
283
    public String toString () {
 
284
        return "Variable ";
 
285
    }
 
286
    
 
287
    /* Uncomment when needed. Was used to create "readable" String and Char values.
 
288
    private static String convertToStringInitializer (String s) {
 
289
        StringBuffer sb = new StringBuffer ();
 
290
        int i, k = s.length ();
 
291
        for (i = 0; i < k; i++)
 
292
            switch (s.charAt (i)) {
 
293
                case '\b':
 
294
                    sb.append ("\\b");
 
295
                    break;
 
296
                case '\f':
 
297
                    sb.append ("\\f");
 
298
                    break;
 
299
                case '\\':
 
300
                    sb.append ("\\\\");
 
301
                    break;
 
302
                case '\t':
 
303
                    sb.append ("\\t");
 
304
                    break;
 
305
                case '\r':
 
306
                    sb.append ("\\r");
 
307
                    break;
 
308
                case '\n':
 
309
                    sb.append ("\\n");
 
310
                    break;
 
311
                case '\"':
 
312
                    sb.append ("\\\"");
 
313
                    break;
 
314
                default:
 
315
                    sb.append (s.charAt (i));
 
316
            }
 
317
        return sb.toString();
 
318
    }
 
319
    
 
320
    private static String convertToCharInitializer (String s) {
 
321
        StringBuffer sb = new StringBuffer ();
 
322
        int i, k = s.length ();
 
323
        for (i = 0; i < k; i++)
 
324
            switch (s.charAt (i)) {
 
325
                case '\b':
 
326
                    sb.append ("\\b");
 
327
                    break;
 
328
                case '\f':
 
329
                    sb.append ("\\f");
 
330
                    break;
 
331
                case '\\':
 
332
                    sb.append ("\\\\");
 
333
                    break;
 
334
                case '\t':
 
335
                    sb.append ("\\t");
 
336
                    break;
 
337
                case '\r':
 
338
                    sb.append ("\\r");
 
339
                    break;
 
340
                case '\n':
 
341
                    sb.append ("\\n");
 
342
                    break;
 
343
                case '\'':
 
344
                    sb.append ("\\\'");
 
345
                    break;
 
346
                default:
 
347
                    sb.append (s.charAt (i));
 
348
            }
 
349
        return sb.toString();
 
350
    }
 
351
     */
 
352
    
 
353
}
 
354