Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're discussing how to identify and isolate modules in your current codebase. This is crucial for a successful migration to JPMS.
What does it mean to isolate a module?
Good question! Isolating a module means identifying a self-contained group of packages that can work together without unnecessary dependencies on other parts of the code. It enhances encapsulation.
How do we know which packages should be grouped?
You should look for related functionalities. For example, all database-related packages can be part of a database module. A mnemonic to remember this is 'R.E.L.A.T.E.': Related Elements Linked As a Team Effectively.
So are we looking for cohesion within the packages?
Exactly! Cohesion is vital. At the end of this conversation, remember: identify, isolate, and group related packages.
Now that we have identified our modules, let's talk about creating the module-info.java file. This file acts as our module descriptor.
What goes into this file?
It includes the module name, the packages it exports, and any modules it requires. You can think of it as the module's 'business card'.
Can you show us an example?
"Absolutely! For instance:
Next, let's move on to the libraries—specifically how to transition them to the module path.
What challenges might I face in this step?
You might encounter issues with libraries that weren't designed for JPMS. It's important to check compatibility.
How do I know if a library is compatible?
Look for libraries that provide modular JARs or verify compatibility in their documentation. A quick tip: a library designed for JPMS will usually mention it clearly.
So how do I actually move them?
Adjust your build path to include these libraries in the module path instead of the classpath. Remember: 'M.P. for L.P.' - Move Packages to Library Path!
Let's explore the directives you’ll use in the module-info.java file: requires, exports, and opens.
What does each directive do?
requires denotes dependencies, exports allows access to certain packages, and opens grants reflective access. You can remember this sequence as 'R.E.O.' — Requires, Exports, Opens.
Will everything need to be exported?
Not necessarily. Only export packages that are meant to be used by other modules. Think of it as deciding which doors to open in your house.
What about the opens directive?
Use opens for packages that need reflection, like when using frameworks. Remember: ‘O’ is for ‘Openness’ in reflective programming.
The last focus of our session is refactoring your code for reflective access using the opens directive.
Why do we need to refactor for reflection?
Some libraries rely on reflection to implement features. Without opens, they can't access those internal packages.
How do I know which packages need opens?
Identify the packages used by the reflective libraries. Document this to ensure transparency. The key phrase here is 'Open Up for Access!'
Will this affect performance?
It can slightly affect performance due to reflection overhead but is balanced by the flexibility it offers.
The process of analyzing code to group related packages into modules.
A descriptor file defining module properties.
Specifies which modules are dependencies.
Makes specific packages available to other modules.
Allows reflective access to specific packages.
Example of module-info.java for a simple application module:
module com.example.myapp {
requires com.utils;
exports com.example.myapp.api;
}```
- To refactor for reflection, you might use:
```java
opens com.example.myapp.internal;``` to allow reflective libraries to access internal packages.To migrate your code with great ease, Identify, isolate; start to please! With module-info, dependencies we seize.
Imagine a library with many rooms. Each room is a package. You need to mark which rooms are open to visitors (exports) and which rooms are only for staff (internal use). This is what module-info.java does for your code.
Remember R.E.O: Requires, Exports, Opens - it helps you recall the order of directives in module-info.java.
I.I.G.: Identify, Isolate, Group - the steps to take when starting module migration.
Term
What is module-info.java?
Click to flip
Term
What does the 'requires' directive do?
Click to flip
Term
What is the purpose of the 'exports' directive?
Click to flip
Term
What is the role of the 'opens' directive?
Click to flip
A Java file that acts as a module descriptor, specifying module details such as dependencies and exported packages.
Java Platform Module System, introduced in Java 9, enabling modularization of Java applications.
A directive in the module-info.java that specifies dependencies on other modules.
A directive that allows specific packages to be made accessible to other modules.
A directive that allows reflective access to the packages by other modules.
Code that does not use the module system to structure dependencies and organization.
A runtime path for locating modules, contrasting with the classpath used for traditional Java applications.
This section outlines the steps to migrate Java code from a non-modular structure to a modular one using the Java Platform Module System (JPMS).
The migration from non-modular to modular code involves identifying modules, creating module descriptors, moving libraries to the module path, and refactoring code for reflection. These steps enhance modular organization and code manageability in Java applications.
Migrating existing Java applications from a non-modular to a modular architecture using the Java Platform Module System (JPMS) is crucial for leveraging the advantages of modularization. The primary steps to achieve this migration include:
module-info.java file that defines the module's name, its dependencies (using the requires directive), and the packages it exports (using the exports directive).module-info.java files where necessary.requires, exports, and opens directives to control access and encapsulation based on the module's requirements.opens directive to allow reflective access.Completing these steps not only facilitates the migration process itself but also sets a foundation for improved scalability, maintainability, and security of Java applications moving forward.