Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch to add useful methods to Layer.Controller
diff --git a/jdk/src/java.base/share/classes/java/lang/reflect/Layer.java b/jdk/src/java.base/share/classes/java/lang/reflect/Layer.java
index bd0036d..75cb609 100644
--- a/jdk/src/java.base/share/classes/java/lang/reflect/Layer.java
+++ b/jdk/src/java.base/share/classes/java/lang/reflect/Layer.java
@@ -219,6 +219,28 @@ public final class Layer {
/**
+ * Updates module {@code source} in the layer to contain a new package.
+ * This method is a no-op if {@code source} already contains package {@code pn}.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name to add
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer
+ */
+ public Controller addPackage(Module source, String pn) {
+ Objects.requireNonNull(source);
+ Objects.requireNonNull(pn);
+ ensureInLayer(source);
+ source.implAddPackage(pn, true);
+ return this;
+ }
+
+ /**
* Updates module {@code source} in the layer to read module
* {@code target}. This method is a no-op if {@code source} already
* reads {@code target}.
@@ -273,6 +295,156 @@ public final class Layer {
Modules.addOpens(source, pn, target);
return this;
}
+
+ /**
+ * Updates module {@code source} in the layer to open a package to
+ * all modules.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer or the package is not
+ * in the source module
+ *
+ * @see Module#addOpens
+ */
+ public Controller addOpensToAll(Module source, String pn) {
+ Objects.requireNonNull(source);
+ ensureInLayer(source);
+ Modules.addOpens(source, pn);
+ return this;
+ }
+
+ /**
+ * Updates module {@code source} in the layer to open a package to
+ * to all unnamed modules.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer or the package is not
+ * in the source module
+ *
+ * @see ModuleDescriptor.Builder#opens(String)
+ */
+ public Controller addOpensToAllUnnamed(Module source, String pn) {
+ Objects.requireNonNull(source);
+ ensureInLayer(source);
+ Modules.addOpensToAllUnnamed(source, pn);
+ return this;
+ }
+
+ /**
+ * Updates module {@code source} in the layer to export a package to
+ * module {@code target}. This method is a no-op if {@code source}
+ * already exports the package to at least {@code target}.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name
+ * @param target
+ * The target module to read
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer or the package is not
+ * in the source module
+ *
+ * @see Module#addExports
+ */
+ public Controller addExports(Module source, String pn, Module target) {
+ Objects.requireNonNull(source);
+ Objects.requireNonNull(target);
+ ensureInLayer(source);
+ Modules.addExports(source, pn, target);
+ return this;
+ }
+
+ /**
+ * Updates module {@code source} in the layer to export a package to
+ * module {@code target}. This method is a no-op if {@code source}
+ * already exports the package to at least {@code target}.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer or the package is not
+ * in the source module
+ *
+ * @see Module#addExports
+ */
+ public Controller addExportsToAll(Module source, String pn) {
+ Objects.requireNonNull(source);
+ ensureInLayer(source);
+ Modules.addExports(source, pn);
+ return this;
+ }
+
+ /**
+ * Updates module {@code source} in the layer to export a package to
+ * all unnamed modules.
+ *
+ * @param source
+ * The source module
+ * @param pn
+ * The package name
+ *
+ * @return This controller
+ *
+ * @throws IllegalArgumentException
+ * If {@code source} is not in the layer or the package is not
+ * in the source module
+ *
+ * @see Module#addExports
+ */
+ public Controller addExportsToAllUnnamed(Module source, String pn) {
+ Objects.requireNonNull(source);
+ ensureInLayer(source);
+ Modules.addExportsToAllUnnamed(source, pn);
+ return this;
+ }
+
+ /**
+ * Updates module {@code source} in the layer to add a
+ * service dependence on the given service type. This method is
+ * a no-op when invoked on an unnamed module or an automatic module.
+ *
+ * @param service
+ * The service type
+ *
+ * @return this module
+ *
+ * @throws IllegalStateException
+ * If this is a named module and the caller is not this module
+ *
+ * @see Module#addUses(Class)
+ * @see ModuleDescriptor#uses()
+ */
+ public Controller addUses(Module source, Class<?> service) {
+ Objects.requireNonNull(source);
+ Objects.requireNonNull(service);
+ ensureInLayer(source);
+ Modules.addUses(source, service);
+ return this;
+ }
+
}
diff --git a/jdk/src/java.base/share/classes/java/lang/reflect/Module.java b/jdk/src/java.base/share/classes/java/lang/reflect/Module.java
index 23fddfa..d395800 100644
--- a/jdk/src/java.base/share/classes/java/lang/reflect/Module.java
+++ b/jdk/src/java.base/share/classes/java/lang/reflect/Module.java
@@ -959,7 +959,7 @@ public final class Module implements AnnotatedElement {
* @throws IllegalArgumentException if the package name is not legal
* @throws IllegalStateException if the package is defined to another module
*/
- private void implAddPackage(String pn, boolean syncVM) {
+ void implAddPackage(String pn, boolean syncVM) {
// no-op if unnamed module
if (!isNamed())
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.