Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
Re-enabled support for PostConstruct (and PreDestroy) methods with ar…
Browse files Browse the repository at this point in the history
…guments
  • Loading branch information
tschneidereit committed Dec 24, 2011
1 parent cd3ab09 commit 07baa3b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 80 deletions.
6 changes: 5 additions & 1 deletion src/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<context name="method"/>
<context name="class"/>
<attribute name="name" type="String" required="false"
description="Name for a named injection"/>
description="Name for a named injection, can be used multiple times for naming individual method arguments"/>
<attribute name="optional" type="Boolean" required="false"
description="Flag indicating that the annotated injection is optional. If not set, a missing dependency will cause an exception"/>
</metadata>
Expand All @@ -21,11 +21,15 @@
<context name="method"/>
<attribute name="order" type="int" required="false"
description="Flag indicating at which point in the total order of PostConstruct-annotated methods this method should be called. Order is independent of inheritance"/>
<attribute name="name" type="String" required="false"
description="Name for a named injection, can be used multiple times for naming individual method arguments"/>
</metadata>
<metadata name="PreDestroy"
description="Swiftsuspenders flag to automatically call this method before object destruction (before the instance is removed from the Injector's lookup maps and thus not available to it anymore)">
<context name="method"/>
<attribute name="order" type="int" required="false"
description="Flag indicating at which point in the total order of PostConstruct-annotated methods this method should be called. Order is independent of inheritance"/>
<attribute name="name" type="String" required="false"
description="Name for a named injection, can be used multiple times for naming individual method arguments"/>
</metadata>
</annotations>
15 changes: 10 additions & 5 deletions src/org/swiftsuspenders/DescribeTypeJSONReflector.as
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,24 @@ package org.swiftsuspenders
for (var i : int = 0; i < length; i++)
{
var method : Object = methods[i];
var parameters : Object =
var injectParameters : Object =
method.metadata && extractTagParameters(tag, method.metadata);
if (!parameters)
if (!injectParameters)
{
continue;
}
var order : int = parseInt(parameters.order, 10);
var parameterNames : Array = (injectParameters.name || '').split(',');
var parameters : Array = method.parameters;
const requiredParameters : uint =
gatherMethodParameters(parameters, parameterNames);
var order : int = parseInt(injectParameters.order, 10);
//int can't be NaN, so we have to verify that parsing succeeded by comparison
if (order.toString(10) != parameters.order)
if (order.toString(10) != injectParameters.order)
{
order = int.MAX_VALUE;
}
injectionPoints.push(new injectionPointClass(method.name, order));
injectionPoints.push(new injectionPointClass(
method.name, parameters, requiredParameters, order));
}
if (injectionPoints.length > 0)
{
Expand Down
9 changes: 7 additions & 2 deletions src/org/swiftsuspenders/DescribeTypeReflector.as
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ package org.swiftsuspenders
for each (var node : XML in
_currentFactoryXML.method.metadata.(@name == tag))
{
const nameArgs : XMLList = node.arg.(@key == 'name');
const parameters : Array =
gatherMethodParameters(node.parent().parameter, nameArgs);
const requiredParameters : uint = parameters.required;
delete parameters.required;
var order : Number = parseInt(node.arg.(@key == 'order').@value);
injectionPoints.push(new injectionPointType(
node.parent().@name, isNaN(order) ? int.MAX_VALUE : order));
injectionPoints.push(new injectionPointType(node.parent().@name,
parameters, requiredParameters, isNaN(order) ? int.MAX_VALUE : order));
}
if (injectionPoints.length > 0)
{
Expand Down
31 changes: 6 additions & 25 deletions src/org/swiftsuspenders/typedescriptions/OrderedInjectionPoint.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,18 @@

package org.swiftsuspenders.typedescriptions
{
import org.swiftsuspenders.Injector;

public class OrderedInjectionPoint extends InjectionPoint
public class OrderedInjectionPoint extends MethodInjectionPoint
{
//---------------------- Public Properties ----------------------//


//---------------------- Private / Protected Properties ----------------------//
public var order : int;


//---------------------- Public Methods ----------------------//
protected var _methodName : String;

protected var _orderValue : int;

public function OrderedInjectionPoint()
{
}

public function get order() : int
{
return _orderValue;
}

override public function applyInjection(
target : Object, targetType : Class, injector : Injector) : void
public function OrderedInjectionPoint(methodName : String, parameters : Array,
requiredParameters : uint, order : int)
{
//TODO: Restore parameter-injection
target[_methodName]();
super(methodName, parameters, requiredParameters, false);
this.order = order;
}

//---------------------- Private / Protected Methods ----------------------//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ package org.swiftsuspenders.typedescriptions
public class PostConstructInjectionPoint extends OrderedInjectionPoint
{
//---------------------- Public Methods ----------------------//
public function PostConstructInjectionPoint(methodName : String, order : int)
public function PostConstructInjectionPoint(methodName : String, parameters : Array,
requiredParameters : uint, order : int)
{
_methodName = methodName;
_orderValue = order;
super(methodName, parameters, requiredParameters, order);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ package org.swiftsuspenders.typedescriptions
public class PreDestroyInjectionPoint extends OrderedInjectionPoint
{
//---------------------- Public Methods ----------------------//
public function PreDestroyInjectionPoint(methodName : String, order : int)
public function PreDestroyInjectionPoint(methodName : String, parameters : Array,
requiredParameters : uint, order : int)
{
_methodName = methodName;
_orderValue = order;
super(methodName, parameters, requiredParameters, order);
}

}
}
35 changes: 15 additions & 20 deletions test/org/swiftsuspenders/InjectorTests.as
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
/*
* Copyright (c) 2009 the original author or authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
* Copyright (c) 2011 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/

package org.swiftsuspenders
{
Expand Down Expand Up @@ -52,6 +37,7 @@ package org.swiftsuspenders
import org.swiftsuspenders.support.injectees.OptionalClassInjectee;
import org.swiftsuspenders.support.injectees.OptionalOneRequiredParameterMethodInjectee;
import org.swiftsuspenders.support.injectees.OrderedPostConstructInjectee;
import org.swiftsuspenders.support.injectees.PostConstructWithArgInjectee;
import org.swiftsuspenders.support.injectees.RecursiveInterfaceInjectee;
import org.swiftsuspenders.support.injectees.SetterInjectee;
import org.swiftsuspenders.support.injectees.StringInjectee;
Expand Down Expand Up @@ -492,6 +478,15 @@ package org.swiftsuspenders
Assert.assertTrue(injectee.someProperty);
}

[Test]
public function postConstructWithArgIsCalledCorrectly():void
{
injector.map(Clazz);
var injectee:PostConstructWithArgInjectee =
injector.getInstance(PostConstructWithArgInjectee);
assertThat(injectee.property, isA(Clazz));
}

[Test]
public function postConstructMethodsCalledAsOrdered():void
{
Expand Down
25 changes: 5 additions & 20 deletions test/org/swiftsuspenders/support/injectees/ClassInjectee.as
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
/*
* Copyright (c) 2009 the original author or authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
* Copyright (c) 2011 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/

package org.swiftsuspenders.support.injectees
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2011 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/

package org.swiftsuspenders.support.injectees
{
import org.swiftsuspenders.support.types.Clazz;

public class PostConstructWithArgInjectee
{
public var property:Clazz;

[PostConstruct]
public function doSomeStuff(arg : Clazz):void
{
property = arg;
}
}
}

0 comments on commit 07baa3b

Please sign in to comment.