1 11 12 package org.eclipse.core.internal.databinding; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 26 public class ClassLookupSupport { 27 28 31 private static HashMap classSearchOrderLookup; 32 33 38 public static Class [] getTypeHierarchyFlattened(Class type) { 39 List classes = null; 40 HashMap lookup = classSearchOrderLookup; 42 if (lookup != null) 43 classes = (List ) lookup.get(type); 44 if (classes == null) { 46 classes = new ArrayList (); 47 computeClassOrder(type, classes); 48 if (lookup == null) 49 classSearchOrderLookup = lookup = new HashMap (); 50 lookup.put(type, classes); 51 } 52 return (Class []) classes.toArray(new Class [classes.size()]); 53 } 54 55 65 private static void computeClassOrder(Class adaptable, Collection classes) { 66 Class clazz = adaptable; 67 Set seen = new HashSet (4); 68 while (clazz != null) { 69 classes.add(clazz); 70 computeInterfaceOrder(clazz.getInterfaces(), classes, seen); 71 clazz = clazz.isInterface() ? Object .class : clazz.getSuperclass(); 72 } 73 } 74 75 private static void computeInterfaceOrder(Class [] interfaces, Collection classes, Set seen) { 76 List newInterfaces = new ArrayList (interfaces.length); 77 for (int i = 0; i < interfaces.length; i++) { 78 Class interfac = interfaces[i]; 79 if (seen.add(interfac)) { 80 classes.add(interfac); 82 newInterfaces.add(interfac); 83 } 84 } 85 for (Iterator it = newInterfaces.iterator(); it.hasNext();) 86 computeInterfaceOrder(((Class ) it.next()).getInterfaces(), classes, seen); 87 } 88 89 90 } 91 | Popular Tags |