Skip to content

Commit e734611

Browse files
authored
Refactor remove BuilderFactory and move newBuilder() methods to Builder interface (#80)
1 parent 0296ef1 commit e734611

File tree

7 files changed

+38
-55
lines changed

7 files changed

+38
-55
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Constants {
2525
static final String BUILDER = "io.avaje.inject.spi.Builder";
2626
static final String DEPENDENCYMETA = "io.avaje.inject.spi.DependencyMeta;";
2727
static final String BEANCONTEXTFACTORY = "io.avaje.inject.spi.BeanContextFactory;";
28-
static final String BUILDERFACTORY = "io.avaje.inject.spi.BuilderFactory;";
2928

3029
static boolean isBeanLifecycle(String type) {
3130
return BEAN_LIFECYCLE.equals(type);

inject-generator/src/main/java/io/avaje/inject/generator/ProcessingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Element asElement(TypeMirror returnType) {
137137
}
138138

139139
void buildNewBuilder(Append writer) {
140-
writer.append(" this.builder = BuilderFactory.newBuilder(\"%s\"", contextName);
140+
writer.append(" this.builder = Builder.newBuilder(\"%s\"", contextName);
141141
writer.append(", ");
142142
buildStringArray(writer, contextProvides, true);
143143
writer.append(", ");

inject-generator/src/main/java/io/avaje/inject/generator/SimpleFactoryWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ private Set<String> factoryImportTypes() {
135135
importTypes.add(Constants.DEPENDENCYMETA);
136136
importTypes.add(Constants.BEANCONTEXTFACTORY);
137137
importTypes.add(Constants.BUILDER);
138-
importTypes.add(Constants.BUILDERFACTORY);
139138
return importTypes;
140139
}
141140

inject/src/main/java/io/avaje/inject/BeanContextBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.avaje.inject.spi.BeanContextFactory;
44
import io.avaje.inject.spi.Builder;
5-
import io.avaje.inject.spi.BuilderFactory;
65
import io.avaje.inject.spi.EnrichBean;
76
import io.avaje.inject.spi.SuppliedBean;
87
import org.slf4j.Logger;
@@ -368,9 +367,9 @@ public BeanContext build() {
368367
" Refer to https://avaje.io/inject#gradle");
369368
}
370369
log.debug("building context with modules {}", moduleNames);
371-
Builder rootBuilder = BuilderFactory.newRootBuilder(suppliedBeans, enrichBeans);
370+
Builder rootBuilder = Builder.newRootBuilder(suppliedBeans, enrichBeans);
372371
for (BeanContextFactory factory : factoryOrder.factories()) {
373-
rootBuilder.addChild(factory.createContext(rootBuilder));
372+
rootBuilder.addChild(factory);
374373
}
375374

376375
BeanContext beanContext = rootBuilder.build();

inject/src/main/java/io/avaje/inject/spi/Builder.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@
1313
*/
1414
public interface Builder {
1515

16+
/**
17+
* Create the root level Builder.
18+
*
19+
* @param suppliedBeans The list of beans (typically test doubles) supplied when building the context.
20+
* @param enrichBeans The list of classes we want to have with mockito spy enhancement
21+
*/
22+
@SuppressWarnings("rawtypes")
23+
static Builder newRootBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
24+
if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
25+
// simple case, no mocks or spies
26+
return new DBuilder();
27+
}
28+
return new DBuilderExtn(suppliedBeans, enrichBeans);
29+
}
30+
31+
/**
32+
* Create a Builder for the named context (module).
33+
*
34+
* @param name the name of the module / bean context
35+
* @param provides the module features this module provides
36+
* @param dependsOn the names of modules this module is depends on.
37+
*/
38+
static Builder newBuilder(String name, String[] provides, String[] dependsOn) {
39+
return new DBuilder(name, provides, dependsOn);
40+
}
41+
1642
/**
1743
* Return the name of the (module) context this builder is creating.
1844
*/
@@ -89,7 +115,7 @@ public interface Builder {
89115
/**
90116
* Add a child context.
91117
*/
92-
void addChild(BeanContext context);
118+
void addChild(BeanContextFactory factory);
93119

94120
/**
95121
* Get an optional dependency.
@@ -126,11 +152,6 @@ public interface Builder {
126152
*/
127153
<T> BeanEntry<T> candidate(Class<T> cls, String name);
128154

129-
/**
130-
* Build and return the bean context.
131-
*/
132-
BeanContext build();
133-
134155
/**
135156
* Return a potentially enriched bean for registration into the context.
136157
* Typically for use with mockito spy.
@@ -140,4 +161,9 @@ public interface Builder {
140161
* @return Either the bean or the enriched bean to register into the context.
141162
*/
142163
<T> T enrich(T bean, Class<?>[] types);
164+
165+
/**
166+
* Build and return the bean context.
167+
*/
168+
BeanContext build();
143169
}

inject/src/main/java/io/avaje/inject/spi/BuilderFactory.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

inject/src/main/java/io/avaje/inject/spi/DBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ private <T> T getMaybe(Class<T> beanClass, String name) {
150150
}
151151

152152
@Override
153-
public void addChild(BeanContext child) {
154-
children.put(child.getName(), child);
153+
public void addChild(BeanContextFactory factory) {
154+
final BeanContext context = factory.createContext(this);
155+
children.put(context.getName(), context);
155156
}
156157

157158
/**

0 commit comments

Comments
 (0)