Skip to content

Commit 4ffc10a

Browse files
committed
Simplify Builder part 2 - removing unnecessary methods etc
1 parent 046ec43 commit 4ffc10a

File tree

10 files changed

+22
-185
lines changed

10 files changed

+22
-185
lines changed

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
@@ -148,7 +148,7 @@ void buildNewBuilder(Append writer) {
148148
writer.append(";").eol();
149149
writer.append(" this.dependsOn = ", contextDependsOn);
150150
buildStringArray(writer, contextDependsOn, true);
151-
writer.append(";").eol().eol();
151+
writer.append(";").eol();
152152
}
153153

154154
void buildAtContextModule(Append writer) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void writeBuildMethods() {
102102
private void writeCreateMethod() {
103103
writer.append(CODE_COMMENT_CREATE_CONTEXT).eol();
104104
writer.append(" @Override").eol();
105-
writer.append(" public void createContext(Builder builder) {").eol();
105+
writer.append(" public void build(Builder builder) {").eol();
106106
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();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public String[] getDependsOn() {
128128
}
129129

130130
@Override
131-
public void createContext(Builder parent) {
131+
public void build(Builder parent) {
132132

133133
}
134134
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,6 @@ static BeanContextBuilder newBuilder() {
7777
return new DBeanContextBuilder();
7878
}
7979

80-
/**
81-
* Return the module name of the bean context.
82-
*
83-
* @see ContextModule
84-
*/
85-
String getName();
86-
87-
/**
88-
* Return the names of module features this bean context provides.
89-
*
90-
* @see ContextModule
91-
*/
92-
String[] getProvides();
93-
94-
/**
95-
* Return the names of modules this bean context depends on.
96-
*
97-
* @see ContextModule
98-
*/
99-
String[] getDependsOn();
100-
10180
/**
10281
* Return a single bean given the type.
10382
*

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@
88
import org.slf4j.LoggerFactory;
99

1010
import java.lang.annotation.Annotation;
11-
import java.util.ArrayList;
12-
import java.util.Arrays;
13-
import java.util.HashMap;
14-
import java.util.Iterator;
15-
import java.util.LinkedHashSet;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.ServiceLoader;
19-
import java.util.Set;
11+
import java.util.*;
2012
import java.util.concurrent.locks.ReentrantLock;
2113
import java.util.function.Consumer;
2214

@@ -120,12 +112,12 @@ public BeanContext build() {
120112
" Refer to https://avaje.io/inject#gradle");
121113
}
122114
log.debug("building context with modules {}", moduleNames);
123-
Builder rootBuilder = Builder.newRootBuilder(suppliedBeans, enrichBeans);
115+
Builder builder = Builder.newBuilder(suppliedBeans, enrichBeans);
124116
for (BeanContextFactory factory : factoryOrder.factories()) {
125-
factory.createContext(rootBuilder);
117+
factory.build(builder);
126118
}
127119

128-
BeanContext beanContext = rootBuilder.build();
120+
BeanContext beanContext = builder.build();
129121
// entire graph built, fire postConstruct
130122
beanContext.start();
131123
if (shutdownHook) {
@@ -180,21 +172,6 @@ private static class ShutdownAwareBeanContext implements BeanContext {
180172
Runtime.getRuntime().addShutdownHook(hook);
181173
}
182174

183-
@Override
184-
public String getName() {
185-
return context.getName();
186-
}
187-
188-
@Override
189-
public String[] getProvides() {
190-
return context.getProvides();
191-
}
192-
193-
@Override
194-
public String[] getDependsOn() {
195-
return context.getDependsOn();
196-
}
197-
198175
@Override
199176
public <T> T getBean(Class<T> beanClass) {
200177
return context.getBean(beanClass);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.avaje.inject.spi;
22

3-
import io.avaje.inject.BeanContext;
4-
53
/**
64
* This is the service loader interface defining the bean contexts that can be created.
75
*/
@@ -23,7 +21,7 @@ public interface BeanContextFactory {
2321
String[] getDependsOn();
2422

2523
/**
26-
* Create and return the BeanContext.
24+
* Build all the beans.
2725
*/
28-
void createContext(Builder parent);
26+
void build(Builder builder);
2927
}

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,14 @@ public interface Builder {
2121
* @param enrichBeans The list of classes we want to have with mockito spy enhancement
2222
*/
2323
@SuppressWarnings("rawtypes")
24-
static Builder newRootBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
24+
static Builder newBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
2525
if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
2626
// simple case, no mocks or spies
2727
return new DBuilder();
2828
}
2929
return new DBuilderExtn(suppliedBeans, enrichBeans);
3030
}
3131

32-
/**
33-
* Return the name of the (module) context this builder is creating.
34-
*/
35-
String getName();
36-
37-
/**
38-
* Return the names of module features that this module provides.
39-
*/
40-
String[] getProvides();
41-
42-
/**
43-
* Return the names of modules that this module depends on.
44-
*/
45-
String[] getDependsOn();
46-
47-
/**
48-
* Set a parent builder that can provide cross-module dependencies.
49-
*/
50-
void setParent(Builder parent);
51-
5232
/**
5333
* Return true if the bean should be created and registered with the context.
5434
* <p/>
@@ -97,11 +77,6 @@ static Builder newRootBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean>
9777
*/
9878
void addInjector(Consumer<Builder> injector);
9979

100-
/**
101-
* Add a child context.
102-
*/
103-
void addChild(BeanContextFactory factory);
104-
10580
/**
10681
* Get an optional dependency.
10782
*/

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,16 @@ class DBeanContext implements BeanContext {
1717
private static final Logger log = LoggerFactory.getLogger(DBeanContext.class);
1818

1919
private final ReentrantLock lock = new ReentrantLock();
20-
21-
private final String name;
22-
23-
private final String[] provides;
24-
25-
private final String[] dependsOn;
26-
2720
private final List<BeanLifecycle> lifecycleList;
28-
2921
private final DBeanMap beans;
3022

3123
private boolean closed;
3224

33-
DBeanContext(String name, String[] provides, String[] dependsOn, List<BeanLifecycle> lifecycleList, DBeanMap beans) {//}, Map<String, BeanContext> children) {
34-
this.name = name;
35-
this.provides = provides;
36-
this.dependsOn = dependsOn;
25+
DBeanContext(List<BeanLifecycle> lifecycleList, DBeanMap beans) {
3726
this.lifecycleList = lifecycleList;
3827
this.beans = beans;
3928
}
4029

41-
@Override
42-
public String getName() {
43-
return name;
44-
}
45-
46-
@Override
47-
public String[] getProvides() {
48-
return provides;
49-
}
50-
51-
@Override
52-
public String[] getDependsOn() {
53-
return dependsOn;
54-
}
55-
5630
@Override
5731
public <T> T getBean(Class<T> beanClass) {
5832
return getBean(beanClass, null);
@@ -130,9 +104,7 @@ public List<Object> getBeansWithAnnotation(Class<?> annotation) {
130104
public void start() {
131105
lock.lock();
132106
try {
133-
if (name != null) {
134-
log.debug("firing postConstruct on beans in context:{}", name);
135-
}
107+
log.trace("firing postConstruct");
136108
for (BeanLifecycle bean : lifecycleList) {
137109
bean.postConstruct();
138110
}
@@ -148,9 +120,7 @@ public void close() {
148120
if (!closed) {
149121
// we only allow one call to preDestroy
150122
closed = true;
151-
if (name != null) {
152-
log.debug("firing preDestroy on beans in context:{}", name);
153-
}
123+
log.trace("firing preDestroy");
154124
for (BeanLifecycle bean : lifecycleList) {
155125
bean.preDestroy();
156126
}

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

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@
22

33
import io.avaje.inject.BeanContext;
44
import io.avaje.inject.BeanEntry;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
7-
85
import jakarta.inject.Provider;
9-
import java.util.ArrayList;
10-
import java.util.LinkedHashMap;
11-
import java.util.LinkedHashSet;
12-
import java.util.List;
13-
import java.util.Map;
14-
import java.util.Optional;
15-
import java.util.Set;
6+
7+
import java.util.*;
168
import java.util.function.Consumer;
179

1810
class DBuilder implements Builder {
1911

20-
private static final Logger log = LoggerFactory.getLogger(DBuilder.class);
21-
2212
/**
2313
* List of Lifecycle beans.
2414
*/
@@ -34,21 +24,6 @@ class DBuilder implements Builder {
3424
*/
3525
final DBeanMap beanMap = new DBeanMap();
3626

37-
/**
38-
* The context/module name.
39-
*/
40-
private final String name;
41-
42-
/**
43-
* The module features this context provides.
44-
*/
45-
private final String[] provides;
46-
47-
/**
48-
* The other modules this context dependsOn (that should be built prior).
49-
*/
50-
private final String[] dependsOn;
51-
5227
/**
5328
* Debug of the current bean being wired - used in injection errors.
5429
*/
@@ -59,51 +34,22 @@ class DBuilder implements Builder {
5934
*/
6035
private boolean runningPostConstruct;
6136

62-
/**
63-
* Create for the root builder.
64-
*/
65-
DBuilder() {
66-
this.name = null;
67-
this.provides = null;
68-
this.dependsOn = null;
69-
}
70-
71-
@Override
72-
public String getName() {
73-
return name;
74-
}
75-
76-
@Override
77-
public String[] getProvides() {
78-
return provides;
79-
}
80-
81-
@Override
82-
public String[] getDependsOn() {
83-
return dependsOn;
84-
}
85-
86-
@Override
87-
public void setParent(Builder parent) {
88-
//this.parent = parent;
89-
}
90-
9137
@Override
9238
public boolean isAddBeanFor(Class<?>... types) {
9339
return isAddBeanFor(null, types);
9440
}
9541

96-
protected void next(String name, Class<?>... types) {
97-
injectTarget = firstOf(types);
98-
beanMap.nextBean(name, types);
99-
}
100-
10142
@Override
10243
public boolean isAddBeanFor(String name, Class<?>... types) {
10344
next(name, types);
10445
return true;
10546
}
10647

48+
protected void next(String name, Class<?>... types) {
49+
injectTarget = firstOf(types);
50+
beanMap.nextBean(name, types);
51+
}
52+
10753
private Class<?> firstOf(Class<?>[] types) {
10854
return types != null && types.length > 0 ? types[0] : null;
10955
}
@@ -133,11 +79,6 @@ private <T> T getMaybe(Class<T> beanClass, String name) {
13379
return (entry == null) ? null : entry.getBean();
13480
}
13581

136-
@Override
137-
public void addChild(BeanContextFactory factory) {
138-
factory.createContext(this);
139-
}
140-
14182
/**
14283
* Return the bean to register potentially with spy enhancement.
14384
*/
@@ -238,9 +179,6 @@ public <T> T get(Class<T> cls, String name) {
238179
}
239180

240181
private void runInjectors() {
241-
if (name != null) {
242-
log.debug("perform field injection in context:{}", name);
243-
}
244182
runningPostConstruct = true;
245183
for (Consumer<Builder> injector : injectors) {
246184
injector.accept(this);
@@ -249,6 +187,6 @@ private void runInjectors() {
249187

250188
public BeanContext build() {
251189
runInjectors();
252-
return new DBeanContext(name, provides, dependsOn, lifecycleList, beanMap);
190+
return new DBeanContext(lifecycleList, beanMap);
253191
}
254192
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public String[] getDependsOn() {
128128
}
129129

130130
@Override
131-
public void createContext(Builder parent) {
131+
public void build(Builder parent) {
132132

133133
}
134134
}

0 commit comments

Comments
 (0)