Skip to content

Commit

Permalink
Better singleton testing
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Oct 8, 2021
1 parent 0bf09ba commit 8fed7ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
12 changes: 4 additions & 8 deletions src/main/java/aerogel/internal/codegen/ClassInstanceMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ private ClassInstanceMaker() {
mv.visitTypeInsn(NEW, intName(ct));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, intName(ct), CONSTRUCTOR_NAME, "()V", false);
// if this is a singleton store the value in the AtomicReference
if (singleton) {
appendSingletonWrite(mv, proxyName);
}
// no types for the class init are required
elements = NO_ELEMENT;
} else {
Expand All @@ -186,10 +182,10 @@ private ClassInstanceMaker() {
loadParameters(elements, mv);
// instantiate the constructor with the parameters
mv.visitMethodInsn(INVOKESPECIAL, intName(ct), CONSTRUCTOR_NAME, consDesc(target), false);
// if this is a singleton store the value in the AtomicReference
if (singleton) {
appendSingletonWrite(mv, proxyName);
}
}
// if this is a singleton store the value in the AtomicReference
if (singleton) {
appendSingletonWrite(mv, proxyName);
}
// return the created value
mv.visitInsn(ARETURN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ private FactoryMethodInstanceMaker() {
loadParameters(elements, mv);
// invoke the method with these arguments
mv.visitMethodInsn(INVOKESTATIC, intName(ct), method.getName(), methodDesc(method), ct.isInterface());
// if this is a singleton store the value in the AtomicReference
if (shouldBeSingleton) {
appendSingletonWrite(mv, proxyName);
}
}
// if this is a singleton store the value in the AtomicReference
if (shouldBeSingleton) {
appendSingletonWrite(mv, proxyName);
}
// if the return type of the class is primitive we need to box it as the next instance takes care
// of the primitive / non-primitive conversion and the method signature indicates an object return type
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/aerogel/SingletonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

public class SingletonTest {

@Name("holder")
private static StringHolder factoryStringHolder() {
return new StringHolder();
}

@Test
void testConstructingSingleton() {
Injector injector = Injector.newInjector();
Expand All @@ -45,6 +50,20 @@ void testConstructingSingleton() {
Assertions.assertSame(value, value2);
}

@Test
void testFactoryMethodSingleton() throws NoSuchMethodException {
Injector injector = Injector.newInjector();
injector.install(Bindings.factory(SingletonTest.class.getDeclaredMethod("factoryStringHolder")));

StringHolder holderA = injector.instance(Element.get(StringHolder.class).requireName("holder"));
Assertions.assertNotNull(holderA);

StringHolder holderB = injector.instance(Element.get(StringHolder.class).requireName("holder"));
Assertions.assertNotNull(holderB);

Assertions.assertSame(holderA, holderB);
}

@Singleton
private static final class StringHolder {

Expand Down

0 comments on commit 8fed7ba

Please sign in to comment.