1 26 27 package org.objectweb.util.explorer.core.common.lib; 28 29 30 import java.util.Vector ; 31 import java.util.List ; 32 import java.util.Iterator ; 33 34 40 public class ClassesInheritance { 41 42 48 49 protected List classes_; 50 51 57 61 public ClassesInheritance(Class theClass) { 62 classes_ = new Vector (); 63 if (theClass != null) 64 build(theClass); 65 } 66 67 73 78 protected Class getClass(String className) { 79 Iterator it = classes_.iterator(); 80 while (it.hasNext()) { 81 Class c = (Class ) it.next(); 82 if (className.equals(c.getName())) 83 return c; 84 } 85 return null; 86 } 87 88 92 protected void buildClass(Class c) { 93 if (c != null) { 95 classes_.add(c); 96 } 97 } 98 99 109 protected void buildInterfaces(Class [] interfaces) { 110 for (int i = 0; i < interfaces.length; i++) { 112 Class oldClass = getClass(interfaces[i].getName()); 113 if (oldClass != null) 114 classes_.remove(oldClass); 115 classes_.add(interfaces[i]); 116 } 117 Vector allExtendedInterfaces = new Vector (); 119 for (int i = 0; i < interfaces.length; i++) { 120 Class [] extendedInterfaces = interfaces[i].getInterfaces(); 121 for (int j = 0; j < extendedInterfaces.length; j++) { 122 allExtendedInterfaces.add(extendedInterfaces[j]); 123 } 124 } 125 if (allExtendedInterfaces.size() != 0) 127 buildInterfaces( 128 (Class []) allExtendedInterfaces.toArray(new Class [0])); 129 } 130 131 140 protected void build(Class c) { 141 if (c != null) { 142 buildClass(c); 143 buildInterfaces(c.getInterfaces()); 144 build(c.getSuperclass()); 145 } 146 } 147 148 154 158 public List getInheritClasses() { 159 return classes_; 160 } 161 162 167 public static Class [] getInheritedClasses(Class c){ 168 return (Class [])(new ClassesInheritance(c)).getInheritClasses().toArray(new Class [0]); 169 } 170 171 177 180 public static void main(String [] args) { 181 ClassesInheritance ci = new ClassesInheritance((new java.util.ArrayList ()).getClass()); 182 List list = ci.getInheritClasses(); 183 Iterator it = list.iterator(); 184 while (it.hasNext()) { 185 System.out.println("=> " + it.next()); 186 } 187 } 188 189 } 190 | Popular Tags |