Get all classes in package

Discussion in 'Plugin Development' started by FisheyLP, May 2, 2015.

Thread Status:
Not open for further replies.
  1. My plugin has several modules which can be activated and deactivated.
    The struggle is, that I must "register" each of them when I create a new module manually (yes, its such terrible xD) :
    Code:
    String[] mods = {"ChatClear", "ChatFunctions", "CreativeEnderPearls",
                    "Downloader", "GiveAll", "GlobalMute", "ItemFrameItems",
                    "Mute", "NoDropsMining", "NoFall", "Ride", "Spider", "Team"};
    
    for(String mod : mods) {
                try {
                @SuppressWarnings("unchecked")
                Class<Module> clazz = (Class<Module>) Class.forName("com.FisheyLP.AllInOne.modules."+mod);
                Module m = clazz.newInstance();
                m.setEnabled(true);
                modules.add(m);
                } catch(Exception e) {
                    Bukkit.getConsoleSender().sendMessage(m.l+"§4The module §c"+mod+" §4couldn't be loaded.");
                }
    Is there a way, that I can get all class (-names) in a package, to load them automaticly?
    I already tried something like:
    Code:
    try {
                Enumeration<URL> classes = getClass().getClassLoader().getResources("com/FisheyLP/AllInOne/modules");
                while(classes.hasMoreElements()) {
                    URL url = classes.nextElement();
                    System.out.println(url.getFile());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    But that didn't work (no output)...
     
  2. Offline

    mine-care

  3. Offline

    1Rogue

    Instead of reflecting, just provide a method for all the classes to be registered with.
     
  4. Solved:
    I used JarFile and JarEntry class to iterate through every file in the exported plugin.
    If anyone want's to know how exactly:
    Code:
    public class ClassFinder {
    
        public static Set<Class<?>> getClasses(File jarFile, String packageName) {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            try {
                JarFile file = new JarFile(jarFile); 
                for (Enumeration<JarEntry> entry = file.entries(); entry.hasMoreElements();) { 
                   JarEntry jarEntry = entry.nextElement();
                   String name = jarEntry.getName().replace("/", ".");
                   if(name.startsWith(packageName) && name.endsWith(".class"))
                   classes.add(Class.forName(name.substring(0, name.length() - 6)));
                }
                file.close();
            } catch(Exception e) {
                e.printStackTrace();
            }
            return classes;
        }
    }
    And how to use it:
    Code:
    for (Class<?> clazz : ClassFinder.getClasses(getJarFile(), "packageName") {
    //Do stuff
    }
    (getJarFile() is the method from JavaPlugin to get the plugin's jar file)
     
    Last edited: May 2, 2015
Thread Status:
Not open for further replies.

Share This Page