Project Jigsaw introduces a modular system to Java to address issues with non-modular code. It divides the Java platform API into named modules that clearly define dependencies and exported packages. This avoids issues with classes from sun.* and com.sun.* packages being inaccessible or the huge size of rt.jar. The module system allows modular and non-modular code to coexist through automatic modules and the unnamed module. Tools like jdeps and jlink are provided to help with adoption and management of modules.
23. import com.sun.javafx.tk.FontMetrics;
import com.sun.javafx.tk.Toolkit;
import javafx.scene.text.Font;
public class FontSample {
public static void main(String... args) {
Font font = Font.font(24);
Toolkit toolkit = Toolkit.getToolkit();
FontMetrics metrics
= toolkit.getFontLoader().getFontMetrics(font);
System.out.println(metrics.getLineHeight());
}
}
24. C:fxsample>javac FontSample.java
FontSample.java:1: error: package com.sun.javafx.tk is not
visible
import com.sun.javafx.tk.FontMetrics;
^
(package com.sun.javafx.tk is declared in module javafx.
graphics, which does not export it to the unnamed module)
FontSample.java:2: error: package com.sun.javafx.tk is not
visible
import com.sun.javafx.tk.Toolkit;
^
(package com.sun.javafx.tk is declared in module javafx.
graphics, which does not export it to the unnamed module)
2 errors
25. import com.sun.javafx.tk.FontMetrics;
import com.sun.javafx.tk.Toolkit;
import javafx.scene.text.Font;
public class FontSample {
public static void main(String... args) {
Font font = Font.font(24);
Toolkit toolkit = Toolkit.getToolkit();
FontMetrics metrics
= toolkit.getFontLoader().getFontMetrics(font);
System.out.println(metrics.getLineHeight());
}
}
29. Solution
add export by
--add-exports option
--add-exports module/package=target
javac
java
when non-modular system
target is ALL-UNNAMED
ex.
javac --add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED
FontSample.java
30. public class FontSample {
public static void main(String... args) throws Exception {
Font font = Font.font(24);
FontMetrics metrics
= Toolkit.getToolkit().getFontLoader().getFontMetrics(font);
Class<FontMetrics> metricsClass = FontMetrics.class;
Field lineHeight
= metricsClass.getDeclaredField("lineHeight");
lineHeight.setAccessible(true);
System.out.println(lineHeight.getFloat(metrics));
}
}
31. C:fxsample>javac --add-exports
javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED
FontSample.java
C:fxsample>java --add-exports
javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED FontSample
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by FontSample (file:/C:/fxsa
mple/) to field com.sun.javafx.tk.FontMetrics.lineHeight
WARNING: Please consider reporting this to the maintainers of
FontSample
WARNING: Use --illegal-access=warn to enable warnings of
further illegal reflective access operations
WARNING: All illegal access operations will be denied in a futu
re release
31.921875
32. public class FontSample {
public static void main(String... args) throws Exception {
Font font = Font.font(24);
FontMetrics metrics
= Toolkit.getToolkit().getFontLoader().getFontMetrics(font);
Class<FontMetrics> metricsClass = FontMetrics.class;
Field lineHeight
= metricsClass.getDeclaredField("lineHeight");
lineHeight.setAccessible(true);
System.out.println(lineHeight.getFloat(metrics));
}
}
34. Non-Disclosure API
Reflection
Reflective access to public
--add-exports
Reflective access to non-public
--add-opens in execution
using setAccessible(true)
ex.
java --add-opens javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED
FontSample.java
35. import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import net.javainthebox.xml.Name;
public class JAXBSample {
public static void main(String... args) throws JAXBException {
JAXBContext context
= JAXBContext.newInstance("net.javainthebox.xml");
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("sakuraba.xml");
Name sakuraba = (Name)unmarshaller.unmarshal(file);
System.out.println(sakuraba.getFirst()
+ " " + sakuraba.getLast());
}
}
36. C:jaxbsample>javac JAXBSample.java
netjavaintheboxxmlName.java:11: error: package javax.xml.
bind.annotation is not visible
import javax.xml.bind.annotation.XmlAccessType;
^
(package javax.xml.bind.annotation is declared in module
java.xml.bind, which is not in the module graph)
<<skip the rest>>
37. import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import net.javainthebox.xml.Name;
public class JAXBSample {
public static void main(String... args) throws JAXBException {
JAXBContext context
= JAXBContext.newInstance("net.javainthebox.xml");
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("sakuraba.xml");
Name sakuraba = (Name)unmarshaller.unmarshal(file);
System.out.println(sakuraba.getFirst()
+ " " + sakuraba.getLast());
}
}
40. Non-Standard Module
Modules not included
in standard modulepath
JAXB
JAX-WS
CORBA
java.xml.bind
java.xml.ws
java.corba
These modules will be removed in future
You should use the modules supplied by Java EE
41. Non-Standard Module
Modules not included
in standard modulepath
JAXB
JAX-WS
CORBA
java.xml.bind
java.xml.ws
java.corba
These modules will be removed in future
You should use the modules supplied by Java EE
Incubator jdk.incubator.httpclient
45. In Java SE 8, Only classpath is Used
Module and Non-Module are Used
In Java SE 9,
modulepath classpath
46. In Java SE 8, Only classpath is Used
Module and Non-Module are Used
In Java SE 9,
modulepath classpath
specified in module-info.java
Module CAN Access Only Modules
51. foo.jar bar.jarmyapp.jar
Module Non-Module
Automatic Unnamed
modulepath classpath
Automatic Module: Named Module
Unnamed Module: No Name Modu
MANIFEST.MF Automatic-Module-Name
or JAR filename
Automatic Module Name is Defined by
55. foo.jar bar.jarmyapp.jar
java.xml.bind
Module Non-Module
Automatic Module Can’
Describe Dependency
t
> java -cp bar.jar -p mods
--add-modules java.xml.bind
-m myapp/myapp.Main
> java -cp bar.jar -p mods
--add-modules ALL-MODULE-PATH
-m myapp/myapp.Main
If You Don’ t Know Dependencies
56. Conclusion
Non-Disclosure API
Issues of Non-Modular Software
Non-Standard Module
May Remove These APIs/Modules in Future
modulepath & classpath
Issues of Modular Software
May Use Only modulepath in Future??