Skip to content

Commit 046ec43

Browse files
committed
Simplify Builder removing parent/child aspect
1 parent c968368 commit 046ec43

File tree

10 files changed

+33
-93
lines changed

10 files changed

+33
-93
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,13 @@ Element asElement(TypeMirror returnType) {
142142
}
143143

144144
void buildNewBuilder(Append writer) {
145-
writer.append(" this.builder = Builder.newBuilder(\"%s\"", contextName);
146-
writer.append(", ");
145+
writer.append(" this.name = \"%s\";", contextName).eol();
146+
writer.append(" this.provides = ", contextProvides);
147147
buildStringArray(writer, contextProvides, true);
148-
writer.append(", ");
148+
writer.append(";").eol();
149+
writer.append(" this.dependsOn = ", contextDependsOn);
149150
buildStringArray(writer, contextDependsOn, true);
150-
writer.append(");").eol();
151+
writer.append(";").eol().eol();
151152
}
152153

153154
void buildAtContextModule(Append writer) {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,13 @@ private void writeBuildMethods() {
102102
private void writeCreateMethod() {
103103
writer.append(CODE_COMMENT_CREATE_CONTEXT).eol();
104104
writer.append(" @Override").eol();
105-
writer.append(" public BeanContext createContext(Builder parent) {").eol();
106-
writer.append(" builder.setParent(parent);").eol();
105+
writer.append(" public void createContext(Builder builder) {").eol();
106+
writer.append(" this.builder = builder;").eol();
107107
writer.append(" // create beans in order based on constructor dependencies").eol();
108108
writer.append(" // i.e. \"provides\" followed by \"dependsOn\"").eol();
109109
for (MetaData metaData : ordering.getOrdered()) {
110110
writer.append(" build_%s();", metaData.getBuildName()).eol();
111111
}
112-
writer.append(" return builder.build();").eol();
113112
writer.append(" }").eol();
114113
writer.eol();
115114
}
@@ -143,25 +142,28 @@ private void writeStartClass() {
143142
context.buildAtContextModule(writer);
144143

145144
writer.append("public class %s implements BeanContextFactory {", factoryShortName).eol().eol();
146-
writer.append(" private final Builder builder;").eol().eol();
145+
writer.append(" private final String name;").eol();
146+
writer.append(" private final String[] provides;").eol();
147+
writer.append(" private final String[] dependsOn;").eol();
148+
writer.append(" private Builder builder;").eol().eol();
147149

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

152154
writer.append(" @Override").eol();
153155
writer.append(" public String getName() {").eol();
154-
writer.append(" return builder.getName();").eol();
156+
writer.append(" return name;").eol();
155157
writer.append(" }").eol().eol();
156158

157159
writer.append(" @Override").eol();
158160
writer.append(" public String[] getProvides() {").eol();
159-
writer.append(" return builder.getProvides();").eol();
161+
writer.append(" return provides;").eol();
160162
writer.append(" }").eol().eol();
161163

162164
writer.append(" @Override").eol();
163165
writer.append(" public String[] getDependsOn() {").eol();
164-
writer.append(" return builder.getDependsOn();").eol();
166+
writer.append(" return dependsOn;").eol();
165167
writer.append(" }").eol().eol();
166168
}
167169

inject-test/src/test/java/io/avaje/inject/BeanContextBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public String[] getDependsOn() {
128128
}
129129

130130
@Override
131-
public BeanContext createContext(Builder parent) {
132-
return null;
131+
public void createContext(Builder parent) {
132+
133133
}
134134
}
135135
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public BeanContext build() {
122122
log.debug("building context with modules {}", moduleNames);
123123
Builder rootBuilder = Builder.newRootBuilder(suppliedBeans, enrichBeans);
124124
for (BeanContextFactory factory : factoryOrder.factories()) {
125-
rootBuilder.addChild(factory);
125+
factory.createContext(rootBuilder);
126126
}
127127

128128
BeanContext beanContext = rootBuilder.build();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public interface BeanContextFactory {
2525
/**
2626
* Create and return the BeanContext.
2727
*/
28-
BeanContext createContext(Builder parent);
28+
void createContext(Builder parent);
2929
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ static Builder newRootBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean>
2929
return new DBuilderExtn(suppliedBeans, enrichBeans);
3030
}
3131

32-
/**
33-
* Create a Builder for the named context (module).
34-
*
35-
* @param name the name of the module / bean context
36-
* @param provides the module features this module provides
37-
* @param dependsOn the names of modules this module is depends on.
38-
*/
39-
static Builder newBuilder(String name, String[] provides, String[] dependsOn) {
40-
return new DBuilder(name, provides, dependsOn);
41-
}
42-
4332
/**
4433
* Return the name of the (module) context this builder is creating.
4534
*/

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.ArrayList;
1111
import java.util.Collections;
1212
import java.util.List;
13-
import java.util.Map;
1413
import java.util.concurrent.locks.ReentrantLock;
1514

1615
class DBeanContext implements BeanContext {
@@ -29,17 +28,14 @@ class DBeanContext implements BeanContext {
2928

3029
private final DBeanMap beans;
3130

32-
private final Map<String, BeanContext> children;
33-
3431
private boolean closed;
3532

36-
DBeanContext(String name, String[] provides, String[] dependsOn, List<BeanLifecycle> lifecycleList, DBeanMap beans, Map<String, BeanContext> children) {
33+
DBeanContext(String name, String[] provides, String[] dependsOn, List<BeanLifecycle> lifecycleList, DBeanMap beans) {//}, Map<String, BeanContext> children) {
3734
this.name = name;
3835
this.provides = provides;
3936
this.dependsOn = dependsOn;
4037
this.lifecycleList = lifecycleList;
4138
this.beans = beans;
42-
this.children = children;
4339
}
4440

4541
@Override
@@ -67,9 +63,6 @@ public <T> BeanEntry<T> candidate(Class<T> type, String name) {
6763
// sort candidates by priority - Primary, Normal, Secondary
6864
EntrySort<T> entrySort = new EntrySort<>();
6965
entrySort.add(beans.candidate(type, name));
70-
for (BeanContext childContext : children.values()) {
71-
entrySort.add(childContext.candidate(type, name));
72-
}
7366
return entrySort.get();
7467
}
7568

@@ -83,9 +76,6 @@ public <T> T getBean(Class<T> beanClass, String name) {
8376
public <T> List<T> getBeans(Class<T> interfaceType) {
8477
List<T> list = new ArrayList<>();
8578
beans.addAll(interfaceType, list);
86-
for (BeanContext childContext : children.values()) {
87-
list.addAll(childContext.getBeans(interfaceType));
88-
}
8979
return list;
9080
}
9181

@@ -133,9 +123,6 @@ public <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation
133123
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
134124
List<Object> list = new ArrayList<>();
135125
beans.addAll(annotation, list);
136-
for (BeanContext childContext : children.values()) {
137-
list.addAll(childContext.getBeansWithAnnotation(annotation));
138-
}
139126
return list;
140127
}
141128

@@ -149,9 +136,6 @@ public void start() {
149136
for (BeanLifecycle bean : lifecycleList) {
150137
bean.postConstruct();
151138
}
152-
for (BeanContext childContext : children.values()) {
153-
childContext.start();
154-
}
155139
} finally {
156140
lock.unlock();
157141
}
@@ -170,9 +154,6 @@ public void close() {
170154
for (BeanLifecycle bean : lifecycleList) {
171155
bean.preDestroy();
172156
}
173-
for (BeanContext childContext : children.values()) {
174-
childContext.close();
175-
}
176157
}
177158
} finally {
178159
lock.unlock();

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

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class DBuilder implements Builder {
4949
*/
5050
private final String[] dependsOn;
5151

52-
private final Map<String, BeanContext> children = new LinkedHashMap<>();
53-
5452
/**
5553
* Debug of the current bean being wired - used in injection errors.
5654
*/
@@ -61,17 +59,6 @@ class DBuilder implements Builder {
6159
*/
6260
private boolean runningPostConstruct;
6361

64-
Builder parent;
65-
66-
/**
67-
* Create a named context for non-root builders.
68-
*/
69-
DBuilder(String name, String[] provides, String[] dependsOn) {
70-
this.name = name;
71-
this.provides = provides;
72-
this.dependsOn = dependsOn;
73-
}
74-
7562
/**
7663
* Create for the root builder.
7764
*/
@@ -98,22 +85,23 @@ public String[] getDependsOn() {
9885

9986
@Override
10087
public void setParent(Builder parent) {
101-
this.parent = parent;
88+
//this.parent = parent;
10289
}
10390

10491
@Override
10592
public boolean isAddBeanFor(Class<?>... types) {
10693
return isAddBeanFor(null, types);
10794
}
10895

96+
protected void next(String name, Class<?>... types) {
97+
injectTarget = firstOf(types);
98+
beanMap.nextBean(name, types);
99+
}
100+
109101
@Override
110102
public boolean isAddBeanFor(String name, Class<?>... types) {
111-
beanMap.nextBean(name, types);
112-
if (parent == null) {
113-
return true;
114-
}
115-
this.injectTarget = firstOf(types);
116-
return parent.isAddBeanFor(name, types);
103+
next(name, types);
104+
return true;
117105
}
118106

119107
private Class<?> firstOf(Class<?>[] types) {
@@ -130,26 +118,13 @@ public <T> Set<T> getSet(Class<T> interfaceType) {
130118
public <T> List<T> getList(Class<T> interfaceType) {
131119
List list = new ArrayList<>();
132120
beanMap.addAll(interfaceType, list);
133-
for (BeanContext childContext : children.values()) {
134-
list.addAll(childContext.getBeansWithAnnotation(interfaceType));
135-
}
136-
if (parent != null) {
137-
list.addAll(parent.getList(interfaceType));
138-
}
139121
return (List<T>) list;
140122
}
141123

142124
@Override
143125
public <T> BeanEntry<T> candidate(Class<T> cls, String name) {
144126
DBeanContext.EntrySort<T> entrySort = new DBeanContext.EntrySort<>();
145127
entrySort.add(beanMap.candidate(cls, name));
146-
for (BeanContext childContext : children.values()) {
147-
entrySort.add(childContext.candidate(cls, name));
148-
}
149-
if (parent != null) {
150-
// look in parent context (cross-module dependency)
151-
entrySort.add(parent.candidate(cls, name));
152-
}
153128
return entrySort.get();
154129
}
155130

@@ -160,8 +135,7 @@ private <T> T getMaybe(Class<T> beanClass, String name) {
160135

161136
@Override
162137
public void addChild(BeanContextFactory factory) {
163-
final BeanContext context = factory.createContext(this);
164-
children.put(context.getName(), context);
138+
factory.createContext(this);
165139
}
166140

167141
/**
@@ -189,10 +163,7 @@ public <T> T registerSecondary(T bean) {
189163
}
190164

191165
private <T> T register(int flag, T bean) {
192-
if (parent != null) {
193-
// enrichment only exist on top level builder
194-
bean = parent.enrich(bean, beanMap.types());
195-
}
166+
bean = enrich(bean, beanMap.types());
196167
beanMap.register(flag, bean);
197168
return bean;
198169
}
@@ -278,6 +249,6 @@ private void runInjectors() {
278249

279250
public BeanContext build() {
280251
runInjectors();
281-
return new DBeanContext(name, provides, dependsOn, lifecycleList, beanMap, children);
252+
return new DBeanContext(name, provides, dependsOn, lifecycleList, beanMap);
282253
}
283254
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ class DBuilderExtn extends DBuilder {
2828
}
2929
}
3030

31-
@Override
32-
public boolean isAddBeanFor(Class<?>... types) {
33-
return isAddBeanFor(null, types);
34-
}
35-
3631
@Override
3732
public boolean isAddBeanFor(String qualifierName, Class<?>... types) {
33+
next(qualifierName, types);
3834
if (hasSuppliedBeans) {
3935
return !beanMap.isSupplied(qualifierName, types);
4036
}

inject/src/test/java/io/avaje/inject/BeanContextBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public String[] getDependsOn() {
128128
}
129129

130130
@Override
131-
public BeanContext createContext(Builder parent) {
132-
return null;
131+
public void createContext(Builder parent) {
132+
133133
}
134134
}
135135
}

0 commit comments

Comments
 (0)