Layer Primitives (2/3): addUses()

David M. Lloyd david.lloyd at redhat.com
Wed May 10 20:48:08 UTC 2017


I'd like to re-propose adding the following method to the layer 
controller specification and implementation [1].

Justification:

• No new functionality is introduced to the specification or 
implementation by this method, other than to simplify access for layer 
controllers.
• Module#addUses() exists and does the exact same thing.
• It is not reasonable to require containers to generate bytecode within 
their contained modules in order to call this method.
• Since Module#addUses() exists, there is no scenario in which adding 
this primitive to the layer controller could possibly cause any form of 
liability or technical debt that does not already exist.
• The controller is only available to user-defined Layers, so there is 
no risk to the platform, jlink, etc.
• The patch itself is very small and simple, and should be very low-risk.

Drawbacks:

None that I am aware of.

Patch information:

The content and format of the JavaDoc are drawn from neighboring 
methods.  I relinquish any copyright claim to the patch in this mail. 
Please do not get bogged down by any formatting problems introduced by 
the mailing list; the purpose of directly including the patch is to give 
a clear, unambiguous subject for discussion.  I can provide a proper 
webrev (or whatever other form is requested) if needed.  This patch 
applies atop the previous (1/3) patch, but can trivially be applied 
without it as well.

[1]:

diff --git a/jdk/src/java.base/share/classes/java/lang/ModuleLayer.java 
b/jdk/src/java.base/share/classes/java/lang/ModuleLayer.java
index e224b4a..7f52b7c 100644
--- a/jdk/src/java.base/share/classes/java/lang/ModuleLayer.java
+++ b/jdk/src/java.base/share/classes/java/lang/ModuleLayer.java
@@ -303,6 +303,33 @@ public final class ModuleLayer {

              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(service);
+
+            if (source.isNamed() && ! 
source.getDescriptor().isAutomatic()) {
+                ensureInLayer(source);
+                source.implAddUses(service);
+            }
+
+            return this;
+        }
      }


-- 
- DML


More information about the jpms-spec-experts mailing list