dev.vality.geck.common.reflection.ClassFinder Maven / Gradle / Ivy
The newest version!
package dev.vality.geck.common.reflection;
import org.reflections.Configuration;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ConfigurationBuilder;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ClassFinder {
public static Collection> find(Collection scannedPackages,
String classNameSuffix,
Class classType) {
Set> classes = new HashSet<>();
for (String scannedPackage : scannedPackages) {
classes.addAll(find(scannedPackage, classNameSuffix, classType));
}
return classes;
}
public static Collection> find(String scannedPackage,
String classSuffix,
Class classType) {
return new Reflections(scannedPackage)
.getSubTypesOf(classType).stream()
.filter(t -> t.getSimpleName().endsWith(classSuffix))
.collect(Collectors.toSet());
}
public static Set findResources(String path, String regex) {
Configuration configuration = new ConfigurationBuilder()
.setScanners(new ResourcesScanner())
.forPackages(path);
return new Reflections(configuration).getResources(Pattern.compile(regex));
}
}