Modular Programming in Java 9

Book description

Kick-start your modular programming journey and gear up for the future of Java development

About This Book

  • Master design patterns and best practices to build truly modular applications in Java 9
  • Upgrade your old Java code to Java 9 with ease
  • Build and run a smooth functioning multi-module application.

Who This Book Is For

This book is written for Java developers who are interested in learning and understanding the techniques and best practices to build modular applications in Java. The book assumes some previous programming experience in Java 8 or earlier, familiarity with the basic Java types such as classes and interfaces, as well as experience in compiling and executing Java programs.

What You Will Learn

  • Get introduced to the concept of modules and modular programming by working on a fully modular Java application
  • Build and configure your own Java 9 modules
  • Work with multiple modules and establish inter-module dependencies
  • Understand and use the principles of encapsulation, readability, and accessibility
  • Use jlink to generate fully loaded custom runtime images like a pro
  • Discover the best practices to help you write awesome modules that are a joy to use and maintain
  • Upgrade your old Java code to use the new Java 9 module system

In Detail

The Java 9 module system is an important addition to the language that affects the way we design, write, and organize code and libraries in Java. It provides a new way to achieve maintainable code by the encapsulation of Java types, as well as a way to write better libraries that have clear interfaces. Effectively using the module system requires an understanding of how modules work and what the best practices of creating modules are.

This book will give you step-by-step instructions to create new modules as well as migrate code from earlier versions of Java to the Java 9 module system. You'll be working on a fully modular sample application and add features to it as you learn about Java modules. You'll learn how to create module definitions, setup inter-module dependencies, and use the built-in modules from the modular JDK. You will also learn about module resolution and how to use jlink to generate custom runtime images.

We will end our journey by taking a look at the road ahead. You will learn some powerful best practices that will help you as you start building modular applications. You will also learn how to upgrade an existing Java 8 codebase to Java 9, handle issues with libraries, and how to test Java 9 applications.

Style and Approach

The book is a step-by-step guide to understanding Modularity and building a complete application using a modular design.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Introducing Java 9 Modularity
    1. Modularity in Java
    2. Rethinking Java development with packages
      1. The unfortunate tale of a library developer
      2. The impossible task of a deployment engineer
      3. The classpath problem
    3. Java - the 20-year-old code base
      1. Legacy classes
        1. Internal APIs
    4. Java Platform Module System
    5. Project Jigsaw
    6. Summary
  3. Creating Your First Java Module
    1. Setting up the JDK
    2. Switching between JDKs
    3. Setting up the NetBeans IDE
    4. Java 9 modules
      1. Traditional Java code structure
      2. What is a module?
      3. Creating a module
      4. Creating your first Java module
      5. Compiling your module
      6. Executing your module
    5. Creating a module using NetBeans
    6. The address book viewer application
    7. Handling possible errors
    8. Summary
  4. Handling Inter-Module Dependencies
    1. Creating the second module
    2. Compiling two modules
    3. Configuring module dependency
    4. Module versioning
    5. Rethinking package structure
    6. Understanding module path arguments
    7. Revisiting classpath
    8. Revisiting the classpath problems
    9. Summary
  5. Introducing the Modular JDK
    1. Examining the legacy JDK
      1. The JRE structure
        1. Understanding rt.jar
        2. The problem with this model
          1. The attempted solution - Compact profiles
      2. The state of API encapsulation
        1. Understanding internal APIs
        2. The problem with this model
          1. The attempted solution - Deprecation, warnings, and documentation
    2. Enter Project Jigsaw
    3. Platform modularity
      1. The impact of platform modularity
      2. Module graph
      3. The java.base module
      4. Browsing modules
      5. Module types
      6. Examining platform file structure
    4. Observable modules
    5. Revisiting the two problems
      1. Solving the monolithic runtime
      2. Solving the API encapsulation problem
    6. Summary
  6. Using Platform APIs
    1. Adding logging ability
      1. Using an alternative compiler command
    2. Reading contacts from an XML file
      1. Removing the hard-coded contact list
      2. Creating the module
      3. Coding the XmlUtil class
      4. Coding the ContactLoader class
        1. Shared classes
        2. Dependency leakage
      5. Consuming the new module
    3. Adding UI with Java FX
      1. Creating the module
    4. Summary
  7. Module Resolution, Readability, and Accessibility
    1. Readability
    2. Accessibility
      1. Interface and implementation accessibility
      2. Split packages
    3. Tweaking modularity
      1. Implied readability
      2. Aggregator modules
        1. Java platform aggregator modules
      3. Qualified exports
    4. Applying the concepts to address book viewer
      1. Creating a custom aggregator module
      2. Optimizing module imports
      3. Optimizing module exports
    5. Summary
  8. Introducing Services
    1. The problem of coupling
    2. Understanding services
      1. The service registry
      2. Creating and using services
      3. Implementing sorting services
      4. Drawing the module graph
    3. Advanced services
      1. Supporting singleton and factory providers
      2. Implementing service priorities
      3. Service interface provider lookup
      4. Selective service instantiation
    4. Services and the module system goals
    5. Summary
  9. Understanding Linking and Using jlink
    1. Module resolution process
      1. Module resolution steps
      2. Examining module resolution in action
    2. Revisiting the state of the JDK
    3. Linking using jlink
      1. The jlink command
      2. Link phase optimizations and jlink plugins
    4. Building a modular JAR file
    5. Summary
  10. Module Design Patterns and Strategies
    1. Designing modules
      1. Scoping
      2. Team structure
      3. Reusability
    2. Modularizing by concerns
    3. Modularizing by layers
    4. Modularizing by change patterns
    5. Designing API
    6. Java modules coding patterns and strategies
      1. Pattern 1 - Public interface, private implementation, and factory class
        1. Example
      2. Pattern 2 - Services for multiple dynamic implementations
        1. Example
      3. Pattern 3 - Optional dependencies
        1. Example
      4. Pattern 4 - Optional dependencies using services
        1. Example
      5. Pattern 6 - Bundle model classes as separate sharable modules
      6. Pattern 7 - Open modules for reflection
        1. Example
      7. Pattern 8 - Use tooling for version control
      8. Pattern 9 - Design for changes
      9. Pattern 10 - Protect against dependency leakage
      10. Pattern 11 - Aggregator and facade modules
      11. Example
    7. Summary
  11. Preparing Your Code for Java 9
    1. Beginning Java 9 migration
    2. Introducing the sample Java 8 application
    3. Using the Java 9 compiler and runtime
      1. The unnamed module
      2. Handling non-standard access
        1. The jdeps tool
      3. Overriding module behavior
    4. Understanding the impact
    5. Recommended strategy
    6. Summary
  12. Migrating Your Code to Java 9
    1. Understanding the migration goal
    2. Beginning the migration
      1. Automatic modules
        1. Automatic module naming
        2. Automatic module definition
      2. Migrating with automatic modules
      3. Using jdeps to outline module relationships
      4. Refactoring into smaller modules
    3. Handling larger codebases
    4. Migrating libraries
      1. Reserving library names
      2. Using jdeps to create module descriptors
    5. Building libraries for multiple Java versions
    6. Summary
  13. Using Build Tools and Testing Java Modules
    1. Integrating with Apache Maven
      1. A Maven refresher
      2. Using Maven with Java 9 modules
      3. Working on a multi-module Java 9 Maven project
        1. Building the multi-module project
        2. Executing the multi-module project
        3. Understanding the exec plugin's module path
    2. Unit testing modules with Java 9
      1. Testing a Java 9 module
        1. Integrating with JUnit
    3. Wrapping up
    4. Summary

Product information

  • Title: Modular Programming in Java 9
  • Author(s): Koushik Kothagal
  • Release date: August 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787126909