本站首页    管理页面    写新日志    退出 [QQ:172832876] [MSN:lanlanq@hotmail.com]
麒麟在天欢迎您

.:日期

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.:我的分类

.:最新日志

.:显示信息

blog名称:
日志总数:64
评论数量:34
留言数量:3
访问次数:343240
建立时间:2006年3月10日

.:留言板

.:链接

        公告

在属于自己的一片天空中,敲打着键盘,记录下自己感兴趣和认为自己因该回忆的碎片,可能是财富,也可能是不足,过后你会发现你的进步,这就足够。
[J2EE]通过一个包名来获得这个包下面的所有类列表
luckystar 发表于 2006/4/29 9:24:38

 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();        }     }}

阅读全文(3438) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.281 second(s), page refreshed 144764143 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号