KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassNotLoadedException;
23 import com.sun.jdi.ClassType;
24 import com.sun.jdi.Field;
25 import com.sun.jdi.InvalidTypeException;
26 import com.sun.jdi.ObjectReference;
27 import com.sun.jdi.ReferenceType;
28 import com.sun.jdi.Value;
29 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
30 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
31
32
33 /**
34  * @author Jan Jancura
35  */

36 class FieldVariable extends AbstractVariable implements
37 org.netbeans.api.debugger.jpda.Field {
38
39     protected Field field;
40     //private String className;
41
private ObjectReference objectReference;
42     private String JavaDoc genericSignature;
43     
44
45     FieldVariable (
46         JPDADebuggerImpl debugger,
47         Value value,
48     // String className,
49
Field field,
50         String JavaDoc parentID,
51         ObjectReference objectReference
52     ) {
53         super (
54             debugger,
55             value,
56             parentID + '.' + field.name () +
57                 (value instanceof ObjectReference ? "^" : "")
58         );
59         this.field = field;
60         //this.className = className;
61
this.objectReference = objectReference;
62     }
63
64     FieldVariable (
65         JPDADebuggerImpl debugger,
66         Value value,
67        // String className,
68
Field field,
69         String JavaDoc parentID,
70         String JavaDoc genericSignature
71     ) {
72         super(debugger, value, genericSignature, parentID + '.' + field.name () +
73                 (value instanceof ObjectReference ? "^" : ""));
74         this.field = field;
75         this.genericSignature = genericSignature;
76        // this.className = className;
77
}
78     
79     // LocalVariable impl.......................................................
80

81
82     /**
83     * Returns string representation of type of this variable.
84     *
85     * @return string representation of type of this variable.
86     */

87     public String JavaDoc getName () {
88         return field.name ();
89     }
90
91     /**
92      * Returns name of enclosing class.
93      *
94      * @return name of enclosing class
95      */

96     public String JavaDoc getClassName () {
97         return field.declaringType ().name (); //className;
98
}
99
100     /**
101     * Returns string representation of type of this variable.
102     *
103     * @return string representation of type of this variable.
104     */

105     public String JavaDoc getDeclaredType () {
106         return field.typeName ();
107     }
108
109     /**
110      * Returns <code>true</code> for static fields.
111      *
112      * @return <code>true</code> for static fields
113      */

114     public boolean isStatic () {
115         return field.isStatic ();
116     }
117     
118     protected void setValue (Value value) throws InvalidExpressionException {
119         try {
120             boolean set = false;
121             if (objectReference != null) {
122                 objectReference.setValue (field, value);
123                 set = true;
124             } else {
125                 ReferenceType rt = field.declaringType();
126                 if (rt instanceof ClassType) {
127                     ClassType ct = (ClassType) rt;
128                     ct.setValue(field, value);
129                     set = true;
130                 }
131             }
132             if (!set) {
133                 throw new InvalidExpressionException(field.toString());
134             }
135         } catch (InvalidTypeException ex) {
136             throw new InvalidExpressionException (ex);
137         } catch (ClassNotLoadedException ex) {
138             throw new InvalidExpressionException (ex);
139         }
140     }
141
142     public FieldVariable clone() {
143         FieldVariable clon;
144         if (genericSignature == null) {
145             clon = new FieldVariable(getDebugger(), getJDIValue(), field,
146                     getID().substring(0, getID().length() - ("." + field.name() + (getJDIValue() instanceof ObjectReference ? "^" : "")).length()),
147                     objectReference);
148         } else {
149             clon = new FieldVariable(getDebugger(), getJDIValue(), field,
150                     getID().substring(0, getID().length() - ("." + field.name() + (getJDIValue() instanceof ObjectReference ? "^" : "")).length()),
151                     genericSignature);
152         }
153         return clon;
154     }
155
156     // other methods ...........................................................
157

158     public String JavaDoc toString () {
159         return "FieldVariable " + field.name ();
160     }
161 }
162
163
Popular Tags