Class ModuleDescriptor.Builder
- Enclosing class:
- ModuleDescriptor
public static final class ModuleDescriptor.Builder extends Object
ModuleDescriptor
objects. ModuleDescriptor
defines the newModule
, newOpenModule
, and newAutomaticModule
methods to create builders for building normal, open, and automatic modules.
The set of packages in the module are accumulated by the
Builder
as the exports
, opens
, packages
, provides
, and mainClass
methods are invoked.
The module names, package names, and class names that are parameters specified to the builder methods are the module names, package names, and qualified names of classes (in named packages) as defined in the The Java Language Specification.
Example usage:
ModuleDescriptor descriptor = ModuleDescriptor.newModule("stats.core")
.requires("java.base")
.exports("org.acme.stats.core.clustering")
.exports("org.acme.stats.core.regression")
.packages(Set.of("org.acme.stats.core.internal"))
.build();
- API Note:
- A
Builder
checks the components and invariants as components are added to the builder. The rationale for this is to detect errors as early as possible and not defer all validation to thebuild
method. - Since:
- 9
Method Summary
Modifier and Type | Method | Description |
---|---|---|
ModuleDescriptor |
build() |
Builds and returns a ModuleDescriptor from its components. |
ModuleDescriptor.Builder |
exports |
Adds an exported package. |
ModuleDescriptor.Builder |
exports |
Adds an exported package. |
ModuleDescriptor.Builder |
exports |
Adds an exported package. |
ModuleDescriptor.Builder |
exports |
Adds an exported package with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
exports |
Adds an exported package with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
mainClass |
Sets the module main class. |
ModuleDescriptor.Builder |
opens |
Adds an open package. |
ModuleDescriptor.Builder |
opens |
Adds an open package. |
ModuleDescriptor.Builder |
opens |
Adds an open package. |
ModuleDescriptor.Builder |
opens |
Adds an open package with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
opens |
Adds an open package with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
packages |
Adds packages to the module. |
ModuleDescriptor.Builder |
provides |
Provides a service with one or more implementations. |
ModuleDescriptor.Builder |
provides |
Provides implementations of a service. |
ModuleDescriptor.Builder |
requires |
Adds a dependence on a module. |
ModuleDescriptor.Builder |
requires |
Adds a dependence on a module with an empty set of modifiers. |
ModuleDescriptor.Builder |
requires |
Adds a dependence on a module with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
requires |
Adds a dependence on a module with the given (and possibly empty) set of modifiers. |
ModuleDescriptor.Builder |
uses |
Adds a service dependence. |
ModuleDescriptor.Builder |
version |
Sets the module version. |
ModuleDescriptor.Builder |
version |
Sets the module version. |
Method Details
requires
public ModuleDescriptor.Builder requires(ModuleDescriptor.Requires req)
- Parameters:
-
req
- The dependence - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the dependence is on the module that this builder was initialized to build -
IllegalStateException
- If the dependence on the module has already been declared or this builder is for an automatic module
requires
public ModuleDescriptor.Builder requires(Set<ModuleDescriptor.Requires.Modifier> ms, String mn, ModuleDescriptor.Version compiledVersion)
- Parameters:
-
ms
- The set of modifiers -
mn
- The module name -
compiledVersion
- The version of the module recorded at compile-time - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the module name isnull
, is not a legal module name, or is equal to the module name that this builder was initialized to build -
IllegalStateException
- If the dependence on the module has already been declared or this builder is for an automatic module
requires
public ModuleDescriptor.Builder requires(Set<ModuleDescriptor.Requires.Modifier> ms, String mn)
- Parameters:
-
ms
- The set of modifiers -
mn
- The module name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the module name isnull
, is not a legal module name, or is equal to the module name that this builder was initialized to build -
IllegalStateException
- If the dependence on the module has already been declared or this builder is for an automatic module
requires
public ModuleDescriptor.Builder requires(String mn)
- Parameters:
-
mn
- The module name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the module name isnull
, is not a legal module name, or is equal to the module name that this builder was initialized to build -
IllegalStateException
- If the dependence on the module has already been declared or this builder is for an automatic module
exports
public ModuleDescriptor.Builder exports(ModuleDescriptor.Exports e)
- Parameters:
-
e
- The export - Returns:
- This builder
- Throws:
-
IllegalStateException
- If thepackage
is already declared as exported or this builder is for an automatic module
exports
public ModuleDescriptor.Builder exports(Set<ModuleDescriptor.Exports.Modifier> ms, String pn, Set<String> targets)
- Parameters:
-
ms
- The set of modifiers -
pn
- The package name -
targets
- The set of target modules names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name, the set of target modules is empty, or the set of target modules contains a name that is not a legal module name -
IllegalStateException
- If the package is already declared as exported or this builder is for an automatic module
exports
public ModuleDescriptor.Builder exports(Set<ModuleDescriptor.Exports.Modifier> ms, String pn)
- Parameters:
-
ms
- The set of modifiers -
pn
- The package name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name -
IllegalStateException
- If the package is already declared as exported or this builder is for an automatic module
exports
public ModuleDescriptor.Builder exports(String pn, Set<String> targets)
- Parameters:
-
pn
- The package name -
targets
- The set of target modules names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name, the set of target modules is empty, or the set of target modules contains a name that is not a legal module name -
IllegalStateException
- If the package is already declared as exported or this builder is for an automatic module
exports
public ModuleDescriptor.Builder exports(String pn)
- Parameters:
-
pn
- The package name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name -
IllegalStateException
- If the package is already declared as exported or this builder is for an automatic module
opens
public ModuleDescriptor.Builder opens(ModuleDescriptor.Opens obj)
- Parameters:
-
obj
- TheOpens
object - Returns:
- This builder
- Throws:
-
IllegalStateException
- If the package is already declared as open, or this is a builder for an open module or automatic module
opens
public ModuleDescriptor.Builder opens(Set<ModuleDescriptor.Opens.Modifier> ms, String pn, Set<String> targets)
- Parameters:
-
ms
- The set of modifiers -
pn
- The package name -
targets
- The set of target modules names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name, the set of target modules is empty, or the set of target modules contains a name that is not a legal module name -
IllegalStateException
- If the package is already declared as open, or this is a builder for an open module or automatic module
opens
public ModuleDescriptor.Builder opens(Set<ModuleDescriptor.Opens.Modifier> ms, String pn)
- Parameters:
-
ms
- The set of modifiers -
pn
- The package name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name -
IllegalStateException
- If the package is already declared as open, or this is a builder for an open module or automatic module
opens
public ModuleDescriptor.Builder opens(String pn, Set<String> targets)
- Parameters:
-
pn
- The package name -
targets
- The set of target modules names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name, the set of target modules is empty, or the set of target modules contains a name that is not a legal module name -
IllegalStateException
- If the package is already declared as open, or this is a builder for an open module or automatic module
opens
public ModuleDescriptor.Builder opens(String pn)
- Parameters:
-
pn
- The package name - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the package name isnull
or is not a legal package name -
IllegalStateException
- If the package is already declared as open, or this is a builder for an open module or automatic module
uses
public ModuleDescriptor.Builder uses(String service)
- Parameters:
-
service
- The service type - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the service type isnull
or not a qualified name of a class in a named package -
IllegalStateException
- If a dependency on the service type has already been declared or this is a builder for an automatic module
provides
public ModuleDescriptor.Builder provides(ModuleDescriptor.Provides p)
provider
(or provider factory) is added to the module if not already added.- Parameters:
-
p
- The provides - Returns:
- This builder
- Throws:
-
IllegalStateException
- If the providers for the service type have already been declared
provides
public ModuleDescriptor.Builder provides(String service, List<String> providers)
- Parameters:
-
service
- The service type -
providers
- The list of provider or provider factory class names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If the service type or any of the provider class names isnull
or not a qualified name of a class in a named package, or the list of provider class names is empty -
IllegalStateException
- If the providers for the service type have already been declared
packages
public ModuleDescriptor.Builder packages(Set<String> pns)
- Parameters:
-
pns
- The (possibly empty) set of package names - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- If any of the package names isnull
or is not a legal package name
version
public ModuleDescriptor.Builder version(ModuleDescriptor.Version v)
- Parameters:
-
v
- The version - Returns:
- This builder
version
public ModuleDescriptor.Builder version(String vs)
- Parameters:
-
vs
- The version string to parse - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- Ifvs
isnull
or cannot be parsed as a version string - See Also:
mainClass
public ModuleDescriptor.Builder mainClass(String mc)
packages
method to add the package name of the main class.- Parameters:
-
mc
- The module main class - Returns:
- This builder
- Throws:
-
IllegalArgumentException
- IfmainClass
isnull
or not a qualified name of a class in a named package
build
public ModuleDescriptor build()
ModuleDescriptor
from its components. The module will require "java.base
" even if the dependence has not been declared (the exception is when building a module named "java.base
" as it cannot require itself). The dependence on "java.base
" will have the MANDATED
modifier if the dependence was not declared.
- Returns:
- The module descriptor
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/module/ModuleDescriptor.Builder.html