Permalink
Browse files

Changed behavior of child injectors. Instead of permanently returning…

… to the parent injector once a missing dependency is encountered, the dependency is satisfied by the parent injector but the child injector retains control and processes subsequent injections further down the object tree
  • Loading branch information...
1 parent 255805e commit 17149eddcbe03f6615db3fc4f2cd6d86fe07f057 @tschneidereit committed May 10, 2010
@@ -28,35 +28,37 @@ package org.swiftsuspenders
/*******************************************************************************************
* public methods *
*******************************************************************************************/
- public function InjectionConfig(
- request : Class, injectionName : String, injector : Injector)
+ public function InjectionConfig(request : Class, injectionName : String, injector : Injector)
{
this.request = request;
this.injectionName = injectionName;
- m_injector = injector;
}
- public function getResponse() : Object
+ public function getResponse(injector : Injector, traverseInjectorsTree : Boolean = true) : Object
{
if (m_result)
{
- return m_result.getResponse();
+ return m_result.getResponse(m_injector || injector);
+ }
+ if (!traverseInjectorsTree)
+ {
+ return null;
}
- var parentConfig : InjectionConfig = m_injector.getParentMapping(request, injectionName);
+ var parentConfig : InjectionConfig = (m_injector || injector).getParentMapping(request, injectionName);
if (parentConfig)
{
- return parentConfig.getResponse();
+ return parentConfig.getResponse(injector, false);
}
return null;
}
- public function hasResponse() : Boolean
+ public function hasResponse(injector : Injector) : Boolean
{
if (m_result)
{
return true;
}
- var parentConfig : InjectionConfig = m_injector.getParentMapping(request, injectionName);
+ var parentConfig : InjectionConfig = (m_injector || injector).getParentMapping(request, injectionName);
if (parentConfig)
{
return true;
@@ -72,7 +74,6 @@ package org.swiftsuspenders
public function setInjector(injector : Injector) : void
{
m_injector = injector;
- m_result.setInjector(injector);
}
}
}
@@ -52,15 +52,15 @@ package org.swiftsuspenders
public function mapValue(whenAskedFor : Class, useValue : Object, named : String = "") : *
{
var config : InjectionConfig = getMapping(whenAskedFor, named);
- config.setResult(new InjectValueResult(useValue, this));
+ config.setResult(new InjectValueResult(useValue));
return config;
}
public function mapClass(
whenAskedFor : Class, instantiateClass : Class, named : String = "") : *
{
var config : InjectionConfig = getMapping(whenAskedFor, named);
- config.setResult(new InjectClassResult(instantiateClass, this));
+ config.setResult(new InjectClassResult(instantiateClass));
return config;
}
@@ -73,7 +73,7 @@ package org.swiftsuspenders
whenAskedFor : Class, useSingletonOf : Class, named : String = "") : *
{
var config : InjectionConfig = getMapping(whenAskedFor, named);
- config.setResult(new InjectSingletonResult(useSingletonOf, this));
+ config.setResult(new InjectSingletonResult(useSingletonOf));
return config;
}
@@ -115,7 +115,7 @@ package org.swiftsuspenders
for (var i : int = 0; i < length; i++)
{
var injectionPoint : InjectionPoint = injectionPoints[i];
- injectionPoint.applyInjection(target);
+ injectionPoint.applyInjection(target, this);
}
}
@@ -128,7 +128,7 @@ package org.swiftsuspenders
getInjectionPoints(clazz);
injectionPoint = m_constructorInjectionPoints[clazz];
}
- var instance : * = injectionPoint.applyInjection(clazz);
+ var instance : * = injectionPoint.applyInjection(clazz, this);
injectInto(instance);
return instance;
}
@@ -152,19 +152,19 @@ package org.swiftsuspenders
{
return false;
}
- return mapping.hasResponse();
+ return mapping.hasResponse(this);
}
public function getInstance(clazz : Class, named : String = '') : *
{
var mapping : InjectionConfig = getConfigurationForRequest(clazz, named);
- if (!mapping || !mapping.hasResponse())
+ if (!mapping || !mapping.hasResponse(this))
{
throw new InjectorError('Error while getting mapping response: ' +
'No mapping defined for class ' + getQualifiedClassName(clazz) +
', named "' + named + '"');
}
- return mapping.getResponse();
+ return mapping.getResponse(this);
}
public function createChildInjector() : Injector
@@ -30,9 +30,9 @@ package org.swiftsuspenders.injectionpoints
super(node, injector);
}
- override public function applyInjection(target : Object) : Object
+ override public function applyInjection(target : Object, injector : Injector) : Object
{
- var p : Array = gatherParameterValues(target);
+ var p : Array = gatherParameterValues(target, injector);
//the only way to implement ctor injections, really!
switch (p.length)
{
@@ -19,7 +19,7 @@ package org.swiftsuspenders.injectionpoints
initializeInjection(node, injector);
}
- public function applyInjection(target : Object) : Object
+ public function applyInjection(target : Object, injector : Injector) : Object
{
return target;
}
@@ -32,9 +32,9 @@ package org.swiftsuspenders.injectionpoints
super(node, injector);
}
- override public function applyInjection(target : Object) : Object
+ override public function applyInjection(target : Object, injector : Injector) : Object
{
- var parameters : Array = gatherParameterValues(target);
+ var parameters : Array = gatherParameterValues(target, injector);
var method : Function = target[methodName];
method.apply(target, parameters);
return target;
@@ -93,14 +93,14 @@ package org.swiftsuspenders.injectionpoints
}
}
- protected function gatherParameterValues(target : Object) : Array
+ protected function gatherParameterValues(target : Object, injector : Injector) : Array
{
var parameters : Array = [];
var length : int = m_injectionConfigs.length;
for (var i : int = 0; i < length; i++)
{
var config : InjectionConfig = m_injectionConfigs[i];
- var injection : Object = config.getResponse();
+ var injection : Object = config.getResponse(injector);
if (injection == null)
{
if (i >= requiredParameters)
@@ -7,14 +7,16 @@
package org.swiftsuspenders.injectionpoints
{
+ import org.swiftsuspenders.Injector;
+
public class NoParamsConstructorInjectionPoint extends InjectionPoint
{
public function NoParamsConstructorInjectionPoint()
{
super(null, null);
}
- override public function applyInjection(target : Object) : Object
+ override public function applyInjection(target : Object, injector : Injector) : Object
{
return new target();
}
@@ -31,7 +31,7 @@ package org.swiftsuspenders.injectionpoints
return orderValue;
}
- override public function applyInjection(target : Object) : Object
+ override public function applyInjection(target : Object, injector : Injector) : Object
{
target[methodName]();
return target;
@@ -31,9 +31,9 @@ package org.swiftsuspenders.injectionpoints
super(node, injector);
}
- override public function applyInjection(target : Object) : Object
+ override public function applyInjection(target : Object, injector : Injector) : Object
{
- var injection : Object = m_injectionConfig.getResponse();
+ var injection : Object = m_injectionConfig.getResponse(injector);
if (injection == null)
{
throw(
@@ -20,15 +20,14 @@ package org.swiftsuspenders.injectionresults
/*******************************************************************************************
* public methods *
*******************************************************************************************/
- public function InjectClassResult(responseType : Class, injector : Injector)
+ public function InjectClassResult(responseType : Class)
{
m_responseType = responseType;
- super(injector);
}
- override public function getResponse() : Object
+ override public function getResponse(injector : Injector) : Object
{
- return m_injector.instantiate(m_responseType);
+ return injector.instantiate(m_responseType);
}
}
}
@@ -8,6 +8,7 @@
package org.swiftsuspenders.injectionresults
{
import org.swiftsuspenders.InjectionConfig;
+ import org.swiftsuspenders.Injector;
public class InjectOtherRuleResult extends InjectionResult
{
@@ -23,12 +24,11 @@ package org.swiftsuspenders.injectionresults
public function InjectOtherRuleResult(rule : InjectionConfig)
{
m_rule = rule;
- super(null);
}
- override public function getResponse() : Object
+ override public function getResponse(injector : Injector) : Object
{
- return m_rule.getResponse();
+ return m_rule.getResponse(injector);
}
}
}
@@ -21,24 +21,23 @@ package org.swiftsuspenders.injectionresults
/*******************************************************************************************
* public methods *
*******************************************************************************************/
- public function InjectSingletonResult(responseType : Class, injector : Injector)
+ public function InjectSingletonResult(responseType : Class)
{
m_responseType = responseType;
- super(injector);
}
- override public function getResponse() : Object
+ override public function getResponse(injector : Injector) : Object
{
- return m_response ||= createResponse();
+ return m_response ||= createResponse(injector);
}
/*******************************************************************************************
* private methods *
*******************************************************************************************/
- private function createResponse() : Object
+ private function createResponse(injector : Injector) : Object
{
- return m_injector.instantiate(m_responseType);
+ return injector.instantiate(m_responseType);
}
}
}
@@ -14,19 +14,18 @@ package org.swiftsuspenders.injectionresults
/*******************************************************************************************
* private properties *
*******************************************************************************************/
- private var m_value : Object
+ private var m_value : Object;
/*******************************************************************************************
* public methods *
*******************************************************************************************/
- public function InjectValueResult(value : Object, injector : Injector)
+ public function InjectValueResult(value : Object)
{
m_value = value;
- super(injector);
}
- override public function getResponse() : Object
+ override public function getResponse(injector : Injector) : Object
{
return m_value;
}
@@ -12,27 +12,15 @@ package org.swiftsuspenders.injectionresults
public class InjectionResult
{
/*******************************************************************************************
- * protected properties *
- *******************************************************************************************/
- protected var m_injector : Injector;
-
-
- /*******************************************************************************************
* public methods *
*******************************************************************************************/
- public function InjectionResult(injector : Injector)
+ public function InjectionResult()
{
- m_injector = injector;
}
- public function getResponse() : Object
+ public function getResponse(injector : Injector) : Object
{
return null;
}
-
- public function setInjector(injector : Injector) : void
- {
- m_injector = injector;
- }
}
}
Oops, something went wrong.

0 comments on commit 17149ed

Please sign in to comment.