public class ExtensionFileFilter implements FileFilter {
public ExtensionFileFilter() { }
/** * Tests whether or not the specified abstract pathname should be * included in a pathname list. * * @param pathname The abstract pathname to be tested * @return <code>true</code> if and only if <code>pathname</code> should * be included * @todo Implement this java.io.FileFilter method */ public boolean accept(File file) { //只选择路径 // if (file.isDirectory()) { // return true; // } else { // return false; // } return true; }public class Precomplie {
public FileFilter fileFilter = new ExtensionFileFilter(); public java.net.URI rootURI;
public Precomplie(String dir) { try { rootURI = ClassLoader.getSystemResource(dir).toURI(); } catch (URISyntaxException ex) { ex.printStackTrace(); } }
public void precompileDirectory(File dir) { File[] files = dir.listFiles(fileFilter); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { precompileDirectory(files[i]); } else { v.add(getClass(files[i])); } } }
private Class getClass(File file) { String ClassName = file.getAbsolutePath(); String ClassPath = ""; //ClassLoader.getSystemResource() int i = ClassName.indexOf("."); if (i > 0) { ClassName = ClassName.substring(0, i); String[] s =ClassName.split("\\\\"); for (int j = 2; j < s.length; j++) { ClassPath = ClassPath + s[j] + "."; } ClassPath = ClassPath.substring(0, ClassPath.length()-1); int y = ClassPath.indexOf("lib"); if(y>0){ ClassPath = ClassPath.substring(y+4,ClassPath.length()); } y = ClassPath.indexOf("classes"); if(y>0){ ClassPath = ClassPath.substring(y+8,ClassPath.length()); } try { Class c = Class.forName(ClassPath); return c; } catch (ClassNotFoundException ex) { ex.printStackTrace(); } } return null; }
public Object[] getObjects() { File f = new File(this.rootURI); this.precompileDirectory(f); return this.getV().toArray(); }
List v = new java.util.ArrayList(); public List getV() { return v; }
public void setV(List v) { this.v = v; }
public static void main(String[] args) { //System.out.println("e:\\dd".replaceAll("\\\\","//")); try { Precomplie p = new Precomplie("st"); Object[] o = p.getObjects(); for (int i = 0; i < o.length; i++) { System.out.println(o.getClass().toString()); } } catch (Exception exc) { exc.printStackTrace(); }
}}