Skip to content

Refactor remove BuilderFactory and move newBuilder() methods to Builder interface #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Constants {
static final String BUILDER = "io.avaje.inject.spi.Builder";
static final String DEPENDENCYMETA = "io.avaje.inject.spi.DependencyMeta;";
static final String BEANCONTEXTFACTORY = "io.avaje.inject.spi.BeanContextFactory;";
static final String BUILDERFACTORY = "io.avaje.inject.spi.BuilderFactory;";

static boolean isBeanLifecycle(String type) {
return BEAN_LIFECYCLE.equals(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Element asElement(TypeMirror returnType) {
}

void buildNewBuilder(Append writer) {
writer.append(" this.builder = BuilderFactory.newBuilder(\"%s\"", contextName);
writer.append(" this.builder = Builder.newBuilder(\"%s\"", contextName);
writer.append(", ");
buildStringArray(writer, contextProvides, true);
writer.append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ private Set<String> factoryImportTypes() {
importTypes.add(Constants.DEPENDENCYMETA);
importTypes.add(Constants.BEANCONTEXTFACTORY);
importTypes.add(Constants.BUILDER);
importTypes.add(Constants.BUILDERFACTORY);
return importTypes;
}

Expand Down
5 changes: 2 additions & 3 deletions inject/src/main/java/io/avaje/inject/BeanContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.avaje.inject.spi.BeanContextFactory;
import io.avaje.inject.spi.Builder;
import io.avaje.inject.spi.BuilderFactory;
import io.avaje.inject.spi.EnrichBean;
import io.avaje.inject.spi.SuppliedBean;
import org.slf4j.Logger;
Expand Down Expand Up @@ -368,9 +367,9 @@ public BeanContext build() {
" Refer to https://avaje.io/inject#gradle");
}
log.debug("building context with modules {}", moduleNames);
Builder rootBuilder = BuilderFactory.newRootBuilder(suppliedBeans, enrichBeans);
Builder rootBuilder = Builder.newRootBuilder(suppliedBeans, enrichBeans);
for (BeanContextFactory factory : factoryOrder.factories()) {
rootBuilder.addChild(factory.createContext(rootBuilder));
rootBuilder.addChild(factory);
}

BeanContext beanContext = rootBuilder.build();
Expand Down
38 changes: 32 additions & 6 deletions inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
*/
public interface Builder {

/**
* Create the root level Builder.
*
* @param suppliedBeans The list of beans (typically test doubles) supplied when building the context.
* @param enrichBeans The list of classes we want to have with mockito spy enhancement
*/
@SuppressWarnings("rawtypes")
static Builder newRootBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
// simple case, no mocks or spies
return new DBuilder();
}
return new DBuilderExtn(suppliedBeans, enrichBeans);
}

/**
* Create a Builder for the named context (module).
*
* @param name the name of the module / bean context
* @param provides the module features this module provides
* @param dependsOn the names of modules this module is depends on.
*/
static Builder newBuilder(String name, String[] provides, String[] dependsOn) {
return new DBuilder(name, provides, dependsOn);
}

/**
* Return the name of the (module) context this builder is creating.
*/
Expand Down Expand Up @@ -89,7 +115,7 @@ public interface Builder {
/**
* Add a child context.
*/
void addChild(BeanContext context);
void addChild(BeanContextFactory factory);

/**
* Get an optional dependency.
Expand Down Expand Up @@ -126,11 +152,6 @@ public interface Builder {
*/
<T> BeanEntry<T> candidate(Class<T> cls, String name);

/**
* Build and return the bean context.
*/
BeanContext build();

/**
* Return a potentially enriched bean for registration into the context.
* Typically for use with mockito spy.
Expand All @@ -140,4 +161,9 @@ public interface Builder {
* @return Either the bean or the enriched bean to register into the context.
*/
<T> T enrich(T bean, Class<?>[] types);

/**
* Build and return the bean context.
*/
BeanContext build();
}
41 changes: 0 additions & 41 deletions inject/src/main/java/io/avaje/inject/spi/BuilderFactory.java

This file was deleted.

5 changes: 3 additions & 2 deletions inject/src/main/java/io/avaje/inject/spi/DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ private <T> T getMaybe(Class<T> beanClass, String name) {
}

@Override
public void addChild(BeanContext child) {
children.put(child.getName(), child);
public void addChild(BeanContextFactory factory) {
final BeanContext context = factory.createContext(this);
children.put(context.getName(), context);
}

/**
Expand Down