A simple module framework for Java
JModules is a simple Java framework that makes creating additional modules easy. All you have to do is implement a class and let JModules do the rest.
Using JModules in your project is as easy as 1, 2, 3.
- Implement the
Module
class - Create a
ModuleManager
- Add some packages
Then you can access, enable or disable modules to your hearts content!
Each module is represented by a simple interface, meaning implementing it is super simple.
public class MyModule implements Module {
@Override
public void onEnable() {
// do some enabling
}
@Override
public void onDisable() {
// do some disabling
}
}
You can also define a custom name for a module and define other modules that this module depends on.
public class MyAdvancedModule implements Module {
@Override
public void onEnable() {
// do some enabling
}
@Override
public void onDisable() {
// do some disabling
}
@Override
public String getName() {
return "CustomName";
}
@Override
public String[] getDependencies() {
return new String[]{"MyModule"};
}
}
The ModuleManager
class is the main class used in JModules. Firstly, you need to create your own instance of the ModuleManager
like so:
ModuleManager myManager = new ModuleManager();
You can also add any amount of runnables to be executed whenever a module is added. This can be used to store each module or register them in some other external system.
Each ModuleInterfaceRunnable
is added to the ModuleManager
during instantiation.
ModuleManager myManager = new ModuleManager(new ModuleInterfaceRunnable(MyInterface.class) {
@Override
public void run(Module module) {
// do some more things with the module
}
});
JModules works by searching a package and adding any Module
s found to the ModuleManager
. This is done by supplying
the ModuleManager
with a package to search, a ClassLoader
to search through. You can also ask the ModuleManager
to
ignore a set of classes if you like.
myManager.addModulesFromPackage("com.mypackage", System.classLoader(), IgnoreMe.class);
You can also add modules by yourself using the addModule
method.
Once you are done you can enable and disable modules using the methods supplied in the ModuleManager
.
Thanks for using JModules! I originally created this because I am lazy and couldn't be bothered to implement something like this for each project I would use it in, but I decided to upload it to the world in the hopes someone else would find it useful!
If you need any help or have any issues or suggestions, please let me know!