From cdc68050c4c0a422783d594dee2cf06f179f56be Mon Sep 17 00:00:00 2001 From: Brian Stansberry Date: Tue, 9 Jul 2024 14:24:06 -0500 Subject: [PATCH] [WFCORE-6889] Add a ModuleDependency.Builder class --- .../deployment/module/ModuleDependency.java | 105 +++++++++++++++++- .../module/ModuleDependencyUnitTestCase.java | 54 +++++++++ 2 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 server/src/test/java/org/jboss/as/server/deployment/module/ModuleDependencyUnitTestCase.java diff --git a/server/src/main/java/org/jboss/as/server/deployment/module/ModuleDependency.java b/server/src/main/java/org/jboss/as/server/deployment/module/ModuleDependency.java index ca55e510ef4..58c08587c3f 100644 --- a/server/src/main/java/org/jboss/as/server/deployment/module/ModuleDependency.java +++ b/server/src/main/java/org/jboss/as/server/deployment/module/ModuleDependency.java @@ -21,6 +21,91 @@ */ public final class ModuleDependency implements Serializable { + public static final class Builder { + private final ModuleLoader moduleLoader; + @SuppressWarnings("deprecation") + private final ModuleIdentifier identifier; + private boolean export; + private boolean optional; + private boolean importServices; + private boolean userSpecified; + private String reason; + + public static Builder of(ModuleLoader moduleLoader, String moduleName) { + return new Builder(moduleLoader, moduleName); + } + + private Builder(ModuleLoader moduleLoader, String moduleName) { + this.moduleLoader = moduleLoader; + //noinspection deprecation + this.identifier = ModuleIdentifier.create(moduleName); + } + + /** + * Sets whether the dependent module should export this dependency's resources. + * + * @param export {@code true} if the dependencies resources should be exported + * @return this builder + */ + public Builder setExport(boolean export) { + this.export = export; + return this; + } + + /** + * Sets whether the dependent module should be able to import services from this dependency. + * + * @param importServices {@code true} if the dependent module should be able to load services from the dependency + * @return this builder + */ + public Builder setImportServices(boolean importServices) { + this.importServices = importServices; + return this; + } + + /** + * Sets whether this dependency is optional. + * + * @param optional {@code true} if the dependencys is optional + * @return this builder + */ + public Builder setOptional(boolean optional) { + this.optional = optional; + return this; + } + + /** + * Sets whether this dependency was explicitly specified by the user. + * + * @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added + * @return this builder + */ + public Builder setUserSpecified(boolean userSpecified) { + this.userSpecified = userSpecified; + return this; + } + + /** + * Sets an informational reason describing why this dependency was added. + * + * @param reason the reason. May be {@code null} + * @return this builder + */ + public Builder setReason(String reason) { + this.reason = reason; + return this; + } + + /** + * Build a {@code ModuleDependency} using this builder's settings. + * + * @return the {@code ModuleDependency}. Will not return {@code null} + */ + public ModuleDependency build() { + return new ModuleDependency(moduleLoader, identifier, optional, export, importServices, userSpecified, reason); + } + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -50,6 +135,9 @@ public String toString() { private final boolean userSpecified; private final String reason; + // NOTE: this constructor isn't deprecated because it's used in over 100 places, and perhaps 40+ more if + // the uses of the equivalent c'tor taking ModuleIdentifier make a simple switch to this. Changing all that + // code to use the builder just to clear a deprecation warning is a simple way to introduce bugs. /** * Construct a new instance. * @@ -57,7 +145,7 @@ public String toString() { * @param identifier the module identifier * @param optional {@code true} if this is an optional dependency * @param export {@code true} if resources should be exported by default - * @param importServices + * @param importServices {@code true} if the dependent module should be able to load services from the dependency * @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added */ public ModuleDependency(final ModuleLoader moduleLoader, final String identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified) { @@ -71,10 +159,13 @@ public ModuleDependency(final ModuleLoader moduleLoader, final String identifier * @param identifier the module identifier * @param optional {@code true} if this is an optional dependency * @param export {@code true} if resources should be exported by default - * @param importServices + * @param importServices {@code true} if the dependent module should be able to load services from the dependency * @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added * @param reason reason for adding implicit module dependency + * + * @deprecated Use a {@link Builder} */ + @Deprecated(forRemoval = true) public ModuleDependency(final ModuleLoader moduleLoader, final String identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified, String reason) { this(moduleLoader, ModuleIdentifier.create(identifier), optional, export, importServices, userSpecified, reason); } @@ -86,9 +177,12 @@ public ModuleDependency(final ModuleLoader moduleLoader, final String identifier * @param identifier the module identifier * @param optional {@code true} if this is an optional dependency * @param export {@code true} if resources should be exported by default - * @param importServices + * @param importServices {@code true} if the dependent module should be able to load services from the dependency * @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added + * + * @deprecated Use a {@link Builder} or @link ModuleDependency(ModuleLoader, String, boolean, boolean, boolean, boolean)} */ + @Deprecated(forRemoval = true) public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified) { this(moduleLoader, identifier, optional, export, importServices, userSpecified, null); } @@ -100,10 +194,13 @@ public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier * @param identifier the module identifier * @param optional {@code true} if this is an optional dependency * @param export {@code true} if resources should be exported by default - * @param importServices + * @param importServices {@code true} if the dependent module should be able to load services from the dependency * @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added * @param reason reason for adding implicit module dependency + * + * @deprecated Use a {@link Builder} */ + @Deprecated(forRemoval = true) public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified, String reason) { this.identifier = identifier; this.optional = optional; diff --git a/server/src/test/java/org/jboss/as/server/deployment/module/ModuleDependencyUnitTestCase.java b/server/src/test/java/org/jboss/as/server/deployment/module/ModuleDependencyUnitTestCase.java new file mode 100644 index 00000000000..ab2d9c61d53 --- /dev/null +++ b/server/src/test/java/org/jboss/as/server/deployment/module/ModuleDependencyUnitTestCase.java @@ -0,0 +1,54 @@ +/* + * Copyright The WildFly Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.jboss.as.server.deployment.module; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.jboss.modules.ModuleLoader; +import org.junit.Test; + +public class ModuleDependencyUnitTestCase { + + private static final String MODULE_NAME = "foo"; + private static final ModuleLoader TEST_LOADER = new ModuleLoader(ModuleLoader.NO_FINDERS); + + @Test + public void testBasicBuilder() { + ModuleDependency dep = ModuleDependency.Builder.of(TEST_LOADER, MODULE_NAME).build(); + assertEquals(TEST_LOADER, dep.getModuleLoader()); + assertEquals(MODULE_NAME, dep.getIdentifier().getName()); + assertFalse(dep.isExport()); + assertFalse(dep.isImportServices()); + assertFalse(dep.isOptional()); + assertFalse(dep.isUserSpecified()); + assertNotNull(dep.getReason()); + assertTrue(dep.getReason().isEmpty()); + } + + @Test + public void testSpecifiedBuilder() { + + ModuleDependency dep = ModuleDependency.Builder.of(TEST_LOADER, MODULE_NAME) + .setExport(true) + .setImportServices(true) + .setOptional(true) + .setUserSpecified(true) + .setReason(MODULE_NAME) + .build(); + + assertEquals(TEST_LOADER, dep.getModuleLoader()); + assertEquals(MODULE_NAME, dep.getIdentifier().getName()); + assertTrue(dep.isExport()); + assertTrue(dep.isImportServices()); + assertTrue(dep.isOptional()); + assertTrue(dep.isUserSpecified()); + assertFalse(dep.getReason().isEmpty()); + assertEquals(MODULE_NAME, dep.getReason().get()); + } +}