Search in sources :

Example 6 with ClassInfo

use of com.blade.ioc.bean.ClassInfo in project blade by biezhi.

the class AbstractClassReader method findClassByPackage.

/**
 * Get class by condition
 *
 * @param packageName
 * @param packagePath
 * @param parent
 * @param annotation
 * @param recursive
 * @return
 */
private Set<ClassInfo> findClassByPackage(final String packageName, final String packagePath, final Class<?> parent, final Class<? extends Annotation> annotation, final boolean recursive, Set<ClassInfo> classes) throws ClassNotFoundException {
    // Get the directory of this package to create a File
    File dir = new File(packagePath);
    // If not exist or is not a direct return to the directory
    if ((!dir.exists()) || (!dir.isDirectory())) {
        log.warn("The package [{}] not found.", packageName);
    }
    // If present, get all the files under the package include the directory
    File[] dirFiles = accept(dir, recursive);
    // Loop all files
    if (null != dirFiles && dirFiles.length > 0) {
        for (File file : dirFiles) {
            // If it is a directory, continue scanning
            if (file.isDirectory()) {
                findClassByPackage(packageName + '.' + file.getName(), file.getAbsolutePath(), parent, annotation, recursive, classes);
            } else {
                // If the java class file is removed later. Class only leave the class name
                String className = file.getName().substring(0, file.getName().length() - 6);
                Class<?> clazz = Class.forName(packageName + '.' + className);
                if (null != parent && null != annotation) {
                    if (null != clazz.getSuperclass() && clazz.getSuperclass().equals(parent) && null != clazz.getAnnotation(annotation)) {
                        classes.add(new ClassInfo(clazz));
                    }
                    continue;
                }
                if (null != parent) {
                    if (null != clazz.getSuperclass() && clazz.getSuperclass().equals(parent)) {
                        classes.add(new ClassInfo(clazz));
                    } else {
                        if (null != clazz.getInterfaces() && clazz.getInterfaces().length > 0 && clazz.getInterfaces()[0].equals(parent)) {
                            classes.add(new ClassInfo(clazz));
                        }
                    }
                    continue;
                }
                if (null != annotation) {
                    if (null != clazz.getAnnotation(annotation)) {
                        classes.add(new ClassInfo(clazz));
                    }
                    continue;
                }
                classes.add(new ClassInfo(clazz));
            }
        }
    }
    return classes;
}
Also used : File(java.io.File) ClassInfo(com.blade.ioc.bean.ClassInfo)

Example 7 with ClassInfo

use of com.blade.ioc.bean.ClassInfo in project blade by biezhi.

the class DynamicContext method recursionFindClasses.

public static Stream<ClassInfo> recursionFindClasses(String packageName) {
    Scanner scanner = Scanner.builder().packageName(packageName).recursive(true).build();
    Set<ClassInfo> classInfos = getClassReader(packageName).readClasses(scanner);
    return classInfos.stream();
}
Also used : Scanner(com.blade.ioc.bean.Scanner) ClassInfo(com.blade.ioc.bean.ClassInfo)

Example 8 with ClassInfo

use of com.blade.ioc.bean.ClassInfo in project blade by biezhi.

the class ClassInfoTest method testClassInfo.

@Test
public void testClassInfo() {
    String clsName = "com.blade.types.BladeClassDefineType";
    ClassInfo classInfo = new ClassInfo(clsName);
    Assert.assertEquals(null, classInfo.getClazz());
    Assert.assertEquals(BladeClassDefineType.class.getName(), classInfo.getClassName());
    classInfo = new ClassInfo(BladeClassDefineType.class);
    Assert.assertEquals(BladeClassDefineType.class, classInfo.getClazz());
    classInfo = new ClassInfo(clsName, BladeClassDefineType.class);
    Assert.assertEquals(BladeClassDefineType.class, classInfo.getClazz());
    Object object = classInfo.newInstance();
    Assert.assertNotNull(object);
}
Also used : BladeClassDefineType(com.blade.types.BladeClassDefineType) ClassInfo(com.blade.ioc.bean.ClassInfo) Test(org.junit.Test)

Example 9 with ClassInfo

use of com.blade.ioc.bean.ClassInfo in project blade by biezhi.

the class ClassPathClassReaderTest method testClassPathReader.

@Test
public void testClassPathReader() {
    String packageName = "com.blade.types.controller";
    ClassReader classReader = DynamicContext.getClassReader(packageName);
    assertEquals(ClassPathClassReader.class, classReader.getClass());
    Set<ClassInfo> classInfos = classReader.readClasses(Scanner.builder().packageName(packageName).build());
    assertEquals(2, classInfos.size());
    classInfos = classReader.readClasses(Scanner.builder().packageName(packageName).parent(Runnable.class).build());
    assertEquals(1, classInfos.size());
    assertEquals(UserService.class, classInfos.stream().findFirst().get().getClazz());
    classInfos = classReader.readClasses(Scanner.builder().packageName(packageName).annotation(Path.class).build());
    assertEquals(1, classInfos.size());
    assertEquals(IndexController.class, classInfos.stream().findFirst().get().getClazz());
    classInfos = classReader.readClasses(Scanner.builder().packageName(packageName).parent(Object.class).annotation(Path.class).build());
    assertEquals(1, classInfos.size());
    assertEquals(IndexController.class, classInfos.stream().findFirst().get().getClazz());
}
Also used : Path(com.blade.mvc.annotation.Path) ClassPathClassReader(com.blade.ioc.reader.ClassPathClassReader) ClassInfo(com.blade.ioc.bean.ClassInfo) Test(org.junit.Test)

Aggregations

ClassInfo (com.blade.ioc.bean.ClassInfo)9 IOException (java.io.IOException)3 Test (org.junit.Test)3 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Ioc (com.blade.ioc.Ioc)1 BeanDefine (com.blade.ioc.bean.BeanDefine)1 OrderComparator (com.blade.ioc.bean.OrderComparator)1 Scanner (com.blade.ioc.bean.Scanner)1 ClassPathClassReader (com.blade.ioc.reader.ClassPathClassReader)1 JarReaderImpl (com.blade.ioc.reader.JarReaderImpl)1 Path (com.blade.mvc.annotation.Path)1 RouteBuilder (com.blade.mvc.route.RouteBuilder)1 RouteMatcher (com.blade.mvc.route.RouteMatcher)1 TaskStruct (com.blade.task.TaskStruct)1 BladeClassDefineType (com.blade.types.BladeClassDefineType)1 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1