1 package it.stefanochizzolini.reflex; 2 3 import java.io.*; 4 import java.util.*; 5 import java.util.jar.*; 6 import java.net.*; 7 8 public class Package 9 { 10 17 public static List<java.lang.Class > getClasses( 18 String packageName 19 ) 20 { 21 return getClasses( 22 packageName, 23 (System.getProperty("sun.boot.class.path") + File.pathSeparator + System.getProperty("java.ext.dirs") + File.pathSeparator + System.getProperty("java.class.path") ).split(File.pathSeparator) 27 ); 28 } 29 30 39 public static List<java.lang.Class > getClasses( 40 String packageName, 41 String [] locations 42 ) 43 { 44 try 45 { 46 String packagePath = packageName.replace('.','/') + '/'; 47 String osDependentPackagePath = packagePath.replace('/',File.separatorChar); 48 ArrayList<java.lang.Class > classes = new ArrayList<java.lang.Class >(); 49 for( 50 String location : locations 51 ) 52 { 53 File locationFile = new File(location); 54 if(!locationFile.exists()) 55 continue; 56 57 if(locationFile.isDirectory()) { 59 locationFile = new File(location + File.separator + osDependentPackagePath); 60 if(!locationFile.exists()) 61 continue; 62 63 String [] filePaths = locationFile.list(); 65 for(String filePath : filePaths) 66 { 67 if(filePath.endsWith(".class")) 69 { 70 classes.add( 71 java.lang.Class.forName( 72 packageName + '.' + filePath.substring(0, filePath.length() - 6) 73 ) 74 ); 75 } 76 } 77 } 78 else { 80 JarFile jarFile = new JarFile(locationFile); 81 for( 82 Enumeration entries = jarFile.entries(); 83 entries.hasMoreElements(); 84 ) 85 { 86 String jarEntryPath = ((JarEntry)entries.nextElement()).getName(); 87 if( 88 jarEntryPath.startsWith(packagePath) 89 && jarEntryPath.endsWith(".class") 90 ) 91 { 92 classes.add( 93 java.lang.Class.forName( 94 jarEntryPath.substring(0, jarEntryPath.length() - 6).replaceAll("/",".") 95 ) 96 ); 97 } 98 } 99 } 100 } 101 102 return classes; 103 } 104 catch(Exception e) 105 {throw new RuntimeException (e);} 106 } 107 }
| Popular Tags
|