Repository: avaje/avaje-inject
Pull request #381 (2023-07-23 00:55:30)

Use Var-Args for profiles
Also log the current profiles


Patch:
diff --git a/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java b/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java
index 5cb16022..3ffda98c 100644
--- a/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java
+++ b/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java
@@ -193,7 +193,7 @@ public interface BeanScopeBuilder {
    *
    * <p>If profiles are not set explicitly here they are read from the properties plugin.
    */
-  BeanScopeBuilder profiles(String profiles);
+  BeanScopeBuilder profiles(String... profiles);
 
   /**
    * Add a supplied bean provider that acts as a default fallback for a dependency.
diff --git a/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java b/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
index 52d74d55..75df562b 100644
--- a/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
+++ b/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
@@ -14,6 +14,8 @@ import java.util.function.Supplier;
 
 import static java.lang.System.Logger.Level.DEBUG;
 import static java.lang.System.Logger.Level.INFO;
+import static java.util.Collections.emptySet;
+import static java.util.stream.Collectors.toSet;
 
 /**
  * Build a bean scope with options for shutdown hook and supplying test doubles.
@@ -35,7 +37,7 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
   private boolean shutdownHook;
   private ClassLoader classLoader;
   private PropertyRequiresPlugin propertyRequiresPlugin;
-  private String profiles;
+  private Set<String> profiles;
 
   /**
    * Create a BeanScopeBuilder to ultimately load and return a new BeanScope.
@@ -105,8 +107,8 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
   }
 
   @Override
-  public BeanScopeBuilder profiles(String profiles) {
-    this.profiles = profiles;
+  public BeanScopeBuilder profiles(String... profiles) {
+    this.profiles = Set.of(profiles);
     return this;
   }
 
@@ -220,6 +222,19 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
     }
   }
 
+  private void initProfiles() {
+    if (profiles == null) {
+      profiles =
+          propertyRequiresPlugin.get("avaje.profiles").map(DBeanScopeBuilder::toProfiles).orElse(emptySet());
+    }
+  }
+
+  private static Set<String> toProfiles(String profiles) {
+
+    return Arrays.stream(profiles.split(",")).collect(toSet());
+  }
+
+
   @Override
   public BeanScope build() {
     final var start = System.currentTimeMillis();
@@ -248,6 +263,10 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting {
     final var level = propertyRequiresPlugin.contains("printModules") ? INFO : DEBUG;
     log.log(level, "building with modules {0}", moduleNames);
 
+    initProfiles();
+
+    log.log(INFO, "building with profiles {0}", profiles);
+
     final Builder builder = Builder.newBuilder(profiles, propertyRequiresPlugin, suppliedBeans, enrichBeans, parent, parentOverride);
     for (final Module factory : factoryOrder.factories()) {
       factory.build(builder);
diff --git a/inject/src/main/java/io/avaje/inject/spi/Builder.java b/inject/src/main/java/io/avaje/inject/spi/Builder.java
index b58e6068..45d5feee 100644
--- a/inject/src/main/java/io/avaje/inject/spi/Builder.java
+++ b/inject/src/main/java/io/avaje/inject/spi/Builder.java
@@ -1,7 +1,6 @@
 package io.avaje.inject.spi;
 
 import io.avaje.inject.BeanScope;
-import io.avaje.lang.Nullable;
 import jakarta.inject.Provider;
 
 import java.lang.reflect.Type;
@@ -26,7 +25,7 @@ public interface Builder {
    * @param parentOverride When false do not add beans that already exist on the parent
    */
   @SuppressWarnings("rawtypes")
-  static Builder newBuilder(@Nullable String profiles, PropertyRequiresPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
+  static Builder newBuilder(Set<String> profiles, PropertyRequiresPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
     if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
       // simple case, no mocks or spies
       return new DBuilder(profiles, plugin, parent, parentOverride);
diff --git a/inject/src/main/java/io/avaje/inject/spi/DBuilder.java b/inject/src/main/java/io/avaje/inject/spi/DBuilder.java
index 15176931..080ea756 100644
--- a/inject/src/main/java/io/avaje/inject/spi/DBuilder.java
+++ b/inject/src/main/java/io/avaje/inject/spi/DBuilder.java
@@ -2,7 +2,6 @@ package io.avaje.inject.spi;
 
 import io.avaje.inject.BeanEntry;
 import io.avaje.inject.BeanScope;
-import io.avaje.lang.Nullable;
 import jakarta.inject.Provider;
 
 import java.lang.reflect.Type;
@@ -10,8 +9,6 @@ import java.util.*;
 import java.util.function.Consumer;
 
 import static io.avaje.inject.spi.DBeanScope.combine;
-import static java.util.Collections.emptySet;
-import static java.util.stream.Collectors.toSet;
 
 class DBuilder implements Builder {
 
@@ -38,24 +35,11 @@ class DBuilder implements Builder {
 
   private DBeanScopeProxy beanScopeProxy;
 
-  DBuilder(@Nullable String profiles, PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) {
+  DBuilder(Set<String> profiles, PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) {
     this.propertyRequires = propertyRequires;
     this.parent = parent;
     this.parentOverride = parentOverride;
-    this.profiles = initProfiles(profiles, propertyRequires);
-  }
-
-  private Set<String> initProfiles(@Nullable String profiles, PropertyRequiresPlugin properties) {
-    if (profiles != null) {
-      return toProfiles(profiles);
-    }
-    return properties.get("avaje.profiles")
-      .map(this::toProfiles)
-      .orElse(emptySet());
-  }
-
-  private Set<String> toProfiles(String profiles) {
-    return Arrays.stream(profiles.split(",")).collect(toSet());
+    this.profiles = profiles;
   }
 
   @Override
diff --git a/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java b/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java
index 78e8cb53..01edd337 100644
--- a/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java
+++ b/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java
@@ -2,12 +2,12 @@ package io.avaje.inject.spi;
 
 import io.avaje.inject.BeanEntry;
 import io.avaje.inject.BeanScope;
-import io.avaje.lang.Nullable;
 
 import java.lang.reflect.Type;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Extended builder that supports supplied beans (mocks) and enriching beans (spy).
@@ -20,14 +20,14 @@ final class DBuilderExtn extends DBuilder {
   private final boolean hasSuppliedBeans;
 
   @SuppressWarnings("rawtypes")
-  DBuilderExtn(@Nullable String profiles, PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
+  DBuilderExtn(Set<String> profiles, PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
     super(profiles, plugin, parent, parentOverride);
     this.hasSuppliedBeans = (suppliedBeans != null && !suppliedBeans.isEmpty());
     if (hasSuppliedBeans) {
       beanMap.add(suppliedBeans);
     }
     if (enrichBeans != null && !enrichBeans.isEmpty()) {
-      for (EnrichBean spy : enrichBeans) {
+      for (final EnrichBean spy : enrichBeans) {
         enrichMap.put(spy.key(), spy);
       }
     }
@@ -51,7 +51,7 @@ final class DBuilderExtn extends DBuilder {
    */
   private void enrichParentMatch() {
     if (parentMatch != null && !enrichMap.isEmpty()) {
-      Object enrichedBean = enrich(parentMatch, beanMap.next());
+      final Object enrichedBean = enrich(parentMatch, beanMap.next());
       if (enrichedBean != parentMatch) {
         beanMap.nextPriority(BeanEntry.SUPPLIED);
         beanMap.register(enrichedBean);
@@ -68,7 +68,7 @@ final class DBuilderExtn extends DBuilder {
     if (enrich != null) {
       return enrich.enrich(bean);
     }
-    for (Type type : next.types) {
+    for (final Type type : next.types) {
       enrich = enrichLookup(type, next.name);
       if (enrich != null) {
         return enrich.enrich(bean);


Test patch:
diff --git a/inject-test/src/main/java/io/avaje/inject/test/MetaInfo.java b/inject-test/src/main/java/io/avaje/inject/test/MetaInfo.java
index d7995801..02d2ae63 100644
--- a/inject-test/src/main/java/io/avaje/inject/test/MetaInfo.java
+++ b/inject-test/src/main/java/io/avaje/inject/test/MetaInfo.java
@@ -44,17 +44,13 @@ final class MetaInfo {
       builder.parent(parent, false);
     }
 
-    final var profiles =
-        Optional.ofNullable(testInstance)
-            .map(Object::getClass)
-            .map(c -> c.getAnnotation(InjectTest.class))
-            .map(InjectTest::profiles)
-            .map(p -> String.join(",", p))
-            .orElse("");
-
-    if (!profiles.isBlank()) {
-      builder.profiles(profiles);
-    }
+    //set wiring profile
+    Optional.ofNullable(testInstance)
+        .map(Object::getClass)
+        .map(c -> c.getAnnotation(InjectTest.class))
+        .map(InjectTest::profiles)
+        .ifPresent(builder::profiles);
+
     // register mocks and spies local to this test
     reader.build(builder, testInstance);