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