/** * Scan all classes belonging to the specified package. * * @param pkg A package. */ public void scanPackage(Package pkg) { scanPackage(pkg.getName()); }
/** * Creates and registers a component definition for a class by scanning the class and its * superclasses for method annotations. * * @param clazz Class to scan. */ @SuppressWarnings("unchecked") public void scanClass(Class<?> clazz) { for (Class<?> innerClass : clazz.getDeclaredClasses()) { scanClass(innerClass); } if (!clazz.isAnnotationPresent(annotationClass)) { return; } if (!targetClass.isAssignableFrom(clazz)) { throw new RuntimeException( annotationClass.getName() + " annotation only valid on " + targetClass.getName() + " subclass."); } doScanClass((Class<T>) clazz); }
/** * Creates and registers a component definition for a class by scanning the named class and its * superclasses for method annotations. * * @param className Fully qualified name of the class to scan. */ public void scanClass(String className) { try { scanClass(Class.forName(className)); } catch (ClassNotFoundException e) { throw MiscUtil.toUnchecked(e); } }
/** * Scan all classes belonging to the specified package. * * @param pkgName A package name. Wild card characters are allowed. */ public void scanPackage(String pkgName) { try { for (Resource resource : resolver.getResources("classpath*:" + pkgName.replace(".", "/") + "/*.class")) { String path = resource.getURL().getPath(); int i = path.lastIndexOf(".jar!/") + 6; i = i > 5 ? i : path.lastIndexOf("/classes/") + 9; int j = path.lastIndexOf(".class"); path = path.substring(i, j).replace("/", "."); Class<?> clazz = Class.forName(path); scanClass(clazz); } } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }