| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The main goal of this project is to explore basic features of modularity introduced in Java 9:
Reference: Java 9 Modularity
moduleA, moduleB, moduleC - exploring exports, exports X to Y, requires, requires transitive.
openModule, ordinaryModule, reflection - exporing open module and correlate modularity with reflection.
Assume that we analyze module-info.java under module X
module moduleA {
exports moduleA.export;
exports moduleA.exportOnlyToB to moduleB;
}
so package:
// InternalA internalA; // no access
module moduleB {
requires transitive moduleA;
exports moduleB.export;
}
<dependency>
<groupId>basic</groupId>
<artifactId>moduleA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
so (we don't repeat keyword defined in previous step):
module moduleC {
requires moduleB;
}
<dependency>
<groupId>basic</groupId>
<artifactId>moduleA</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>basic</groupId>
<artifactId>moduleB</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
so:
// ExportAOnlyToB exportAOnlyToB; // no access
project will not compile, so package ExportAOnlyToB was correctly exported only to moduleB (moduleB.TestB)
ExportA.export();
Module with only one simple class Invoker to invoker static methods:
public static void invokeStatic(Object obj, String methodName) throws IllegalAccessException {
try {
obj.getClass().getMethod(methodName).invoke(null);
} catch (InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
open module openModule {
requires reflection;
}
Running openModule.Test class will cause printing to console: "Hello from openModule!" - because of that the modul is marked as open in the module-info.java file.
module ordinaryModule {
requires reflection;
}
Running ordinaryModule.Test class will cause exception:
java.lang.IllegalAccessException: class Invoker (in module reflection) cannot access class Test (in module ordinaryModule) because module ordinaryModule does not export ordinaryModule to module reflection
Error occurred during initialization of boot layer java.lang.module.FindException: Error reading module: path\moduleB\target\classes Caused by: java.lang.module.InvalidModuleDescriptorException: TestB.class found in top-level directory (unnamed package not allowed in module)
| Back | FazBrowse Home | New Git URL |