KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIModificationVariable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.model;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.model.IValue;
17 import org.eclipse.jdt.debug.core.IJavaValue;
18
19 import com.sun.jdi.Value;
20 import com.sun.jdi.VirtualMachine;
21  
22 /**
23  * Common functionality for variables that support value modification
24  */

25 public abstract class JDIModificationVariable extends JDIVariable {
26     
27     private final static ArrayList JavaDoc fgValidSignatures = new ArrayList JavaDoc (9);
28     static {
29         fgValidSignatures.add("B"); // byte //$NON-NLS-1$
30
fgValidSignatures.add("C"); // char //$NON-NLS-1$
31
fgValidSignatures.add("D"); // double //$NON-NLS-1$
32
fgValidSignatures.add("F"); // float //$NON-NLS-1$
33
fgValidSignatures.add("I"); // int //$NON-NLS-1$
34
fgValidSignatures.add("J"); // long //$NON-NLS-1$
35
fgValidSignatures.add("S"); // short //$NON-NLS-1$
36
fgValidSignatures.add("Z"); // boolean //$NON-NLS-1$
37
fgValidSignatures.add(jdiStringSignature); // String
38
}
39
40     public JDIModificationVariable(JDIDebugTarget target) {
41         super(target);
42     }
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
46      */

47     public boolean supportsValueModification() {
48         return true;
49     }
50     
51     protected Value generateVMValue(String JavaDoc expression) throws DebugException {
52     
53         String JavaDoc signature= null;
54         Value cValue= getCurrentValue();
55         VirtualMachine vm= getVM();
56         if (vm == null) {
57             requestFailed(JDIDebugModelMessages.JDIModificationVariable_Unable_to_generate_value___VM_disconnected__1, null);
58         }
59         if (cValue == null) {
60             //String with null value
61
signature = jdiStringSignature;
62         } else {
63             signature= cValue.type().signature();
64         }
65         if (signature.length() > 1 && !signature.equals(jdiStringSignature)) {
66             return null;
67         }
68         Value vmValue= null;
69         try {
70             switch (signature.charAt(0)) {
71                 case 'Z' :
72                     String JavaDoc flse= Boolean.FALSE.toString();
73                     String JavaDoc tre= Boolean.TRUE.toString();
74                     if (expression.equals(tre) || expression.equals(flse)) {
75                         boolean booleanValue= Boolean.valueOf(expression).booleanValue();
76                         vmValue= vm.mirrorOf(booleanValue);
77                     }
78                     break;
79                 case 'B' :
80                     byte byteValue= Byte.valueOf(expression).byteValue();
81                     vmValue= vm.mirrorOf(byteValue);
82                     break;
83                 case 'C' :
84                     if (expression.length() == 1) {
85                         char charValue= expression.charAt(0);
86                         vmValue= vm.mirrorOf(charValue);
87                     } else if (expression.length() == 2) {
88                             char charValue;
89                             if (!(expression.charAt(0) == '\\')) {
90                                 return null;
91                             }
92                             switch (expression.charAt(1)) {
93                                 case 'b':
94                                     charValue= '\b';
95                                     break;
96                                 case 'f':
97                                     charValue= '\f';
98                                     break;
99                                 case 'n':
100                                     charValue= '\n';
101                                     break;
102                                 case 'r':
103                                     charValue= '\r';
104                                     break;
105                                 case 't':
106                                     charValue= '\t';
107                                     break;
108                                 case '\'':
109                                     charValue= '\'';
110                                     break;
111                                 case '\"':
112                                     charValue= '\"';
113                                     break;
114                                 case '\\':
115                                     charValue= '\\';
116                                     break;
117                                 default :
118                                     return null;
119                             }
120                         vmValue= vm.mirrorOf(charValue);
121                     }
122                     break;
123                 case 'S' :
124                     short shortValue= Short.valueOf(expression).shortValue();
125                     vmValue= vm.mirrorOf(shortValue);
126                     break;
127                 case 'I' :
128                     int intValue= Integer.valueOf(expression).intValue();
129                     vmValue= vm.mirrorOf(intValue);
130                     break;
131                 case 'J' :
132                     long longValue= Long.valueOf(expression).longValue();
133                     vmValue= vm.mirrorOf(longValue);
134                     break;
135                 case 'F' :
136                     float floatValue= Float.valueOf(expression).floatValue();
137                     vmValue= vm.mirrorOf(floatValue);
138                     break;
139                 case 'D' :
140                     double doubleValue= Double.valueOf(expression).doubleValue();
141                     vmValue= vm.mirrorOf(doubleValue);
142                     break;
143                 case 'L' :
144                     if (expression.equals("null")) { //$NON-NLS-1$
145
vmValue = null;
146                     } else if (expression.equals("\"null\"")) { //$NON-NLS-1$
147
vmValue = vm.mirrorOf("null"); //$NON-NLS-1$
148
} else {
149                         vmValue= vm.mirrorOf(expression);
150                     }
151                     break;
152
153             }
154         } catch (NumberFormatException JavaDoc nfe) {
155             return null;
156         }
157         return vmValue;
158     }
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
162      */

163     public boolean verifyValue(String JavaDoc expression) throws DebugException {
164         IValue value = JDIValue.createValue(getJavaDebugTarget(), generateVMValue(expression));
165         return verifyValue(value);
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
170      */

171     public boolean verifyValue(IValue value) {
172         return value instanceof IJavaValue &&
173             value.getDebugTarget().equals(getDebugTarget());
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
178      */

179     public final void setValue(String JavaDoc expression) throws DebugException {
180         Value value= generateVMValue(expression);
181         setJDIValue(value);
182     }
183
184     /**
185      * Set this variable's value to the given value
186      */

187     protected abstract void setJDIValue(Value value) throws DebugException;
188     
189 }
190
Popular Tags