1 11 package org.eclipse.ui.internal.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IExtensionPoint; 20 import org.eclipse.core.runtime.Platform; 21 22 import org.eclipse.ui.editors.text.EditorsUI; 23 24 25 31 public final class AnnotationTypeHierarchy { 32 33 private Map fTypeMap; 34 private Map fTypesCache= new HashMap (); 35 36 public AnnotationTypeHierarchy() { 37 } 38 39 public AnnotationType getAnnotationType(String typeName) { 40 AnnotationType type= (AnnotationType) fTypesCache.get(typeName); 41 if (type == null) { 42 String [] superTypes= computeSuperTypes(typeName); 43 type= new AnnotationType(typeName, superTypes); 44 fTypesCache.put(typeName, type); 45 } 46 return type; 47 } 48 49 public boolean isSubtype(String superType, String subtypeCandidate) { 50 AnnotationType type= getAnnotationType(subtypeCandidate); 51 return type.isSubtype(superType); 52 } 53 54 private String [] computeSuperTypes(String typeName) { 55 ArrayList types= new ArrayList (); 56 append(types, getDirectSuperType(typeName)); 57 int index= 0; 58 while (index < types.size()) { 59 String type= (String ) types.get(index++); 60 append(types, getDirectSuperType(type)); 61 } 62 63 String [] superTypes= new String [types.size()]; 64 types.toArray(superTypes); 65 return superTypes; 66 } 67 68 private String getDirectSuperType(String typeName) { 69 return (String ) getTypeMap().get(typeName); 70 } 71 72 private void append(List list, String string) { 73 if (string == null || string.trim().length() == 0) 74 return; 75 76 if (!list.contains(string)) 77 list.add(string); 78 } 79 80 private Map getTypeMap() { 81 if (fTypeMap == null) 82 fTypeMap= readTypes(); 83 return fTypeMap; 84 } 85 86 private Map readTypes() { 87 HashMap allTypes= new HashMap (); 88 89 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, "annotationTypes"); if (extensionPoint != null) { 91 IConfigurationElement[] elements= extensionPoint.getConfigurationElements(); 92 for (int i= 0; i < elements.length; i++) { 93 IConfigurationElement element= elements[i]; 94 95 String name= element.getAttribute("name"); if (name == null || name.trim().length() == 0) 97 continue; 98 99 String parent= element.getAttribute("super"); if (parent == null || parent.trim().length() == 0) 101 parent= ""; 103 allTypes.put(name, parent); 104 } 105 } 106 107 return allTypes; 108 } 109 } 110 | Popular Tags |