Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rs.utilities;
- import java.io.File;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.List;
- /**
- * A class that stores the essential utilities for the {@link File} resources used for the component handlers in-game.
- *
- * @author Abdallah Wahidi | Aug 22, 2017 : 10:52:19 PM
- */
- public class FileUtils {
- /**
- * Gets and returns an array of classes in a specified package.
- *
- * @param packageName The package name to get the classes of.
- *
- * @return array of classes in package
- */
- @SuppressWarnings({ "rawtypes" })
- public static Class[] getClasses(String packageName) {
- try {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- assert classLoader != null;
- String path = packageName.replace('.', '/');
- Enumeration<URL> resources = classLoader.getResources(path);
- List<File> dirs = new ArrayList<File>();
- while (resources.hasMoreElements()) {
- URL resource = resources.nextElement();
- dirs.add(new File(resource.getFile().replaceAll("%20", " ")));
- }
- ArrayList<Class> classes = new ArrayList<Class>();
- for (File directory : dirs) {
- classes.addAll(findClasses(directory, packageName));
- }
- return classes.toArray(new Class[classes.size()]);
- } catch (Exception e) {
- }
- return null;
- }
- /**
- * Finds a list of classes in a specified directory.
- *
- * @param directory The directory to look in.
- *
- * @param packageName The name of the package that contains these classes.
- *
- * @return The {@code List} of classes
- */
- @SuppressWarnings("rawtypes")
- private static List<Class> findClasses(File directory, String packageName) {
- List<Class> classes = new ArrayList<Class>();
- if (!directory.exists()) {
- return classes;
- }
- File[] files = directory.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- assert !file.getName().contains(".");
- classes.addAll(findClasses(file, packageName + "." + file.getName()));
- } else if (file.getName().endsWith(".class") && !file.getName().contains("$")) {
- try {
- classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
- } catch (Throwable e) {
- }
- }
- }
- return classes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment