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.ClassNotLoadedException;
045: import com.sun.jdi.ClassType;
046: import com.sun.jdi.Field;
047: import com.sun.jdi.InvalidTypeException;
048: import com.sun.jdi.ObjectReference;
049: import com.sun.jdi.ReferenceType;
050: import com.sun.jdi.Value;
051:
052: import org.netbeans.api.debugger.jpda.InvalidExpressionException;
053: import org.netbeans.api.debugger.jpda.JPDAClassType;
054: import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
055:
056: /**
057: * @author Jan Jancura
058: */
059: class ObjectFieldVariable extends AbstractObjectVariable implements
060: org.netbeans.api.debugger.jpda.Field {
061:
062: protected Field field;
063: private ObjectReference objectReference;
064: private String genericSignature;
065:
066: ObjectFieldVariable(JPDADebuggerImpl debugger,
067: ObjectReference value,
068: //String className,
069: Field field, String parentID,
070: ObjectReference objectReference) {
071: super (debugger, value, parentID + '.' + field.name() + "^");
072: this .field = field;
073: //this.className = className;
074: this .objectReference = objectReference;
075: }
076:
077: ObjectFieldVariable(JPDADebuggerImpl debugger,
078: ObjectReference value,
079: //String className,
080: Field field, String parentID, String genericSignature,
081: ObjectReference objectReference) {
082: this (debugger, value, field, parentID, objectReference);
083: this .genericSignature = genericSignature;
084: }
085:
086: /**
087: * Returns string representation of type of this variable.
088: *
089: * @return string representation of type of this variable.
090: */
091: public String getName() {
092: return field.name();
093: }
094:
095: /**
096: * Returns name of enclosing class.
097: *
098: * @return name of enclosing class
099: */
100: public String getClassName() {
101: return field.declaringType().name(); //className;
102: }
103:
104: public JPDAClassType getDeclaringClass() {
105: return new JPDAClassTypeImpl(getDebugger(),
106: (ReferenceType) objectReference.type());
107: }
108:
109: /**
110: * Returns string representation of type of this variable.
111: *
112: * @return string representation of type of this variable.
113: */
114: public String getDeclaredType() {
115: return field.typeName();
116: }
117:
118: public JPDAClassType getClassType() {
119: Value value = getInnerValue();
120: if (value != null) {
121: return super .getClassType();
122: }
123: try {
124: com.sun.jdi.Type type = field.type();
125: if (type instanceof ReferenceType) {
126: return new JPDAClassTypeImpl(getDebugger(),
127: (ReferenceType) type);
128: } else {
129: return null;
130: }
131: } catch (ClassNotLoadedException cnlex) {
132: return null;
133: }
134: }
135:
136: /**
137: * Returns <code>true</code> for static fields.
138: *
139: * @return <code>true</code> for static fields
140: */
141: public boolean isStatic() {
142: return field.isStatic();
143: }
144:
145: protected void setValue(Value value)
146: throws InvalidExpressionException {
147: try {
148: boolean set = false;
149: if (objectReference != null) {
150: objectReference.setValue(field, value);
151: set = true;
152: } else {
153: ReferenceType rt = field.declaringType();
154: if (rt instanceof ClassType) {
155: ClassType ct = (ClassType) rt;
156: ct.setValue(field, value);
157: set = true;
158: }
159: }
160: if (!set) {
161: throw new InvalidExpressionException(field.toString());
162: }
163: } catch (InvalidTypeException ex) {
164: throw new InvalidExpressionException(ex);
165: } catch (ClassNotLoadedException ex) {
166: throw new InvalidExpressionException(ex);
167: }
168: }
169:
170: public ObjectFieldVariable clone() {
171: return new ObjectFieldVariable(
172: getDebugger(),
173: (ObjectReference) getJDIValue(),
174: field,
175: getID()
176: .substring(
177: 0,
178: getID().length()
179: - ("." + field.name() + (getJDIValue() instanceof ObjectReference ? "^"
180: : "")).length()),
181: genericSignature, objectReference);
182: }
183:
184: // other methods ...........................................................
185:
186: public String toString() {
187: return "ObjectFieldVariable " + field.name();
188: }
189: }
|