Refactoring your code to Java 9 modules

Refactoring your code to Java 9 modules

GeekOut, Tallinn, Estonia
Code: https://github.com/rgra/java9-module-refactoring

363cf27411356afc37199a73bb58bcfe?s=128

Rabea Gransberger

June 09, 2017
Tweet

Transcript

  1. Refactoring your code to Java 9 modules Rabea Gransberger (MEKO-S

    GmbH) @rgransberger
  2. Rabea Gransberger • Computer Science Diploma 2008 • Java Developer,

    Project Lead at MEKOS, Germany • Organization Team JUG Bremen, JavaLand, JCrete 09.06.2017 2 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  3. 09.06.2017 3 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  4. 09.06.2017 4 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    Carola Lilienthal: It's all about the domain, honey!
  5. 09.06.2017 5 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  6. 09.06.2017 6 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    https://c2.staticflickr.com/4/3206 /2348940045_40482b7109_b.jpg
  7. Modules customer.service invoice.service picture.service camera.service 09.06.2017 7 GeekOut / Java

    9 Refactoring (Rabea Gransberger @rgransberger)
  8. OTIS • 155.159 LOC • 42.528 Classes • 3884 Packages

    • 285 Plugins (aka Modules) 09.06.2017 8 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  9. At the Vet 09.06.2017 9 GeekOut / Java 9 Refactoring

    (Rabea Gransberger @rgransberger) Java 8 JavaFX Sample Application
  10. 09.06.2017 10 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  11. Entities 09.06.2017 11 GeekOut / Java 9 Refactoring (Rabea Gransberger

    @rgransberger) Customer Pet Visit Invoice
  12. 09.06.2017 12 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  13. My friend … 09.06.2017 13 GeekOut / Java 9 Refactoring

    (Rabea Gransberger @rgransberger)
  14. 09.06.2017 14 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    Work Items Cars Brand Repair
  15. Vet Entities 09.06.2017 15 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger) Customer Pet Visit Invoice
  16. Car Repair Entities 09.06.2017 16 GeekOut / Java 9 Refactoring

    (Rabea Gransberger @rgransberger) Customer Pet Visit Invoice Car
  17. Identifiy Modules • Parts without cyclic dependencies • Tools •

    Structure 101 • Sonargraph • UML • jQAssistant • Degraph 09.06.2017 17 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) customer.service invoice.service
  18. 1. Analysis 09.06.2017 18 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger)
  19. 09.06.2017 19 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  20. 09.06.2017 20 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  21. 09.06.2017 21 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  22. Domain Layers 09.06.2017 22 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger) Customer Pet Visit Invoice
  23. Technical Layers 09.06.2017 23 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger) Customer Pet Visit Invoice DB Model UI
  24. 1. Refactoring: Technical Layers 09.06.2017 24 GeekOut / Java 9

    Refactoring (Rabea Gransberger @rgransberger) customer customer. model customer. db customer. ui
  25. Technical Layers 09.06.2017 25 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger) Customer Pet Visit Invoice DB Model UI
  26. 2. Analysis 09.06.2017 26 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger)
  27. Java 9 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger

    @rgransberger) 27
  28. Java 9: Early Access • Java 9 Early Acccess Build

    172 • Release Date: 21.9.2017 • Eclipse • Download Oxygen (Eclipse SDK) • Patch Java 9 Support (BETA) for Oxygen • IntelliJ • IntelliJ IDEA 2017.1 • NetBeans • Nightly Builds 09.06.2017 28 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) Test before your customers ;)
  29. 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) 29

    102: Process API Updates 110: HTTP 2 Client 143: Improve Contended Locking 158: Unified JVM Logging 165: Compiler Control 193: Variable Handles 197: Segmented Code Cache 199: Smart Java Compilation, Phase Two 200: The Modular JDK 201: Modular Source Code 211: Elide Deprecation Warnings on Import Statements 212: Resolve Lint and Doclint Warnings 213: Milling Project Coin 214: Remove GC Combinations Deprecated in JDK 8 215: Tiered Attribution for javac 216: Process Import Statements Correctly 217: Annotations Pipeline 2.0 219: Datagram Transport Layer Security (DTLS) 220: Modular Run-Time Images 221: Simplified Doclet API 222: jshell: The Java Shell (Read-Eval-Print Loop) 223: New Version-String Scheme 224: HTML5 Javadoc 225: Javadoc Search 226: UTF-8 Property Files 227: Unicode 7.0 228: Add More Diagnostic Commands 229: Create PKCS12 Keystores by Default 231: Remove Launch-Time JRE Version Selection 232: Improve Secure Application Performance 233: Generate Run-Time Compiler Tests Automatically 235: Test Class-File Attributes Generated by javac 236: Parser API for Nashorn 237: Linux/AArch64 Port 238: Multi-Release JAR Files 240: Remove the JVM TI hprof Agent 241: Remove the jhat Tool 243: Java-Level JVM Compiler Interface 244: TLS Application-Layer Protocol Negotiation Extension 245: Validate JVM Command-Line Flag Arguments 246: Leverage CPU Instructions for GHASH and RSA 247: Compile for Older Platform Versions 248: Make G1 the Default Garbage Collector 249: OCSP Stapling for TLS 250: Store Interned Strings in CDS Archives 251: Multi-Resolution Images 252: Use CLDR Locale Data by Default 253: Prepare JavaFX UI Controls & CSS APIs for Modularization 254: Compact Strings 255: Merge Selected Xerces 2.11.0 Updates into JAXP 256: BeanInfo Annotations 257: Update JavaFX/Media to Newer Version of GStreamer 258: HarfBuzz Font-Layout Engine 259: Stack-Walking API 260: Encapsulate Most Internal APIs 261: Module System 262: TIFF Image I/O 263: HiDPI Graphics on Windows and Linux 264: Platform Logging API and Service 265: Marlin Graphics Renderer 265: Marlin Graphics Renderer 266: More Concurrency Updates 267: Unicode 8.0 268: XML Catalogs 269: Convenience Factory Methods for Collections 270: Reserved Stack Areas for Critical Sections 271: Unified GC Logging 272: Platform-Specific Desktop Features 273: DRBG-Based SecureRandom Implementations 274: Enhanced Method Handles 275: Modular Java Application Packaging 276: Dynamic Linking of Language-Defined Object Models 277: Enhanced Deprecation 278: Additional Tests for Humongous Objects in G1 279: Improve Test-Failure Troubleshooting 280: Indify String Concatenation 281: HotSpot C++ Unit-Test Framework 282: jlink: The Java Linker 283: Enable GTK 3 on Linux 284: New HotSpot Build System 285: Spin-Wait Hints 287: SHA-3 Hash Algorithms 288: Disable SHA-1 Certificates 289: Deprecate the Applet API 290: Filter Incoming Serialization Data 292: Implement Selected ECMAScript 6 Features in Nashorn 294: Linux/s390x Port 295: Ahead-of-Time Compilation 297: Unified arm32/arm64 Port 298: Remove Demos and Samples
  30. Project Jigsaw • JSR 376: Java Platform Module System •

    Old: Classpath (-cp) • New: Module-Path (-p) 09.06.2017 30 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) Copyright Oracle
  31. Java 9 Module Definition src\module-info.java module <name> { requires <Module>;

    exports <package>; uses <Service>; provides <Service> with <Impl>; } 09.06.2017 31 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  32. Module 09.06.2017 32 GeekOut / Java 9 Refactoring (Rabea Gransberger

    @rgransberger) model ui db util requires module model { exports db; } module ui { requires model; exports table; } table CustomerTable Customer getCustomer()
  33. Module 09.06.2017 33 GeekOut / Java 9 Refactoring (Rabea Gransberger

    @rgransberger) model ui db util table requires module model { exports db; } module ui { requires model; exports table; }
  34. 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) 34

    Customer Pet Visit Invoice Car
  35. New Modules 09.06.2017 35 GeekOut / Java 9 Refactoring (Rabea

    Gransberger @rgransberger) model customer.model visit.model invoice.model ui customer.ui visit.ui invoice.ui pet pet.model pet.db pet.ui vet ui
  36. 2. Refactoring: Create Modules 09.06.2017 36 GeekOut / Java 9

    Refactoring (Rabea Gransberger @rgransberger) • Warning: No Split-Packages allowed model customer ui customer
  37. 09.06.2017 37 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

  38. CustomerView private Node createTabPane() { TabPane tabPane = new TabPane();

    tabPane.getTabs().add(createTab("Pets", createPetNode())); tabPane.getTabs().add(createTab("Invoices", createInvoiceNode())); return tabPane; } private Node createPetNode() { List<Pet> pets = new PetDao().fetchPetsForCustomer(customer.getId()); PetTable table = new PetTable(pets, false) { protected void onDoubleClick(Pet pet) { replace(new PetView(customer, pet)); } }; return table; } 09.06.2017 38 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  39. 3. Refactoring: Resolve Dependencies 09.06.2017 39 GeekOut / Java 9

    Refactoring (Rabea Gransberger @rgransberger) ui CustomerView TabService List<Tab> createTabs() pet PetTabService List<Tab> createTabs() PetTable module ui { exports service; uses CustomerViewTabService; } module pet { requires ui; provides CustomerViewTabService with PetCustomerViewTabService; } Service Loader
  40. 4. Refactoring: Car Repair Shop • de.rgra.car replaces de.rgra.pet •

    de.rgra.vet.ui transformed to de.rgra.app • New Service: ApplicationConfigurationProvider • New: de.rgra.vet.app and de.rgra.car.app • Implement ApplicationConfigurationProvider 09.06.2017 40 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  41. 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) 41

  42. Jigsaw Dependency Visualizer 09.06.2017 42 GeekOut / Java 9 Refactoring

    (Rabea Gransberger @rgransberger)
  43. Deployment 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    43
  44. Compile • %JAVA_9%\javac --module-path mods/vet -d mods/vet/de.rgra.model_05 @sources.txt • %JAVA_9%\jar

    --create --file mlib/vet/de.rgra.model.jar -C mods/vet/de.rgra.model_05 . 09.06.2017 44 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  45. Run • %JAVA_9%\java -p mods/vet -m de.rgra.vet.app_05/de.rgra.vet.VetAppConfiguration 09.06.2017 45 GeekOut

    / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  46. JLink – Java Linker • %JAVA_9%/jlink --module-path %JAVA_9%\jmods;mlib/vet --add-modules de.rgra.vet.app_05

    --output vetapp --launcher vet=de.rgra.vet.app_05/de.rgra.vet.VetAppConfiguration • run vetapp/bin/vet.bat 09.06.2017 46 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  47. Summary 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    47
  48. 09.06.2017 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) 48

  49. GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger) 09.06.2017 49

  50. Refactoring Steps Package Structure *: <domain>.<product>.<deployable> .<businesslayer> .<technicallayer>.<subpackage> 1. Analyse

    dependencies 2. Identifiy / Create Modules 3. Move Code 4. Resolve Dependencies with Services 5. Go to 1. 09.06.2017 50 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  51. 09.06.2017 51 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)

    https://i.ytimg.com/vi/RijScDMVQzQ/maxresdefault.jpg "The only thing a big bang rewrite guarantees is a Big Bang!" (M. Fowler)
  52. Advantages Module vs. Monolith • Architecture reflected in modules with

    clear boundaries • Hide public classes • Replace single modules • Module Path more robust than Classpath • Runtime Errors (NoClassDefFoundError) detected at startup, not after x hours • Jar Hell: 2 versions of Jars in Classpath • Tool Support: Which Dependencies are missing? • Smaller deployable units • Better (Startup-) Performance 09.06.2017 52 GeekOut / Java 9 Refactoring (Rabea Gransberger @rgransberger)
  53. Slides & Code: https://rgra.github.io Twitter: @rgransberger 09.06.2017 53 GeekOut /

    Java 9 Refactoring (Rabea Gransberger @rgransberger)