Load all classes from package

Discussion in 'Plugin Development' started by Aqua, May 3, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Aqua

    I have recently been in the need to load a lot of classes from a single package without having to instantiate every single class in the package. I tried this by using the ClassLoader from the mainclass which extends JavaPlugin, this was no success. I tried to use the Bukkit classloader, no success. Used reflection to "rip" the classloader out of JavaPlugin, still no success.

    How should I get this to work, the only error I get is
    URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
    is null, when replacing the ClassLoader.getSystemClassLoader() with all classloaders above it returned null.

    Code I try to run:
    Code:java
    1.  
    2. public static ArrayList<Class<?>> getClassesForPackage(Package pkg) {
    3. ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
    4.  
    5. String pkgname = pkg.getName();
    6. String relPath = pkgname.replace('.', '/');
    7.  
    8. // Get a File object for the package
    9. URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
    10. if (resource == null) {
    11. throw new RuntimeException("Unexpected problem: No resource for " + relPath);
    12. }
    13. log("Package: '" + pkgname + "' becomes Resource: '" + resource.toString() + "'");
    14.  
    15. resource.getPath();
    16. if(resource.toString().startsWith("jar:")) {
    17. processJarfile(resource, pkgname, classes);
    18. } else {
    19. processDirectory(new File(resource.getPath()), pkgname, classes);
    20. }
    21.  
    22. return classes;
    23. }
    24.  

    It reaches the throw new RuntimeException.
     
  2. Offline

    Aqua

    bump
     
  3. Offline

    ZekeMo

    Why are you trying to accomplish by loading all of your classes in said package?
    and by load do you mean you have classes 1, 2, 3 in your package for example and you want to do this?
    class1 x = new class1();
    class2 y = new class2();
    class3 z = new class3();
     
  4. Offline

    Aqua

    ZekeMo As I stated in original post, I will end up with quite some classes which I need to instantiate, but want to have it done automaticaly.
     
  5. Offline

    ZekeMo

  6. Offline

    Cirno

    CraftBukkit has a class built into it, however, the standard API does not have such a method. I do this to load classes dynamically by only declaring the package. Note you need a class loader; I recommend you pass the one that you can get via this.getClassLoader() in your main class.

    Code:java
    1. ClassPath path = ClassPath.from(classloader);
    2. for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(packageName)) {
    3. Class clazz = Class.forName(info.getName(), true, classloader);
    4. //dowhatever
    5. }


    I believe this works by merely just exploring the JAR file.
     
Thread Status:
Not open for further replies.

Share This Page