Skip to content

Refactor/simplify to single builder - remove parent/child part #106

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 3 commits into from
May 15, 2021
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 @@ -16,16 +16,16 @@ class Constants {
static final String AT_GENERATED = "@Generated(\"io.avaje.inject.generator\")";
static final String META_INF_FACTORY = "META-INF/services/io.avaje.inject.spi.BeanContextFactory";

static final String BEANCONTEXT = "io.avaje.inject.BeanContext;";
static final String CONTEXTMODULE = "io.avaje.inject.ContextModule;";
static final String BEANCONTEXT = "io.avaje.inject.BeanContext";
static final String CONTEXTMODULE = "io.avaje.inject.ContextModule";

static final String GENERATED = "io.avaje.inject.spi.Generated";
static final String BEAN_FACTORY = "io.avaje.inject.spi.BeanFactory";
static final String BEAN_FACTORY2 = "io.avaje.inject.spi.BeanFactory2";
static final String BEAN_LIFECYCLE = "io.avaje.inject.spi.BeanLifecycle";
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 DEPENDENCYMETA = "io.avaje.inject.spi.DependencyMeta";
static final String BEANCONTEXTFACTORY = "io.avaje.inject.spi.BeanContextFactory";

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

void buildNewBuilder(Append writer) {
writer.append(" this.builder = Builder.newBuilder(\"%s\"", contextName);
writer.append(", ");
writer.append(" this.name = \"%s\";", contextName).eol();
writer.append(" this.provides = ", contextProvides);
buildStringArray(writer, contextProvides, true);
writer.append(", ");
writer.append(";").eol();
writer.append(" this.dependsOn = ", contextDependsOn);
buildStringArray(writer, contextDependsOn, true);
writer.append(");").eol();
writer.append(";").eol();
}

void buildAtContextModule(Append writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ private void writeBuildMethods() {
private void writeCreateMethod() {
writer.append(CODE_COMMENT_CREATE_CONTEXT).eol();
writer.append(" @Override").eol();
writer.append(" public BeanContext createContext(Builder parent) {").eol();
writer.append(" builder.setParent(parent);").eol();
writer.append(" public void build(Builder builder) {").eol();
writer.append(" this.builder = builder;").eol();
writer.append(" // create beans in order based on constructor dependencies").eol();
writer.append(" // i.e. \"provides\" followed by \"dependsOn\"").eol();
for (MetaData metaData : ordering.getOrdered()) {
writer.append(" build_%s();", metaData.getBuildName()).eol();
}
writer.append(" return builder.build();").eol();
writer.append(" }").eol();
writer.eol();
}
Expand Down Expand Up @@ -143,25 +142,28 @@ private void writeStartClass() {
context.buildAtContextModule(writer);

writer.append("public class %s implements BeanContextFactory {", factoryShortName).eol().eol();
writer.append(" private final Builder builder;").eol().eol();
writer.append(" private final String name;").eol();
writer.append(" private final String[] provides;").eol();
writer.append(" private final String[] dependsOn;").eol();
writer.append(" private Builder builder;").eol().eol();

writer.append(" public %s() {", factoryShortName).eol();
context.buildNewBuilder(writer);
writer.append(" }").eol().eol();

writer.append(" @Override").eol();
writer.append(" public String getName() {").eol();
writer.append(" return builder.getName();").eol();
writer.append(" return name;").eol();
writer.append(" }").eol().eol();

writer.append(" @Override").eol();
writer.append(" public String[] getProvides() {").eol();
writer.append(" return builder.getProvides();").eol();
writer.append(" return provides;").eol();
writer.append(" }").eol().eol();

writer.append(" @Override").eol();
writer.append(" public String[] getDependsOn() {").eol();
writer.append(" return builder.getDependsOn();").eol();
writer.append(" return dependsOn;").eol();
writer.append(" }").eol().eol();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public String[] getDependsOn() {
}

@Override
public BeanContext createContext(Builder parent) {
return null;
public void build(Builder parent) {

}
}
}
21 changes: 0 additions & 21 deletions inject/src/main/java/io/avaje/inject/BeanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,6 @@ static BeanContextBuilder newBuilder() {
return new DBeanContextBuilder();
}

/**
* Return the module name of the bean context.
*
* @see ContextModule
*/
String getName();

/**
* Return the names of module features this bean context provides.
*
* @see ContextModule
*/
String[] getProvides();

/**
* Return the names of modules this bean context depends on.
*
* @see ContextModule
*/
String[] getDependsOn();

/**
* Return a single bean given the type.
*
Expand Down
31 changes: 4 additions & 27 deletions inject/src/main/java/io/avaje/inject/DBeanContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

Expand Down Expand Up @@ -120,12 +112,12 @@ public BeanContext build() {
" Refer to https://avaje.io/inject#gradle");
}
log.debug("building context with modules {}", moduleNames);
Builder rootBuilder = Builder.newRootBuilder(suppliedBeans, enrichBeans);
Builder builder = Builder.newBuilder(suppliedBeans, enrichBeans);
for (BeanContextFactory factory : factoryOrder.factories()) {
rootBuilder.addChild(factory);
factory.build(builder);
}

BeanContext beanContext = rootBuilder.build();
BeanContext beanContext = builder.build();
// entire graph built, fire postConstruct
beanContext.start();
if (shutdownHook) {
Expand Down Expand Up @@ -180,21 +172,6 @@ private static class ShutdownAwareBeanContext implements BeanContext {
Runtime.getRuntime().addShutdownHook(hook);
}

@Override
public String getName() {
return context.getName();
}

@Override
public String[] getProvides() {
return context.getProvides();
}

@Override
public String[] getDependsOn() {
return context.getDependsOn();
}

@Override
public <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.avaje.inject.spi;

import io.avaje.inject.BeanContext;

/**
* This is the service loader interface defining the bean contexts that can be created.
*/
Expand All @@ -23,7 +21,7 @@ public interface BeanContextFactory {
String[] getDependsOn();

/**
* Create and return the BeanContext.
* Build all the beans.
*/
BeanContext createContext(Builder parent);
void build(Builder builder);
}
38 changes: 1 addition & 37 deletions inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,14 @@ public interface Builder {
* @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) {
static Builder newBuilder(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.
*/
String getName();

/**
* Return the names of module features that this module provides.
*/
String[] getProvides();

/**
* Return the names of modules that this module depends on.
*/
String[] getDependsOn();

/**
* Set a parent builder that can provide cross-module dependencies.
*/
void setParent(Builder parent);

/**
* Return true if the bean should be created and registered with the context.
* <p/>
Expand Down Expand Up @@ -108,11 +77,6 @@ static Builder newBuilder(String name, String[] provides, String[] dependsOn) {
*/
void addInjector(Consumer<Builder> injector);

/**
* Add a child context.
*/
void addChild(BeanContextFactory factory);

/**
* Get an optional dependency.
*/
Expand Down
55 changes: 3 additions & 52 deletions inject/src/main/java/io/avaje/inject/spi/DBeanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,21 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

class DBeanContext implements BeanContext {

private static final Logger log = LoggerFactory.getLogger(DBeanContext.class);

private final ReentrantLock lock = new ReentrantLock();

private final String name;

private final String[] provides;

private final String[] dependsOn;

private final List<BeanLifecycle> lifecycleList;

private final DBeanMap beans;

private final Map<String, BeanContext> children;

private boolean closed;

DBeanContext(String name, String[] provides, String[] dependsOn, List<BeanLifecycle> lifecycleList, DBeanMap beans, Map<String, BeanContext> children) {
this.name = name;
this.provides = provides;
this.dependsOn = dependsOn;
DBeanContext(List<BeanLifecycle> lifecycleList, DBeanMap beans) {
this.lifecycleList = lifecycleList;
this.beans = beans;
this.children = children;
}

@Override
public String getName() {
return name;
}

@Override
public String[] getProvides() {
return provides;
}

@Override
public String[] getDependsOn() {
return dependsOn;
}

@Override
Expand All @@ -67,9 +37,6 @@ public <T> BeanEntry<T> candidate(Class<T> type, String name) {
// sort candidates by priority - Primary, Normal, Secondary
EntrySort<T> entrySort = new EntrySort<>();
entrySort.add(beans.candidate(type, name));
for (BeanContext childContext : children.values()) {
entrySort.add(childContext.candidate(type, name));
}
return entrySort.get();
}

Expand All @@ -83,9 +50,6 @@ public <T> T getBean(Class<T> beanClass, String name) {
public <T> List<T> getBeans(Class<T> interfaceType) {
List<T> list = new ArrayList<>();
beans.addAll(interfaceType, list);
for (BeanContext childContext : children.values()) {
list.addAll(childContext.getBeans(interfaceType));
}
return list;
}

Expand Down Expand Up @@ -133,25 +97,17 @@ public <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
List<Object> list = new ArrayList<>();
beans.addAll(annotation, list);
for (BeanContext childContext : children.values()) {
list.addAll(childContext.getBeansWithAnnotation(annotation));
}
return list;
}

@Override
public void start() {
lock.lock();
try {
if (name != null) {
log.debug("firing postConstruct on beans in context:{}", name);
}
log.trace("firing postConstruct");
for (BeanLifecycle bean : lifecycleList) {
bean.postConstruct();
}
for (BeanContext childContext : children.values()) {
childContext.start();
}
} finally {
lock.unlock();
}
Expand All @@ -164,15 +120,10 @@ public void close() {
if (!closed) {
// we only allow one call to preDestroy
closed = true;
if (name != null) {
log.debug("firing preDestroy on beans in context:{}", name);
}
log.trace("firing preDestroy");
for (BeanLifecycle bean : lifecycleList) {
bean.preDestroy();
}
for (BeanContext childContext : children.values()) {
childContext.close();
}
}
} finally {
lock.unlock();
Expand Down
Loading