KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaVarActionFilter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.jdt.debug.core.IJavaArrayType;
19 import org.eclipse.jdt.debug.core.IJavaClassType;
20 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
21 import org.eclipse.jdt.debug.core.IJavaObject;
22 import org.eclipse.jdt.debug.core.IJavaType;
23 import org.eclipse.jdt.debug.core.IJavaVariable;
24 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaStructureErrorValue;
25 import org.eclipse.jdt.internal.debug.core.model.JDINullValue;
26 import org.eclipse.jdt.internal.debug.core.model.JDIPlaceholderValue;
27 import org.eclipse.jdt.internal.debug.core.model.JDIReferenceListVariable;
28 import org.eclipse.jdt.internal.debug.ui.actions.EditVariableLogicalStructureAction;
29 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression;
30 import org.eclipse.ui.IActionFilter;
31
32 import com.sun.jdi.ClassNotLoadedException;
33
34 /**
35  * Provides the action filter for Java and Inspect actions
36  *
37  * @since 3.2
38  */

39 public class JavaVarActionFilter implements IActionFilter {
40
41     /**
42      * The set or primitive types
43      */

44     private static final Set JavaDoc fgPrimitiveTypes = initPrimitiveTypes();
45
46     /**
47      * The predefined set of primitive types
48      * @return the set of predefined types
49      */

50     private static Set JavaDoc initPrimitiveTypes() {
51         HashSet JavaDoc set = new HashSet JavaDoc(8);
52         set.add("short"); //$NON-NLS-1$
53
set.add("int"); //$NON-NLS-1$
54
set.add("long"); //$NON-NLS-1$
55
set.add("float"); //$NON-NLS-1$
56
set.add("double"); //$NON-NLS-1$
57
set.add("boolean"); //$NON-NLS-1$
58
set.add("byte"); //$NON-NLS-1$
59
set.add("char"); //$NON-NLS-1$
60
set.add("null"); //$NON-NLS-1$
61
return set;
62     }
63     
64     /**
65      * Determines if the declared value is the same as the concrete value
66      * @param var the variable to inspect
67      * @return true if the types are the same, false otherwise
68      */

69     protected boolean isDeclaredSameAsConcrete(IJavaVariable var) {
70         try {
71             IValue value = var.getValue();
72             if (value instanceof JDINullValue) {
73                 return false;
74             }
75             return !var.getReferenceTypeName().equals(value.getReferenceTypeName());
76         }
77         catch(DebugException e) {JDIDebugUIPlugin.log(e);}
78         return false;
79     }
80
81     /**
82      * Determines if the passed object is a primitive type or not
83      * @param obj the obj to test
84      * @return true if the object is primitive, false otherwise
85      */

86     protected boolean isPrimitiveType(Object JavaDoc obj) {
87         if(obj instanceof IJavaVariable) {
88             try {
89                 return !fgPrimitiveTypes.contains(removeArray(((IJavaVariable) obj).getReferenceTypeName()));
90             }
91             catch (DebugException e) {
92                 if(!(e.getStatus().getException() instanceof ClassNotLoadedException)) {
93                     JDIDebugUIPlugin.log(e);
94                 }
95                 return false;
96             }
97         }
98         else if(obj instanceof JavaInspectExpression) {
99             try {
100                 JavaInspectExpression exp = (JavaInspectExpression)obj;
101                 IValue value = exp.getValue();
102                 if (value != null) {
103                     return fgPrimitiveTypes.contains(removeArray(value.getReferenceTypeName()));
104                 }
105             } catch (DebugException e) {JDIDebugUIPlugin.log(e);}
106         }
107         return false;
108     }
109     
110     /**
111      * This method returns if the specified object is an array or not
112      * @param object the object to test
113      * @return true if the specified object has the <code>IJavaType</code> of <code>JDIArrayType</code>, false otherwise
114      * @since 3.3
115      */

116     protected boolean isArrayType(Object JavaDoc object) {
117         if(object instanceof IJavaVariable) {
118             try {
119                 IJavaType type = ((IJavaVariable)object).getJavaType();
120                 if(type != null) {
121                     return type instanceof IJavaArrayType;
122                 }
123             }
124             catch (DebugException e) {JDIDebugUIPlugin.log(e);}
125         }
126         return false;
127     }
128     
129     /**
130      * Determines if the ref type of the value is primitive
131      *
132      * @param var the variable to inspect
133      * @return true if the the values ref type is primitive, false otherwise
134      */

135     protected boolean isValuePrimitiveType(IValue value) {
136         try {
137             return !fgPrimitiveTypes.contains(removeArray(value.getReferenceTypeName()));
138         }
139         catch (DebugException e) {JDIDebugUIPlugin.log(e);}
140         return false;
141     }
142     
143     /**
144      * Method removes the array declaration characters to return just the type
145      *
146      * @param typeName the type name we want to strip the array delimiters from
147      * @return the altered type
148      */

149     protected String JavaDoc removeArray(String JavaDoc type) {
150         if (type != null) {
151             int index= type.indexOf('[');
152             if (index > 0) {
153                 return type.substring(0, index);
154             }
155         }
156         return type;
157     }
158     
159     /* (non-Javadoc)
160      * @see org.eclipse.ui.IActionFilter#testAttribute(java.lang.Object, java.lang.String, java.lang.String)
161      */

162     public boolean testAttribute(Object JavaDoc target, String JavaDoc name, String JavaDoc value) {
163         if (target instanceof IJavaVariable) {
164             IJavaVariable var = (IJavaVariable) target;
165             IValue varValue;
166             try {
167                 varValue = var.getValue();
168                 if (name.equals("PrimitiveVariableActionFilter")) { //$NON-NLS-1$
169
if (value.equals("isPrimitive")) { //$NON-NLS-1$
170
return isPrimitiveType(var);
171                     }
172                     else if(value.equals("isArray")) { //$NON-NLS-1$
173
return isArrayType(var);
174                     }
175                     else if (value.equals("isValuePrimitive")) { //$NON-NLS-1$
176
return isValuePrimitiveType(varValue);
177                     }
178                 }
179                 if (name.equals("JavaVariableFilter")) { //$NON-NLS-1$
180
if (value.equals("isInstanceRetrievalAvailable")) { //$NON-NLS-1$
181
return isInstanceRetrievalAvailable(var);
182                     }
183                     if(value.equals("isNullValue")) { //$NON-NLS-1$
184
return varValue instanceof JDINullValue;
185                     }
186                     if (value.equals("isReferenceListVariable")) { //$NON-NLS-1$
187
return var instanceof JDIReferenceListVariable;
188                     }
189                     if (value.equals("isPlaceholderValue")) { //$NON-NLS-1$
190
return varValue instanceof JDIPlaceholderValue;
191                     }
192                 }
193                 else if (name.equals("ConcreteVariableActionFilter") && value.equals("isConcrete")) { //$NON-NLS-1$ //$NON-NLS-2$
194
return isDeclaredSameAsConcrete(var);
195                 }
196                 else if (name.equals("JavaVariableActionFilter")) { //$NON-NLS-1$
197
if(value.equals("instanceFilter")) { //$NON-NLS-1$
198
return !var.isStatic() && (varValue instanceof IJavaObject) && (((IJavaObject)varValue).getJavaType() instanceof IJavaClassType) && ((IJavaDebugTarget)var.getDebugTarget()).supportsInstanceBreakpoints();
199                     }
200                     if(value.equals("isValidField")) { //$NON-NLS-1$
201
return !var.isFinal() & !(var.isFinal() & var.isStatic());
202                     }
203                 }
204                 else if (name.equals("DetailFormatterFilter") & (varValue instanceof IJavaObject)) { //$NON-NLS-1$
205
if(value.equals("isDefined")) { //$NON-NLS-1$
206
return JavaDetailFormattersManager.getDefault().hasAssociatedDetailFormatter(((IJavaObject)varValue).getJavaType());
207                     }
208                     if(value.equals("inInterface")) { //$NON-NLS-1$
209
return JavaDetailFormattersManager.getDefault().hasInterfaceDetailFormatter(((IJavaObject)varValue).getJavaType());
210                     }
211                     if(value.equals("inSuperclass")) { //$NON-NLS-1$
212
return JavaDetailFormattersManager.getDefault().hasSuperclassDetailFormatter(((IJavaObject)varValue).getJavaType());
213                     }
214                 }
215                 else if (name.equals("JavaLogicalStructureFilter") && value.equals("canEditLogicalStructure")) { //$NON-NLS-1$ //$NON-NLS-2$
216
return varValue instanceof JavaStructureErrorValue || EditVariableLogicalStructureAction.getLogicalStructure(varValue) != null;
217                 }
218             } catch (DebugException e) {}
219         }
220         else if (target instanceof JavaInspectExpression) {
221             JavaInspectExpression exp = (JavaInspectExpression) target;
222             if (name.equals("PrimitiveVariableActionFilter") && value.equals("isNotPrimitive")) { //$NON-NLS-1$ //$NON-NLS-2$
223
return !isPrimitiveType(exp);
224             }
225             else if (name.equals("DetailFormatterFilter")) { //$NON-NLS-1$
226
try {
227                     IValue varValue = exp.getValue();
228                     if(varValue instanceof IJavaObject) {
229                         if(value.equals("isDefined")) { //$NON-NLS-1$
230
return JavaDetailFormattersManager.getDefault().hasAssociatedDetailFormatter(((IJavaObject)varValue).getJavaType());
231                         }
232                         if(value.equals("inInterface")) { //$NON-NLS-1$
233
return JavaDetailFormattersManager.getDefault().hasInterfaceDetailFormatter(((IJavaObject)varValue).getJavaType());
234                         }
235                         if(value.equals("inSuperclass")) { //$NON-NLS-1$
236
return JavaDetailFormattersManager.getDefault().hasSuperclassDetailFormatter(((IJavaObject)varValue).getJavaType());
237                         }
238                     }
239                 }
240                 catch (DebugException exception) {}
241             }
242         }
243         return false;
244     }
245
246     /**
247      * Returns whether this variable's VM supports instance/reference information.
248      *
249      * @param var variable
250      * @return whether this variable's VM supports instance/reference information
251      */

252     protected boolean isInstanceRetrievalAvailable(IJavaVariable var) {
253         return ((IJavaDebugTarget)var.getDebugTarget()).supportsInstanceRetrieval() && !(var instanceof JDIReferenceListVariable);
254     }
255 }
Popular Tags