Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1293176
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: Java

2014-10-11 03:27:11


点击(此处)折叠或打开

  1. public class ClassUtils {
  2.     private static final Logger logger = RequestIdentityLogger.getLogger(ClassUtils.class);

  3.     public static String getClassPath(Class cls) {

  4.         return cls.getProtectionDomain().getCodeSource().getLocation().getFile();
  5.     }

  6.     public static List<String> listAllClassesInCurrentDir(Class cls) {

  7.         String path = getClassPath(cls);

  8.         System.out.println(path);

  9.         if (path.endsWith(".jar")) {
  10.             return listAllClassesInJar(path);
  11.         } else {
  12.             return listAllClassesInFiles(path, "", null);
  13.         }
  14.     }

  15.     /*
  16.      * 获取所有的.class文件
  17.      */
  18.     private static List<String> listAllClassesInFiles(String path, String classPath, List<String> classesList) {

  19.         if (path == null || classPath == null) {
  20.             logger.error("in listAllClasses, the argument is null");
  21.             return null;
  22.         }

  23.         if (classPath.equals("") == false) {
  24.             classPath = classPath + ".";
  25.         }

  26.         if (classesList == null) {
  27.             classesList = new ArrayList<String>();
  28.         }

  29.         File classFiles = new File(path);

  30.         for (File classSubFile : classFiles.listFiles()) {
  31.             if (classSubFile.isDirectory()) {
  32.                 listAllClassesInFiles(classSubFile.getPath(), classPath + classSubFile.getName(), classesList);
  33.             } else {
  34.                 if (classSubFile.getName().endsWith(".class")) {
  35.                     classesList.add(classPath
  36.                             + classSubFile.getName().substring(0, classSubFile.getName().length() - 6));
  37.                 }
  38.             }
  39.         }

  40.         return classesList;
  41.     }

  42.     private static List<String> listAllClassesInJar(String path) {

  43.         String clsName = null;
  44.         List<String> clsNameList = new ArrayList<String>();

  45.         try {
  46.             JarFile jarFile = new JarFile(path);
  47.             Enumeration<JarEntry> entrys = jarFile.entries();

  48.             while (entrys.hasMoreElements()) {
  49.                 JarEntry jarEnty = entrys.nextElement();

  50.                 clsName = jarEnty.getName();
  51.                 clsName = clsName.replace("/", ".");

  52.                 if (clsName.endsWith(".class")) {
  53.                     clsNameList.add(clsName.substring(0, clsName.length() - 6));
  54.                 }
  55.             }
  56.         } catch (IOException e) {
  57.             // TODO Auto-generated catch block
  58.             e.printStackTrace();
  59.             return null;
  60.         }

  61.         return clsNameList;
  62.     }
  63. }

阅读(3366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~