Skip to content

Commit 8fed7ba

Browse files
committed
Better singleton testing
1 parent 0bf09ba commit 8fed7ba

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/main/java/aerogel/internal/codegen/ClassInstanceMaker.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ private ClassInstanceMaker() {
170170
mv.visitTypeInsn(NEW, intName(ct));
171171
mv.visitInsn(DUP);
172172
mv.visitMethodInsn(INVOKESPECIAL, intName(ct), CONSTRUCTOR_NAME, "()V", false);
173-
// if this is a singleton store the value in the AtomicReference
174-
if (singleton) {
175-
appendSingletonWrite(mv, proxyName);
176-
}
177173
// no types for the class init are required
178174
elements = NO_ELEMENT;
179175
} else {
@@ -186,10 +182,10 @@ private ClassInstanceMaker() {
186182
loadParameters(elements, mv);
187183
// instantiate the constructor with the parameters
188184
mv.visitMethodInsn(INVOKESPECIAL, intName(ct), CONSTRUCTOR_NAME, consDesc(target), false);
189-
// if this is a singleton store the value in the AtomicReference
190-
if (singleton) {
191-
appendSingletonWrite(mv, proxyName);
192-
}
185+
}
186+
// if this is a singleton store the value in the AtomicReference
187+
if (singleton) {
188+
appendSingletonWrite(mv, proxyName);
193189
}
194190
// return the created value
195191
mv.visitInsn(ARETURN);

src/main/java/aerogel/internal/codegen/FactoryMethodInstanceMaker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ private FactoryMethodInstanceMaker() {
121121
loadParameters(elements, mv);
122122
// invoke the method with these arguments
123123
mv.visitMethodInsn(INVOKESTATIC, intName(ct), method.getName(), methodDesc(method), ct.isInterface());
124-
// if this is a singleton store the value in the AtomicReference
125-
if (shouldBeSingleton) {
126-
appendSingletonWrite(mv, proxyName);
127-
}
124+
}
125+
// if this is a singleton store the value in the AtomicReference
126+
if (shouldBeSingleton) {
127+
appendSingletonWrite(mv, proxyName);
128128
}
129129
// if the return type of the class is primitive we need to box it as the next instance takes care
130130
// of the primitive / non-primitive conversion and the method signature indicates an object return type

src/test/java/aerogel/SingletonTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
public class SingletonTest {
3131

32+
@Name("holder")
33+
private static StringHolder factoryStringHolder() {
34+
return new StringHolder();
35+
}
36+
3237
@Test
3338
void testConstructingSingleton() {
3439
Injector injector = Injector.newInjector();
@@ -45,6 +50,20 @@ void testConstructingSingleton() {
4550
Assertions.assertSame(value, value2);
4651
}
4752

53+
@Test
54+
void testFactoryMethodSingleton() throws NoSuchMethodException {
55+
Injector injector = Injector.newInjector();
56+
injector.install(Bindings.factory(SingletonTest.class.getDeclaredMethod("factoryStringHolder")));
57+
58+
StringHolder holderA = injector.instance(Element.get(StringHolder.class).requireName("holder"));
59+
Assertions.assertNotNull(holderA);
60+
61+
StringHolder holderB = injector.instance(Element.get(StringHolder.class).requireName("holder"));
62+
Assertions.assertNotNull(holderB);
63+
64+
Assertions.assertSame(holderA, holderB);
65+
}
66+
4867
@Singleton
4968
private static final class StringHolder {
5069

0 commit comments

Comments
 (0)