Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practicing The Java Module System

Practicing The Java Module System

Practicing The Java Module System presentation - Devoxx PL

Avatar for Rahman Usta

Rahman Usta

June 21, 2018

Other Decks in Programming

Transcript

  1. Agenda • Concept introduction • Practice times! http://bit.ly/java9-workshop 1. Generate

    JREs 2. Create a hello world module 3. Control access between modules 4. Using Auto-modules 5. Using Unnamed modules 6. Testing HTTP/2 Client API 7. Testing JSHell
  2. Versions history 1995 ...> First release 1996 ...> Java 1.0

    1997 ...> Java 1.1 (Reflection, JDBC) 1998 …> Java 1.2 (JIT, Swing) 2000 …> Java 1.3 (RMI, JNDI) 2002 …> Java 1.4 (Nio, Logging) 2004 …> Java 5 (Generics, Annotations) 2006 …> Java 6 (JDBC 4.0, Scripting) 2011 …> Java 7 (Project Coin, Fork-Join, Nio 2) 2014 …> Java 8 (Lambda, Method Ref, Stream API) 2017 …> Java 9 2018 (March) …> Java 10
  3. Java 9 New features Jigsaw Related Changes • JEP 200

    - The Modular JDK • JEP 201 - Modular Source Code • JEP 220 - Modular Run-Time Images • JEP 260 - Encapsulate Most Internal APIs • JEP 261 - Module System • JEP 282 - The Java Linker • JEP 238 - Multi-Release JAR Files 90+ new features http://openjdk.java.net/projects/jdk9/ • Other changes • Project Kulla (JShell) • HTTP/2 Client API • Flow API • Collections factories • API changes: Process API, • Stream API e.g.
  4. Goals of the Project Jigsaw • To increase access control

    on the Java components • To increase security by removing access to propietry APIs • To increase performance of the Java platform • To get rid of the monolithic JDK • To modularize JDK source code • To bring Scalable JREs
  5. Monolithic X Monolithic Kernel => Micro Kernel Monolithic Services =>

    Micro Services Monolithic JDK => Modular JDK
  6. Practice time! Let’s create different scale of JREs with JLink

    tool. Resources: http://bit.ly/java9-workshop
  7. What is Java module ? • Associated Java package(s) are

    grouped by a name. • Brings access control between modules $ java -d java.sql
  8. How to define a Java Module? Module definitions • exports

    • requires • opens • provides, with
  9. New parameters for Java tools --p <module path> --module-path <module

    path>... A ; separated list of directories, each directory is a directory of modules. --add-modules Adds modules to resolution list -m <module>[/<mainclass>] Root module and main class
  10. Practice time! Let’s create a modular Hello world! application •

    Create a module • Package a module • Run a module • Link a module with JRE
  11. Access modifiers (< Java 9) Brings accessibility levels to Java

    components (class, method, field) • public • protected • package-private • private No Access Control on Package Level
  12. Access modifiers (>= Java 9) Brings accessibility levels to Java

    components (module, class, method, field) public to all modules public only to specific module(s) public only to its own module private protected package Has Access Control on Package Level
  13. Qualified vs Unqualified exports Qualified Export package is readable by

    a spesific module exports com.a.b.c to com.to.a.module Unqualified Export package is readable by any module exports com.a.b.c;
  14. Named module Standard Java module declared with module-info.java Modules are

    loaded from --module-path For example: java --module-path lib/abc-1.0.jar;...
  15. Practice time! Let’s use a non-modular JAR file in a

    modular Java application. • Auto-modules are helpful for migration • Uses jansi library to print colored texts!
  16. Practice time! Let’s try to access from a non-module to

    a module • Check compile time access
  17. Java 9 and Reflection Java Reflection API is one of

    the most helpful API for the library developers. Many open-source libraries use it for example: Hibernate, Spring Framework. • Access Java classes, modules, fields, methods, constructors e.g. • Create Java objects • Call Java methods • Access private methods and fields • Access annotations
  18. Reflective Access between modules If a module want to access

    to other module’s components, other component have to open or export required packages to caller module. exports: Makes packages accessible to other modules at compile time or run time. opens: Makes packages accessible to other modules at run time including access to private members.
  19. How to open a package or module Three ways: •

    opens com.a.b.c; • open module com.a.module { // open module } • --add-opens com.a.module/com.a.b.c=com.other.module
  20. HTTP/2 Client API (an incubator module) module com.kodedu.http2 { requires

    jdk.incubator.httpclient; } HttpResponse response = HttpRequest .create(URI.create("https://devoxx.pl")) .GET() .response(); System.out.println(response.body(HttpResponse.asString()));