R
What expressive gain do we have when using the new module system?Here, as far as I could read and feel the taste (I couldn't help a big sip, just a vine at the tip of the tongue) of Jigsaw, are two main advantages:you can create classes that will not expose and use in any package of your moduleoptimization of class search by classpathThe first part even I felt when trying to put a legacy code to run with Java 12: Oracle has now decided to actually hide her intimate implementations.I don't know if you remember, but you know the packages sun.java.*? Well, they were internal packages that should be consumed only by JRE/JDK of Sun/Oracle. In thesis, no program made to run in Java should depend on these packages. What was it possible to do with Jigsaw? Remove them from path Then what happened to a legacy library that I had to access sun.misc.BASE64Encoder? ♪ ClassNotFoundException and the system stopped.Honestly, did I find it bad? No, as incredible as it sounds, I didn't find it bad. Just curious. As the code to this library was lost in space/time (and it is a depreciated artifact), it only served as another push to abandon it.Now, how you get an improved performance in the search for classpath?Let's go back to the class that I had no more access, sun.misc.Base64Encoder. When you are in a module, the first search will be for classes of that module. So in the case of the module java.base (Let's pretend, okay? I don't know what the real module would be), at some point it was necessary to access the class sun.misc.Base64Encoder. The processing was within the module java.base, then the first thing he does is to inquire inside the module who is sun.misc.Base64Encoder and he finds that class, public.Now we're in my implementation. Suppose it is properly ported to Java 9 and tries to access sun.misc.Base64Encoder. The first thing that ClassLoader will try to do it will be to see, within the module in which I sit, if you have any class with that signature. The answer will be no. So at that time, I also go through all the other modules of the classpath that I declare to use (or inherited via transitive) and see if you have someone who provides the package sun.misc, without entering them deeply for further inspections. Perhaps one or the other library in Java 8- offers some resistance because, by default, they behave like modules with all available packages.Have you seen how search time is reduced using modules?In terms of complexity, how easy is your applicability in large projects that are already running in Java 8 for example?Can you modulate your program well? If so, the first step will be to make a module-info.java with the following information:what are the modules I depend on? requiresfor each required module, should I leave available to third parties that matter from my module or not? transitivewhich packages will programmatically expose so that it is grafted as part of classpath for other modules? exportswhich classes would like to be accessible via reflection, even if it is not "available" to access directly by ClassLoader´?opensThis step of the opens is the most delicate when using a framework that relies heavily on reflections (such as Spring, Hibernate, MyBatis etc).Setting all this correctly, and making proper separation of modules, the rest should flow naturally. I particularly see more Jigsaw application for libraries, but I have not seen reasons to prevent an application from being separated into modules.Now these definitions are not trivial to do. Perhaps a reengineering of the original code to hide the implementation by moving it to another package is necessary.During library upgrade, back and a half came across conflicts between versions and dependencies. In this modularization scenario, does this problem become more common?One advantage that this scheme provides is being able to have several classes with the same name. The only requirement for this is that they are not exported packages. A very well-balled library can make proper use of this, but most of it is missing with target for Java 8.Another point is that this does not exime dependency management by Maven or Gradle (or what you use for this purpose). So I don't see how the use of modules would decrease these upgrade problems (with possible class exceptions with the same signature in two distinct modules).References: https://www.baeldung.com/project-jigsaw-java-modularity https://openjdk.java.net/projects/jigsaw/