| 1 17 18 package org.sape.carbon.core.util.reflection; 19 20 import java.io.File ; 21 import java.util.Enumeration ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.StringTokenizer ; 26 import java.util.zip.ZipEntry ; 27 import java.util.zip.ZipFile ; 28 29 30 39 public class ClassFinder { 40 41 45 protected long foundClasses = 0; 46 47 50 protected Class superClass = null; 51 54 protected String requiredPathSubstring = null; 55 58 protected Set classes = new HashSet (2000); 59 60 61 65 public ClassFinder() { 66 67 } 68 69 78 public ClassFinder(Class superClass) { 79 this.superClass = superClass; 80 81 } 82 83 96 public ClassFinder(Class superClass, String requiredPathSubstring) { 97 this.superClass = superClass; 98 99 this.requiredPathSubstring = requiredPathSubstring; 100 } 101 102 103 104 110 protected void addClassName(String className) { 111 112 if ((this.requiredPathSubstring == null) || 115 (className.indexOf(this.requiredPathSubstring) >= 0)) { 116 117 if (this.superClass == null) { 118 this.classes.add(className); 119 } else { 120 try { 121 122 Class theClass = 125 Class.forName( 126 className, 127 false, 128 this.getClass().getClassLoader()); 129 130 if (this.superClass.isAssignableFrom(theClass)) { 131 this.classes.add(className); 132 } 133 } catch (ClassNotFoundException cnfe) { 134 } catch (Throwable t) { 136 } 138 } 139 } 140 } 141 142 143 149 public Set getClasses() { 150 151 String classpath = System.getProperty("java.class.path"); 153 String pathSeparator = System.getProperty("path.separator"); 154 155 StringTokenizer st = new StringTokenizer (classpath,pathSeparator); 156 157 while (st.hasMoreTokens()) { 159 File currentDirectory = new File (st.nextToken()); 160 161 processFile(currentDirectory.getAbsolutePath(),""); 162 163 } 164 165 return this.classes; 166 } 167 168 169 176 private void processFile(String base, String current) { 177 File currentDirectory = new File (base + File.separatorChar + current); 178 179 if (isArchive(currentDirectory.getName())) { 181 try { 182 processZip(new ZipFile (currentDirectory)); 183 } catch (Exception e) { 184 } 187 return; 188 } else { 189 190 Set directories = new HashSet (); 191 192 File [] children = currentDirectory.listFiles(); 193 194 if (children == null || children.length == 0) { 196 return; 197 } 198 199 for (int i = 0; i < children.length; i++) { 201 File child = children[i]; 202 if (child.isDirectory()) { 203 directories.add(children[i]); 204 } else { 205 if (child.getName().endsWith(".class")) { 206 String className = 207 getClassName( 208 current + 209 ((current == "") ? "" : File.separator) + 210 child.getName()); 211 addClassName(className); 212 this.foundClasses++; 213 } 214 } 215 } 216 217 for (Iterator i = directories.iterator(); i.hasNext(); ) { 219 processFile(base, current + ((current=="")?"":File.separator) + 220 ((File )i.next()).getName()); 221 } 222 } 223 } 224 225 226 232 protected boolean isArchive(String name) { 233 if ((name.endsWith(".jar") || 234 (name.endsWith(".zip")))) { 235 236 return true; 237 } else { 238 return false; 239 } 240 } 241 242 247 protected String getClassName(String fileName) { 248 String newName = fileName.replace(File.separatorChar,'.'); 249 newName = newName.replace('/','.'); 251 return newName.substring(0, fileName.length() - 6); 252 } 253 254 255 262 protected void processZip(ZipFile file) { 263 Enumeration files = file.entries(); 264 265 while (files.hasMoreElements()) { 266 Object tfile = files.nextElement(); 267 ZipEntry child = (ZipEntry ) tfile; 268 if (child.getName().endsWith(".class")) { 269 addClassName(getClassName(child.getName())); 270 271 this.foundClasses++; 272 } 273 } 274 } 275 } | Popular Tags |