Bottom-Up Strategy for Code Migration – Java Module System

Bottom-Up Strategy for Code Migration

If all direct dependencies of a plain JAR are known to be modules, the plain JAR can be directly converted to an explicit module by declaring their dependencies and exports in a module declaration. This idea is embodied in the following algorithm, based on the graph of dependencies between the plain JARs (see Figure 19.16).

Figure 19.16 Bottom-Up Strategy for Code Migration

(1) Place all plain JARs on the class path such that they are in the unnamed module.

(2) Choose leaf JARs from the class path—that is, JARs that do not have any dependencies. Turn them into explicit modules by adding appropriate exports directives in their module descriptors and move them from the class path to the module path.

(3) From the class path, choose plain JARs from the next higher level, that have direct module dependencies. Turn them into explicit modules. Their module declarations must specify requires directives on their dependencies and appropriate exports directives for packages they export. Move the newly created explicit modules from the class path to the module path.

(4) Repeat step (3) until the unnamed module is empty.

There are variations on the bottom-up strategy, but the gist is represented by the algorithm above. Note that when applying the bottom-up strategy for migration, the explicit module on the module path never accesses code in any plain JAR on the class path.

Figure 19.16 shows the bottom-up strategy applied to the plain JARs: main.jar, controller.jar, view.jar, and model.jar, that are converted to modular JARs—that is, explicit modules—as they are moved from the class path to the module path.

Top-Down Strategy for Code Migration

Migrating to modular code via automatic modules is convenient when it will take too long to wait for all dependencies to be available. The algorithm below migrates the code into explicit modules via automatic modules, based on the graph of known dependencies between the plain JARs (see Figure 19.17).

Figure 19.17 Top-Down Strategy for Code Migration

(1) Place all plain JARs on the module path. All plain JARs are now automatic modules.

(2) Choose root JARs that are automatic modules. A root JAR is one that no other JAR depends on. Turn these automatic modules into explicit modules by adding module declarations. Add requires directives using names of the automatic modules that a root JAR depends on.

(3) Choose automatic modules at the next lower level that are directly required by their parent modular JARs. Turn these automatic modules into explicit modules. Add requires directives using names of the automatic modules that these JARs depend on. Add appropriate exports directives for any packages exported.

(4) Repeat step (3) until all automatic modules have been converted to explicit modules.

Again, there are variations to the top-down strategy, but the algorithm embodies the main idea of this strategy. Note that in real life, both strategies will most likely be applied to complete the migration.

Figure 19.17 shows the top-down strategy applied to the plain JARs: main.jar, controller.jar, view.jar, and model.jar, which are first placed on the module path as automatic modules and then are converted to modular JARs—that is, explicit modules.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *