|
|
|
|
@@ -0,0 +1,54 @@
|
|
|
|
|
package com.baeldung.reflection.access.packages;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import org.reflections.Reflections;
|
|
|
|
|
import org.reflections.scanners.SubTypesScanner;
|
|
|
|
|
|
|
|
|
|
import com.google.common.reflect.ClassPath;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
public class AccessingAllClassesInPackage {
|
|
|
|
|
|
|
|
|
|
public Set<Class> findAllClassesUsingClassLoader(String packageName) {
|
|
|
|
|
InputStream stream = ClassLoader.getSystemClassLoader()
|
|
|
|
|
.getResourceAsStream(packageName.replaceAll("[.]", "/"));
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
|
|
|
|
return reader.lines()
|
|
|
|
|
.filter(line -> line.endsWith(".class"))
|
|
|
|
|
.map(line -> getClass(line, packageName))
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Class getClass(String className, String packageName) {
|
|
|
|
|
try {
|
|
|
|
|
return Class.forName(packageName + "." + className.substring(0, className.lastIndexOf('.')));
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<Class> findAllClassesUsingReflectionsLibrary(String packageName) {
|
|
|
|
|
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
|
|
|
|
|
return reflections.getSubTypesOf(Object.class)
|
|
|
|
|
.stream()
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<Class> findAllClassesUsingGoogleGuice(String packageName) throws IOException {
|
|
|
|
|
return ClassPath.from(ClassLoader.getSystemClassLoader())
|
|
|
|
|
.getAllClasses()
|
|
|
|
|
.stream()
|
|
|
|
|
.filter(clazz -> clazz.getPackageName()
|
|
|
|
|
.equalsIgnoreCase(packageName))
|
|
|
|
|
.map(clazz -> clazz.load())
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|