/** * Produce a list of Class references given a list of class names. If any classes cannot be loaded (due to * classloading error, or due to an exception being thrown in the class initialization block), * IllegalArgumentException will be thrown. * * @param classNames * The list of names of classes to load. * @throws IllegalArgumentException * if there were problems loading or initializing the classes. (Note that class initialization on * load is disabled by default, you can enable it with * {@code FastClasspathScanner#initializeLoadedClasses(true)} .) * @return a list of references to the loaded classes. */ public List<Class<?>> classNamesToClassRefs(final List<String> classNames) throws IllegalArgumentException { return classNamesToClassRefs(classNames, /* ignoreExceptions = */ false); }
/** * Produce a list of Class references, given a list of names of classes extending a common superclass or * implementing a common interface. If any classes cannot be loaded (due to classloading error, or due to an * exception being thrown in the class initialization block), IllegalArgumentException will be thrown. * * @param classNames * The list of names of classes to load. * @param commonClassType * The common superclass of, or interface implemented by, the named classes. * @throws IllegalArgumentException * if there were problems loading the classes, initializing the classes, or casting them to the * requested type. (Note that class initialization on load is disabled by default, you can enable it * with {@code FastClasspathScanner#initializeLoadedClasses(true)} .) * @return a list of references to the loaded classes. */ public <T> List<Class<T>> classNamesToClassRefs(final List<String> classNames, final Class<T> commonClassType) throws IllegalArgumentException { return classNamesToClassRefs(classNames, commonClassType, /* ignoreExceptions = */ false); }
/** Return a set of sub types for the given super type. Scans the full classpath. */ @SuppressWarnings("unchecked") public static <T> Set<Class<? extends T>> getSubTypes(Class<T> superType) { if (scanResult == null) { getInstance(); } List<String> classNames; if (superType.isInterface()) { classNames = scanResult.getNamesOfClassesImplementing(superType); } else { classNames = scanResult.getNamesOfSubclassesOf(superType); } Set<Class<? extends T>> ret = new HashSet<>(); scanResult.classNamesToClassRefs(classNames).forEach(c -> ret.add((Class<? extends T>) c)); return ret; }
/** Return a set of sub types for the given super type. Scans the full classpath. */ @SuppressWarnings("unchecked") public static <T> Set<Class<? extends T>> getSubTypes(Class<T> superType) { if (scanResult == null) { getInstance(); } List<String> classNames; if (superType.isInterface()) { classNames = scanResult.getNamesOfClassesImplementing(superType); } else { classNames = scanResult.getNamesOfSubclassesOf(superType); } Set<Class<? extends T>> ret = new HashSet<>(); scanResult.classNamesToClassRefs(classNames).forEach(c -> ret.add((Class<? extends T>) c)); return ret; }
/** Return a set of sub types for the given super type. Scans only the given package. */ @SuppressWarnings("unchecked") public static <T> Set<Class<? extends T>> getSubTypes(String packageName, Class<T> superType) { FastClasspathScanner scanner = new FastClasspathScanner(packageName); ScanResult sr = scanner.scan(); List<String> classNames; if (superType.isInterface()) { classNames = sr.getNamesOfClassesImplementing(superType); } else { classNames = sr.getNamesOfSubclassesOf(superType); } Set<Class<? extends T>> ret = new HashSet<>(); sr.classNamesToClassRefs(classNames).forEach(c -> ret.add((Class<? extends T>) c)); return ret; } }
/** Return a set of sub types for the given super type. Scans only the given package. */ @SuppressWarnings("unchecked") public static <T> Set<Class<? extends T>> getSubTypes(String packageName, Class<T> superType) { FastClasspathScanner scanner = new FastClasspathScanner(packageName); ScanResult sr = scanner.scan(); List<String> classNames; if (superType.isInterface()) { classNames = sr.getNamesOfClassesImplementing(superType); } else { classNames = sr.getNamesOfSubclassesOf(superType); } Set<Class<? extends T>> ret = new HashSet<>(); sr.classNamesToClassRefs(classNames).forEach(c -> ret.add((Class<? extends T>) c)); return ret; } }