Search in sources :

Example 1 with ClassInfo

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

the class JarReaderImpl method getClasses.

private Set<ClassInfo> getClasses(final URL url, final String packageDirName, String packageName, final Class<?> parent, final Class<? extends Annotation> annotation, final boolean recursive, Set<ClassInfo> classes) {
    try {
        if (url.toString().startsWith(JAR_FILE) || url.toString().startsWith(WSJAR_FILE)) {
            // Get jar file
            JarFile jarFile = ((JarURLConnection) url.openConnection()).getJarFile();
            // From the jar package to get an enumeration class
            Enumeration<JarEntry> eje = jarFile.entries();
            while (eje.hasMoreElements()) {
                // Get an entity in jar can be a directory and some other documents in the jar package
                // such as META-INF and other documents
                JarEntry entry = eje.nextElement();
                String name = entry.getName();
                // if start with '/'
                if (name.charAt(0) == '/') {
                    name = name.substring(1);
                }
                // If the first half is the same as the defined package name
                if (!name.startsWith(packageDirName)) {
                    continue;
                }
                int idx = name.lastIndexOf('/');
                // If the end of "/" is a package
                if (idx != -1) {
                    // Get the package name and replace "/" with "."
                    packageName = name.substring(0, idx).replace('/', '.');
                }
                // If it can be iterated and is a package
                if (idx == -1 && !recursive) {
                    continue;
                }
                // If it is a .class file and not a directory
                if (!name.endsWith(".class") || entry.isDirectory()) {
                    continue;
                }
                // Remove the following ".class" to get the real class name
                String className = name.substring(packageName.length() + 1, name.length() - 6);
                // Add to classes
                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));
                    }
                    continue;
                }
                if (null != annotation) {
                    if (null != clazz.getAnnotation(annotation)) {
                        classes.add(new ClassInfo(clazz));
                    }
                    continue;
                }
                classes.add(new ClassInfo(clazz));
            }
        }
    } catch (IOException e) {
        log.error("The scan error when the user to define the view from a jar package file.", e);
    } catch (ClassNotFoundException e) {
        log.error("", e);
    }
    return classes;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ClassInfo(com.blade.ioc.bean.ClassInfo)

Example 2 with ClassInfo

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

the class JarReaderImpl method getClassByAnnotation.

@Override
public Set<ClassInfo> getClassByAnnotation(String packageName, Class<?> parent, Class<? extends Annotation> annotation, boolean recursive) {
    Set<ClassInfo> classes = new HashSet<>();
    // Get the name of the package and replace it
    String packageDirName = packageName.replace('.', '/');
    // Defines an enumerated collection and loops to process the URL in this directory
    Enumeration<URL> dirs;
    try {
        dirs = this.getClass().getClassLoader().getResources(packageDirName);
        while (dirs.hasMoreElements()) {
            // Next
            URL url = dirs.nextElement();
            Set<ClassInfo> subClasses = this.getClasses(url, packageDirName, packageName, parent, annotation, recursive, classes);
            if (subClasses.size() > 0) {
                classes.addAll(subClasses);
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return classes;
}
Also used : IOException(java.io.IOException) URL(java.net.URL) ClassInfo(com.blade.ioc.bean.ClassInfo) HashSet(java.util.HashSet)

Example 3 with ClassInfo

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

the class JarReaderTest method testJarReader.

@Test
public void testJarReader() {
    JarReaderImpl jarReader = new JarReaderImpl();
    Set<ClassInfo> classInfos = jarReader.readClasses(Scanner.builder().packageName("org.slf4j.impl").build());
    Assert.assertNotNull(classInfos);
}
Also used : JarReaderImpl(com.blade.ioc.reader.JarReaderImpl) ClassInfo(com.blade.ioc.bean.ClassInfo) Test(org.junit.Test)

Example 4 with ClassInfo

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

the class NettyServer method initIoc.

private void initIoc() {
    RouteMatcher routeMatcher = blade.routeMatcher();
    routeMatcher.initMiddleware(blade.middleware());
    routeBuilder = new RouteBuilder(routeMatcher);
    blade.scanPackages().stream().flatMap(DynamicContext::recursionFindClasses).map(ClassInfo::getClazz).filter(ReflectKit::isNormalClass).forEach(this::parseAndCreate);
    routeMatcher.register();
    this.loaders.stream().sorted(new OrderComparator<>()).forEach(b -> b.preLoad(blade));
    this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.preHandle(blade));
    Ioc ioc = blade.ioc();
    if (BladeKit.isNotEmpty(ioc.getBeans())) {
        log.info("{}Register bean: {}", getStartedSymbol(), ioc.getBeans());
    }
    List<BeanDefine> beanDefines = ioc.getBeanDefines();
    if (BladeKit.isNotEmpty(beanDefines)) {
        beanDefines.forEach(b -> {
            IocKit.initInjection(ioc, b);
            IocKit.injectionValue(environment, b);
            List<TaskStruct> cronExpressions = BladeKit.getTasks(b.getType(), environment);
            if (null != cronExpressions) {
                taskStruts.addAll(cronExpressions);
            }
        });
    }
    this.loaders.stream().sorted(new OrderComparator<>()).forEach(b -> b.load(blade));
    this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.processor(blade));
}
Also used : RouteBuilder(com.blade.mvc.route.RouteBuilder) RouteMatcher(com.blade.mvc.route.RouteMatcher) TaskStruct(com.blade.task.TaskStruct) OrderComparator(com.blade.ioc.bean.OrderComparator) Ioc(com.blade.ioc.Ioc) BeanDefine(com.blade.ioc.bean.BeanDefine) ClassInfo(com.blade.ioc.bean.ClassInfo)

Example 5 with ClassInfo

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

the class AbstractClassReader method getClassByAnnotation.

public Set<ClassInfo> getClassByAnnotation(String packageName, Class<?> parent, Class<? extends Annotation> annotation, boolean recursive) {
    Set<ClassInfo> classes = new HashSet<>();
    // Get the name of the package and replace it
    String packageDirName = packageName.replace('.', '/');
    // Defines an enumerated collection and loops to process the URL in this directory
    Enumeration<URL> dirs;
    try {
        dirs = this.getClass().getClassLoader().getResources(packageDirName);
        // Loop iterations down
        while (dirs.hasMoreElements()) {
            URL url = dirs.nextElement();
            String filePath = new URI(url.getFile()).getPath();
            Set<ClassInfo> subClasses = findClassByPackage(packageName, filePath, parent, annotation, recursive, classes);
            if (BladeKit.isNotEmpty(subClasses)) {
                classes.addAll(subClasses);
            }
        }
    } catch (IOException | URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        log.error("Add user custom view class error Can't find such Class files.");
    }
    return classes;
}
Also used : IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) ClassInfo(com.blade.ioc.bean.ClassInfo) HashSet(java.util.HashSet)

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