Skip to content

Commit

Permalink
kill code
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanenicolas committed Mar 29, 2016
1 parent bc06b43 commit 403d326
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 42 deletions.
8 changes: 1 addition & 7 deletions toothpick-runtime/src/main/java/toothpick/InjectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
public class InjectorImpl implements Injector {
private IdentityHashMap<Class, Provider> scope = new IdentityHashMap<>();
private Injector parent;
private Object key;
private final List<InjectorImpl> parentInjectors;

public InjectorImpl(Injector parent, Object key, Module... modules) {
public InjectorImpl(Injector parent, Module... modules) {
this.parent = parent;
this.key = key;
parentInjectors = getParentInjectors();
installModules(modules);
}
Expand All @@ -35,10 +33,6 @@ public InjectorImpl(Injector parent, Object key, Module... modules) {
return parent;
}

@Override public Object getKey() {
return key;
}

@Override public <T> void inject(T obj) {
MemberInjector<T> memberInjector = MemberInjectorRegistry.getMemberInjector((Class<T>) obj.getClass());
memberInjector.inject(obj, this);
Expand Down
20 changes: 10 additions & 10 deletions toothpick-runtime/src/main/java/toothpick/ToothPick.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class ToothPick {

//http://stackoverflow.com/a/29421697/693752
//it should really be final, if not volatile
private static final ConcurrentHashMap<Object, Injector> mapKeyToInjector = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Object, Injector> MAP_KEY_TO_INJECTOR = new ConcurrentHashMap<>();

//JACOCO:OFF
private ToothPick() {
Expand All @@ -25,30 +25,30 @@ public static Injector createInjector(Object key, Module... modules) {
}

public static Injector createInjector(Injector parent, Object key, Module... modules) {
Injector injector = mapKeyToInjector.get(key);
Injector injector = MAP_KEY_TO_INJECTOR.get(key);
if (injector != null) {
throw new IllegalStateException(format("An injector for key %s already exists: %s", key, injector));
}

synchronized (mapKeyToInjector) {
injector = mapKeyToInjector.get(key);
synchronized (MAP_KEY_TO_INJECTOR) {
injector = MAP_KEY_TO_INJECTOR.get(key);
if (injector != null) {
throw new IllegalStateException(format("An injector for key %s already exists: %s", key, injector));
}
injector = new InjectorImpl(parent, key, modules);
mapKeyToInjector.put(key, injector);
injector = new InjectorImpl(parent, modules);
MAP_KEY_TO_INJECTOR.put(key, injector);
}
return injector;
}

public static Injector getInjector(Object key) {
return mapKeyToInjector.get(key);
return MAP_KEY_TO_INJECTOR.get(key);
}

public static Injector getOrCreateInjector(Injector parent, Object key, Module... modules) {
Injector injector = getInjector(key);
if (injector == null) {
synchronized (mapKeyToInjector) {
synchronized (MAP_KEY_TO_INJECTOR) {
if (injector == null) {
injector = createInjector(parent, key, modules);
}
Expand All @@ -62,10 +62,10 @@ public static void destroyInjector(Object key) {
if (injector == null) {
return;
}
mapKeyToInjector.remove(key);
MAP_KEY_TO_INJECTOR.remove(key);
}

public static void reset() {
mapKeyToInjector.clear();
MAP_KEY_TO_INJECTOR.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AllBindingsTest {

@Test public void simpleBinding_shouldCreateInjectedInstances_whenNotSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(Foo.class);
}
Expand All @@ -58,7 +58,7 @@ public class AllBindingsTest {

@Test public void simpleBinding_shouldCreateInjectedSingletons_whenSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(FooSingleton.class);
}
Expand All @@ -77,7 +77,7 @@ public class AllBindingsTest {

@Test public void bindToClass_shouldCreateInjectedInstances_whenBoundClassNotAnnotatedSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFoo.class).to(Foo.class);
}
Expand All @@ -100,7 +100,7 @@ public class AllBindingsTest {

@Test public void bindToClass_shouldCreateInjectedSingletons_whenBoundClassAnnotatedSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFooSingleton.class).to(FooSingleton.class);
}
Expand All @@ -124,7 +124,7 @@ public class AllBindingsTest {
//GIVEN
final Provider<IFoo> providerInstance = new IFooProvider();

Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFoo.class).toProvider(providerInstance);
}
Expand All @@ -144,7 +144,7 @@ public class AllBindingsTest {

@Test public void bindToProviderClass_shouldCreateNonInjectedInstances_whenProviderClassIsNotAnnotated() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFoo.class).toProvider(IFooProvider.class);
}
Expand All @@ -171,7 +171,7 @@ public class AllBindingsTest {
@Test public void bindToProviderClass_shouldCreateNonInjectedInstancesWithProviderSingleton_whenProviderClassIsAnnotatedSingleton()
throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFoo.class).toProvider(IFooProviderAnnotatedSingleton.class);
}
Expand All @@ -198,7 +198,7 @@ public class AllBindingsTest {

@Test public void bindToProviderClass_shouldCreateNonInjectedSingleton_whenProviderClassIsAnnotatedProvidesSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(IFoo.class).toProvider(IFooProviderAnnotatedProvidesSingleton.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SimpleInstanceCreationWithModuleTest {

@Test public void testSimpleInjection() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new SimpleModule());
Injector injector = new InjectorImpl(null, new SimpleModule());

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand All @@ -31,7 +31,7 @@ public class SimpleInstanceCreationWithModuleTest {

@Test public void testSimpleInjectionIsNotProducingSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new SimpleModule());
Injector injector = new InjectorImpl(null, new SimpleModule());

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SimpleInstanceCreationWithoutModuleTest {

@Test public void testSimpleInjection() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo");
Injector injector = new InjectorImpl(null);

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand All @@ -30,7 +30,7 @@ public class SimpleInstanceCreationWithoutModuleTest {

@Test public void testSimpleInjectionIsNotProducingSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo");
Injector injector = new InjectorImpl(null);

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SingletonCreationWithModuleTest {

@Test public void testIsProducingSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new SimpleModule());
Injector injector = new InjectorImpl(null, new SimpleModule());

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SingletonCreationWithoutModuleTest {

@Test public void testIsProducingSingleton() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo");
Injector injector = new InjectorImpl(null);

//WHEN
Foo instance = injector.createInstance(Foo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CreatedInstancesAreInjectedWhenNeeded {

@Test public void createdInstance_shouldBeInjected_whenBindingToAClassWithInjectFields() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(Foo.class).to(Foo.class);
}
Expand All @@ -38,7 +38,7 @@ public class CreatedInstancesAreInjectedWhenNeeded {

@Test public void createdInstance_shouldNotBeInjected_whenBindingToAProvider() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(Foo.class).toProvider(new Provider<Foo>() {
@Override public Foo get() {
Expand All @@ -57,7 +57,7 @@ public class CreatedInstancesAreInjectedWhenNeeded {

@Test public void createdInstance_shouldNotBeInjected_whenBindingToAProviderClass() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(Foo.class).toProvider(FooProvider.class);
}
Expand All @@ -72,7 +72,7 @@ public class CreatedInstancesAreInjectedWhenNeeded {

@Test public void createdProvider_shouldBeInjected_whenBindingToAProviderClassThatHasInjectedFields() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo", new Module() {
Injector injector = new InjectorImpl(null, new Module() {
{
bind(Foo.class).toProvider(FooProvider.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InjectionWithoutModuleTest {

@Test public void testSimpleInjection() throws Exception {
//GIVEN
Injector injector = new InjectorImpl(null, "foo");
Injector injector = new InjectorImpl(null);
Foo foo = new Foo();

//WHEN
Expand Down
6 changes: 0 additions & 6 deletions toothpick/src/main/java/toothpick/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
* An injector has its own scope.
*/
public interface Injector {
/**
* The key that identifies the injector in toothpick.
* @return the key of this injector.
*/
Object getKey();

/**
* Injects all fields of an object. This object will be the starting point of an injection sub-graph, i.e.
* all dependencies of this object will be injected as well when created.
Expand Down

0 comments on commit 403d326

Please sign in to comment.