1 11 package org.eclipse.jdt.internal.debug.core.logicalstructures; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.StringTokenizer ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IConfigurationElement; 25 import org.eclipse.core.runtime.IExtensionPoint; 26 import org.eclipse.core.runtime.Platform; 27 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener; 28 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 29 import org.eclipse.debug.core.DebugException; 30 import org.eclipse.debug.core.ILogicalStructureProvider; 31 import org.eclipse.debug.core.ILogicalStructureType; 32 import org.eclipse.debug.core.model.IValue; 33 import org.eclipse.jdt.debug.core.IJavaClassType; 34 import org.eclipse.jdt.debug.core.IJavaInterfaceType; 35 import org.eclipse.jdt.debug.core.IJavaObject; 36 import org.eclipse.jdt.debug.core.IJavaType; 37 import org.eclipse.jdt.debug.core.JDIDebugModel; 38 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 39 40 public class JavaLogicalStructures implements ILogicalStructureProvider { 41 42 static final char IS_SUBTYPE_TRUE= 'T'; 44 static final char IS_SUBTYPE_FALSE= 'F'; 45 46 49 private static Map fJavaLogicalStructureMap; 50 51 54 private static List fPluginContributedJavaLogicalStructures; 55 56 59 private static List fUserDefinedJavaLogicalStructures; 60 61 64 private static Set fListeners= new HashSet (); 65 66 71 private static final String PREF_JAVA_LOGICAL_STRUCTURES= JDIDebugModel.getPluginIdentifier() + ".PREF_JAVA_LOGICAL_STRUCTURES"; 73 76 static class PreferenceListener implements IPropertyChangeListener { 77 78 81 public void propertyChange(PropertyChangeEvent event) { 82 if (PREF_JAVA_LOGICAL_STRUCTURES.equals(event.getProperty())) { 83 initUserDefinedJavaLogicalStructures(); 84 initJavaLogicalStructureMap(); 85 Iterator iter = fListeners.iterator(); 86 while (iter.hasNext()) { 87 ((IJavaStructuresListener) iter.next()).logicalStructuresChanged(); 88 } 89 } 90 } 91 92 } 93 94 98 static { 99 initPluginContributedJavaLogicalStructure(); 100 initUserDefinedJavaLogicalStructures(); 101 initJavaLogicalStructureMap(); 102 JDIDebugPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(new PreferenceListener()); 103 } 104 105 private static void initJavaLogicalStructureMap() { 106 fJavaLogicalStructureMap= new HashMap (); 107 addAllLogicalStructures(fPluginContributedJavaLogicalStructures); 108 addAllLogicalStructures(fUserDefinedJavaLogicalStructures); 109 } 110 111 114 private static void addAllLogicalStructures(List pluginContributedJavaLogicalStructures) { 115 for (Iterator iter= pluginContributedJavaLogicalStructures.iterator(); iter.hasNext();) { 116 addLogicalStructure((JavaLogicalStructure) iter.next()); 117 } 118 } 119 120 123 private static void addLogicalStructure(JavaLogicalStructure structure) { 124 String typeName= structure.getQualifiedTypeName(); 125 List logicalStructure= (List )fJavaLogicalStructureMap.get(typeName); 126 if (logicalStructure == null) { 127 logicalStructure= new ArrayList (); 128 fJavaLogicalStructureMap.put(typeName, logicalStructure); 129 } 130 logicalStructure.add(structure); 131 } 132 133 136 private static void initPluginContributedJavaLogicalStructure() { 137 fPluginContributedJavaLogicalStructures= new ArrayList (); 138 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(JDIDebugPlugin.getUniqueIdentifier(), JDIDebugPlugin.EXTENSION_POINT_JAVA_LOGICAL_STRUCTURES); 139 IConfigurationElement[] javaLogicalStructureElements= extensionPoint.getConfigurationElements(); 140 for (int i= 0; i < javaLogicalStructureElements.length; i++) { 141 try { 142 fPluginContributedJavaLogicalStructures.add(new JavaLogicalStructure(javaLogicalStructureElements[i])); 143 } catch (CoreException e) { 144 JDIDebugPlugin.log(e); 145 } 146 } 147 } 148 149 152 private static void initUserDefinedJavaLogicalStructures() { 153 fUserDefinedJavaLogicalStructures= new ArrayList (); 154 String logicalStructuresString= JDIDebugModel.getPreferences().getString(PREF_JAVA_LOGICAL_STRUCTURES); 155 StringTokenizer tokenizer= new StringTokenizer (logicalStructuresString, "\0", true); while (tokenizer.hasMoreTokens()) { 157 String type= tokenizer.nextToken(); 158 tokenizer.nextToken(); 159 String description= tokenizer.nextToken(); 160 tokenizer.nextToken(); 161 String isSubtypeValue= tokenizer.nextToken(); 162 boolean isSubtype= isSubtypeValue.charAt(0) == IS_SUBTYPE_TRUE; 163 tokenizer.nextToken(); 164 String value= tokenizer.nextToken(); 165 if (value.charAt(0) == '\0') { 166 value= null; 167 } else { 168 tokenizer.nextToken(); 169 } 170 String variablesCounterValue= tokenizer.nextToken(); 171 int variablesCounter= Integer.parseInt(variablesCounterValue); 172 tokenizer.nextToken(); 173 String [][] variables= new String [variablesCounter][2]; 174 for (int i= 0; i < variablesCounter; i++) { 175 variables[i][0]= tokenizer.nextToken(); 176 tokenizer.nextToken(); 177 variables[i][1]= tokenizer.nextToken(); 178 tokenizer.nextToken(); 179 } 180 fUserDefinedJavaLogicalStructures.add(new JavaLogicalStructure(type, isSubtype, value, description, variables)); 181 } 182 } 183 184 187 public static void saveUserDefinedJavaLogicalStructures() { 188 StringBuffer logicalStructuresString= new StringBuffer (); 189 for (Iterator iter= fUserDefinedJavaLogicalStructures.iterator(); iter.hasNext();) { 190 JavaLogicalStructure logicalStructure= (JavaLogicalStructure) iter.next(); 191 logicalStructuresString.append(logicalStructure.getQualifiedTypeName()).append('\0'); 192 logicalStructuresString.append(logicalStructure.getDescription()).append('\0'); 193 logicalStructuresString.append(logicalStructure.isSubtypes() ? IS_SUBTYPE_TRUE : IS_SUBTYPE_FALSE).append('\0'); 194 String value= logicalStructure.getValue(); 195 if (value != null) { 196 logicalStructuresString.append(value); 197 } 198 logicalStructuresString.append('\0'); 199 String [][] variables= logicalStructure.getVariables(); 200 logicalStructuresString.append(variables.length).append('\0'); 201 for (int i= 0; i < variables.length; i++) { 202 String [] strings= variables[i]; 203 logicalStructuresString.append(strings[0]).append('\0'); 204 logicalStructuresString.append(strings[1]).append('\0'); 205 } 206 } 207 JDIDebugModel.getPreferences().setValue(PREF_JAVA_LOGICAL_STRUCTURES, logicalStructuresString.toString()); 208 } 209 210 213 public static JavaLogicalStructure[] getJavaLogicalStructures() { 214 JavaLogicalStructure[] logicalStructures= new JavaLogicalStructure[fPluginContributedJavaLogicalStructures.size() + fUserDefinedJavaLogicalStructures.size()]; 215 int i= 0; 216 for (Iterator iter= fPluginContributedJavaLogicalStructures.iterator(); iter.hasNext();) { 217 logicalStructures[i++]= (JavaLogicalStructure) iter.next(); 218 } 219 for (Iterator iter= fUserDefinedJavaLogicalStructures.iterator(); iter.hasNext();) { 220 logicalStructures[i++]= (JavaLogicalStructure) iter.next(); 221 } 222 return logicalStructures; 223 } 224 225 228 public static void setUserDefinedJavaLogicalStructures(JavaLogicalStructure[] logicalStructures) { 229 fUserDefinedJavaLogicalStructures= Arrays.asList(logicalStructures); 230 saveUserDefinedJavaLogicalStructures(); 231 } 232 233 public static void addStructuresListener(IJavaStructuresListener listener) { 234 fListeners.add(listener); 235 } 236 237 public static void removeStructuresListener(IJavaStructuresListener listener) { 238 fListeners.remove(listener); 239 } 240 241 public ILogicalStructureType[] getLogicalStructureTypes(IValue value) { 242 if (!(value instanceof IJavaObject)) { 243 return new ILogicalStructureType[0]; 244 } 245 IJavaObject javaValue= (IJavaObject) value; 246 List logicalStructures= new ArrayList (); 247 try { 248 IJavaType type= javaValue.getJavaType(); 249 if (!(type instanceof IJavaClassType)) { 250 return new ILogicalStructureType[0]; 251 } 252 IJavaClassType classType= (IJavaClassType) type; 253 List list= (List )fJavaLogicalStructureMap.get(classType.getName()); 254 if (list != null) { 255 logicalStructures.addAll(list); 256 } 257 IJavaClassType superClass= classType.getSuperclass(); 258 while (superClass != null) { 259 addIfIsSubtype(logicalStructures, (List )fJavaLogicalStructureMap.get(superClass.getName())); 260 superClass= superClass.getSuperclass(); 261 } 262 IJavaInterfaceType[] superInterfaces= classType.getAllInterfaces(); 263 for (int i= 0; i < superInterfaces.length; i++) { 264 addIfIsSubtype(logicalStructures, (List )fJavaLogicalStructureMap.get(superInterfaces[i].getName())); 265 } 266 } catch (DebugException e) { 267 JDIDebugPlugin.log(e); 268 return new ILogicalStructureType[0]; 269 } 270 return (ILogicalStructureType[]) logicalStructures.toArray(new ILogicalStructureType[logicalStructures.size()]); 271 } 272 273 private void addIfIsSubtype(List logicalStructures, List list) { 274 if (list == null) { 275 return; 276 } 277 for (Iterator iter= list.iterator(); iter.hasNext();) { 278 JavaLogicalStructure logicalStructure= (JavaLogicalStructure) iter.next(); 279 if (logicalStructure.isSubtypes()) { 280 logicalStructures.add(logicalStructure); 281 } 282 } 283 } 284 285 } 286 | Popular Tags |