Abdallah_Wahidi

Untitled

Aug 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.rs.utilities;
  2.  
  3. import java.io.File;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.Enumeration;
  7. import java.util.List;
  8.  
  9. /**
  10.  * A class that stores the essential utilities for the {@link File} resources used for the component handlers in-game.
  11.  *
  12.  * @author Abdallah Wahidi | Aug 22, 2017 : 10:52:19 PM
  13.  */
  14. public class FileUtils {
  15.  
  16.     /**
  17.      * Gets and returns an array of classes in a specified package.
  18.      *
  19.      * @param packageName The package name to get the classes of.
  20.      *
  21.      * @return array of classes in package
  22.      */
  23.     @SuppressWarnings({ "rawtypes" })
  24.     public static Class[] getClasses(String packageName) {
  25.         try {
  26.             ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  27.             assert classLoader != null;
  28.             String path = packageName.replace('.', '/');
  29.             Enumeration<URL> resources = classLoader.getResources(path);
  30.             List<File> dirs = new ArrayList<File>();
  31.             while (resources.hasMoreElements()) {
  32.                 URL resource = resources.nextElement();
  33.                 dirs.add(new File(resource.getFile().replaceAll("%20", " ")));
  34.             }
  35.             ArrayList<Class> classes = new ArrayList<Class>();
  36.             for (File directory : dirs) {
  37.                 classes.addAll(findClasses(directory, packageName));
  38.             }
  39.             return classes.toArray(new Class[classes.size()]);
  40.         } catch (Exception e) {
  41.  
  42.         }
  43.         return null;
  44.     }
  45.  
  46.     /**
  47.      * Finds a list of classes in a specified directory.
  48.      *
  49.      * @param directory The directory to look in.
  50.      *
  51.      * @param packageName The name of the package that contains these classes.
  52.      *
  53.      * @return The {@code List} of classes
  54.      */
  55.     @SuppressWarnings("rawtypes")
  56.     private static List<Class> findClasses(File directory, String packageName) {
  57.         List<Class> classes = new ArrayList<Class>();
  58.         if (!directory.exists()) {
  59.             return classes;
  60.         }
  61.         File[] files = directory.listFiles();
  62.         for (File file : files) {
  63.             if (file.isDirectory()) {
  64.                 assert !file.getName().contains(".");
  65.                 classes.addAll(findClasses(file, packageName + "." + file.getName()));
  66.             } else if (file.getName().endsWith(".class") && !file.getName().contains("$")) {
  67.                 try {
  68.                     classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
  69.                 } catch (Throwable e) {
  70.  
  71.                 }
  72.             }
  73.         }
  74.         return classes;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment