Search in sources :

Example 1 with ClassReaderException

use of com.blade.kit.exception.ClassReaderException in project blade by biezhi.

the class AbstractClassReader method getClassByAnnotation.

@Override
public Set<ClassInfo> getClassByAnnotation(String packageName, Class<?> parent, Class<? extends Annotation> annotation, boolean recursive) {
    Assert.notBlank(packageName);
    Set<ClassInfo> classes = CollectionKit.newHashSet();
    // 获取包的名字 并进行替换
    String packageDirName = packageName.replace('.', '/');
    // 定义一个枚举的集合 并进行循环来处理这个目录下的URL
    Enumeration<URL> dirs;
    try {
        dirs = this.getClass().getClassLoader().getResources(packageDirName);
        // 循环迭代下去
        while (dirs.hasMoreElements()) {
            // 获取下一个元素
            URL url = dirs.nextElement();
            try {
                // 获取包的物理路径
                String filePath = new URI(url.getFile()).getPath();
                Set<ClassInfo> subClasses = findClassByPackage(packageName, filePath, parent, annotation, recursive, classes);
                if (subClasses.size() > 0) {
                    classes.addAll(subClasses);
                }
            } catch (URISyntaxException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        LOGGER.error("Add user custom view class error Can't find such Class files.");
        throw new ClassReaderException(e);
    }
    return classes;
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) ClassReaderException(com.blade.kit.exception.ClassReaderException)

Aggregations

ClassReaderException (com.blade.kit.exception.ClassReaderException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1