Inspecting packages inside modules

If we go with Java 9 modularity, then our packages will live inside modules. For example, if we have a class called Manager in a package called com.management in a module called org.tournament, then we can fetch all the packages of this module like so:

Manager mgt = new Manager();
Set<String> packages = mgt.getClass().getModule().getPackages();

In addition, if we want to create a class, then we need the following Class.forName() flavor:

Class<?> clazz = Class.forName(mgt.getClass()
.getModule(), "com.management.Manager");

Keep in mind that each module is represented on disk as a directory with the same name. For example, the org.tournament module is on disk a folder with this name. Moreover, each module is mapped as a separate JAR with this name (for example, org.tournament.jar). By having these coordinates in mind, it is pretty straightforward to adapt the code from this section so that it lists all the classes of a given package of a given module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14