Created
November 11, 2018 05:09
-
-
Save hnOsmium0001/74e6991a4cc44d97d384d0fb8767e25b to your computer and use it in GitHub Desktop.
Reflection utils for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.annotation.Nonnull; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.net.URL; | |
| import java.util.ArrayList; | |
| import java.util.Enumeration; | |
| import java.util.List; | |
| public class ReflectionUtils { | |
| private ReflectionUtils() { | |
| } | |
| /** | |
| * Scans all classes accessible from the context class loader which belong to the given package and subpackages. <br /> | |
| * <a href="https://dzone.com/articles/get-all-classes-within-package">Source</a>, with changes to welcome generic system since Java 5. | |
| * | |
| * @param packageName The base package | |
| * @return The classes | |
| * @throws ClassNotFoundException | |
| * @throws IOException | |
| */ | |
| @Nonnull | |
| public static List<Class<?>> getClasses(String packageName) throws ClassNotFoundException, IOException { | |
| ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
| if (classLoader == null) { | |
| return new ArrayList<>(); | |
| } | |
| String path = packageName.replace('.', '/'); | |
| Enumeration<URL> resources = classLoader.getResources(path); | |
| List<File> dirs = new ArrayList<>(); | |
| while (resources.hasMoreElements()) { | |
| URL resource = resources.nextElement(); | |
| dirs.add(new File(resource.getFile())); | |
| } | |
| List<Class<?>> classes = new ArrayList<>(); | |
| for (File directory : dirs) { | |
| classes.addAll(findClasses(directory, packageName)); | |
| } | |
| return classes; | |
| } | |
| /** | |
| * Recursive method used to find all classes in a given directory and subdir's. <br /> | |
| * <a href="https://dzone.com/articles/get-all-classes-within-package">Source</a>, with changes to welcome generic system since Java 5. | |
| * | |
| * @param directory The base directory | |
| * @param packageName The package name for classes found inside the base directory | |
| * @return The classes | |
| * @throws ClassNotFoundException | |
| */ | |
| @Nonnull | |
| private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException { | |
| List<Class<?>> classes = new ArrayList<>(); | |
| if (!directory.exists()) { | |
| return classes; | |
| } | |
| File[] files = directory.listFiles(); | |
| if (files == null) { | |
| return classes; | |
| } | |
| for (File file : files) { | |
| if (file.isDirectory()) { | |
| if (!file.getName().contains(".")) { | |
| return classes; | |
| } | |
| classes.addAll(findClasses(file, packageName + "." + file.getName())); | |
| } else if (file.getName().endsWith(".class")) { | |
| classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); | |
| } | |
| } | |
| return classes; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment