Modular Source Code in java 9



What is Modular Source Code?
  • Reorganizing the JDK source code into modules,
  • Enhance the build system to compile modules and
  • Enforce module boundaries at build time.


Modular Source Code will not change the structure of the JRE and JDK binary images.

Motivation behind Modular JDK is Jigsaw Project.
The primary goals of this Jigsaw Project -
  • To make JDK and Java SE Platform easily scalable
  • To make JDK and Java SE Platform more maintainable.
  • To make JDK and Java SE Platform Secure.
  • To improve performance of JDK and Java SE Platform.
  • To make it easier to add libraries in JDK and Java SE Platform.

Also, to enable the Project Jigsaw development without making it mandatory to switch current non-modular source code into modular form.


Description
Before java 9 - How is JDK source code organized >
src/{share,$OS}/{classes,native}/$PACKAGE/*.{java,c,h,cpp,hpp}

Let’s understand above line >
  • share - It is directory which contains shared and cross-platform code.
  • $OS - It is directory which contains OS (operating system) specific code.
  • classes - It is directory which contains Java source files and resource files.
  • native - It is directory which C or C++ source files
  • $PACKAGE - Java API package name (periods are replaced by slashes)

In java 9 - Modular source code will be implemented in all JDK directories except for hotspot >
src/$MODULE/{share,$OS}/classes/$PACKAGE/*.java
                       native/include/*.{h,hpp}
                              $LIBRARY/*.{c,cpp}
                       conf/*

where:
  • $MODULE - It is module name (Example - java.base)
  • share
  • $OS
  • Classes
  • native - It is directory which C or C++ source files, but source code will be organized differently in java 9 -
    • include - It is directory which contains C or C++ header files. (Example -  jni.h)
    • $LIBRARY - It is directory which contains C or C++ source files. (Example - libjava or libawt)
  • conf - It is directory which contains configuration files which can be edited by end users (Example - net.properties)

Before Java 9 >
Example, source code of java.lang.Object class is there in two files in jdk -
  • In java file
  • And c file
src/share/classes/java/lang/Object.java
         native/java/lang/Object.c

Changes in Modular source code in Java 9 >
source code of java.lang.Object class will be organized in different way -
src/java.base/share/classes/java/lang/Object.java
                   native/libjava/Object.c



Before Java 9 >
Example, source code of java.lang.ProcessImpl and ProcessEnvironment classes in unix is there in 3 files in jdk -
src/unix/classes/java/lang/ProcessImpl.java
                             ProcessEnvironment.java
           native/java/lang/ProcessEnvironment_md.c

Changes in Modular source code in java 9 >
source code of java.lang.ProcessImpl and ProcessEnvironment classes will be organized in different way -
src/java.base/unix/classes/java/lang/ProcessImpl.java
                                    ProcessEnvironment.java
                  native/libjava/ProcessEnvironment_md.c



Modifications in Build system >
The modifications will be made in build system to compile one module at a time rather than one repository at a time.
Modules that do not depend on each other will be compiled concurrently whenever possible.

What will be advantages of compiling modules rather than repositories >
It will be possible for code in the corba, jaxp, and jaxws repositories to use new Java language features and APIs. Befor java 9 this access was forbidden because those repositories were compiled before the jdk repository.

Before java 9 -
The compiled classes in an intermediate build -
jdk/classes/*.class

In java 9 -
The compiled classes in an intermediate build will be divided into modules like this.
jdk/modules/$MODULE/*.class


What about image builds?
The structure of image builds will not change, there will be very minor differences in their content.

Let’s discuss few alternatives to Modular source code >

  1. Keep {share,$OS} at the top.
modules directory will contain module class files.
src/{share,$OS}/modules/$MODULE/$PACKAGE/*.java
               native/include/*.{h,hpp}
                      $LIBRARY/*.{c,cpp}
               conf/*

  1. keep{share,$OS} at the top,
Put everything under appropriate $MODULE directory
src/{share,$OS}/$MODULE/classes/$PACKAGE/*.java
                       native/include/*.{h,hpp}
                              $LIBRARY/*.{c,cpp}
                       conf/*






Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

varHandle (variable handle) in java 9

Improved JVM compiler control and handling in java 9

JVM logging system in Java 9 - Levels, tags, output, rotation, decoration



Labels: Java 9
Must read for you :