需求:获取如下途中的实体类的同包实体类名

获取某个实体类的通报目录下的实体类

 

public class ClassUtils {

    public static void main(String[] args) {

       @SuppressWarnings("rawtypes")

       List<Class> allClass = getAllClass(SchoolReport.class);

       System.out.println(JSONObject.toJSONString(allClass,true));

    }

    // 获取某实体类的同包目录下的所有实体类

    @SuppressWarnings("rawtypes")

    public static List<Class> getAllClass(Class<SchoolReport> c) {

       List<Class> returnClassList = new ArrayList<Class>(); // 返回结果

       String packageName = c.getPackage().getName(); // 获得当前的包名

       try {

           List<Class> allClass = getClasses(packageName); // 获得当前包下以及子包下的所有类

           for (int i = 0; i < allClass.size(); i++) {

               if (!c.equals(allClass.get(i))) { // 本身不加进去

                   returnClassList.add(allClass.get(i));

               }

           }

       } catch (ClassNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

       return returnClassList;

    }

    // 从一个包中查找出所有的类,在jar包中不能查找

    @SuppressWarnings("rawtypes")

    private static List<Class> getClasses(String packageName) throws ClassNotFoundException, IOException {

       ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

       String path = packageName.replace('.', '/');

       Enumeration<URL> resources = classLoader.getResources(path);

       List<File> dirs = new ArrayList<File>();

       while (resources.hasMoreElements()) {

           URL resource = resources.nextElement();

           dirs.add(new File(resource.getFile()));

       }

       ArrayList<Class> classes = new ArrayList<Class>();

       for (File directory : dirs) {

           classes.addAll(findClasses(directory, packageName));

       }

       return classes;

    }

    @SuppressWarnings("rawtypes")

    private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {

       List<Class> classes = new ArrayList<Class>();

       if (!directory.exists()) {

           return classes;

       }

       File[] files = directory.listFiles();

       for (File file : files) {

           if (file.isDirectory()) {

               assert !file.getName().contains(".");

               classes.addAll(findClasses(file, packageName + "." + file.getName()));

           } else if (file.getName().endsWith(".class")) {

               classes.add(

                      Class.forName(packageName + '.'

                      file.getName().substring(0, file.getName().length() - 6)));

           }

       }

       return classes;

    }

}

 

运行结果:

 

[

    "com.syy.test.decorator.DecoratorReport",

    "com.syy.test.decorator.Father",

    "com.syy.test.decorator.FiveReport",

    "com.syy.test.decorator.HighReport",

    "com.syy.test.decorator.SortReport"

]

相关文章:

  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-13
  • 2022-12-23
  • 2021-03-31
猜你喜欢
  • 2022-12-23
  • 2021-11-13
  • 2021-09-17
  • 2021-10-07
  • 2022-12-23
  • 2021-10-13
  • 2021-09-09
相关资源
相似解决方案