Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 104 lines (93 sloc) 3.325 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/*
* Copyright (c) 2009 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.injectionpoints
{
import flash.utils.Dictionary;
import org.swiftsuspenders.Injector;
import org.swiftsuspenders.InjectorError;
import org.swiftsuspenders.InjectionConfig;

public class MethodInjectionPoint extends InjectionPoint
{
/*******************************************************************************************
* private properties *
*******************************************************************************************/
protected var methodName : String;
protected var mappings : Array;
protected var parameterTypes : Array;


/*******************************************************************************************
* public methods *
*******************************************************************************************/
public function MethodInjectionPoint(node : XML, injectorMappings : Dictionary)
{
super(node, injectorMappings);
}

override public function applyInjection(
target : Object, injector : Injector, singletons : Dictionary) : Object
{
var parameters : Array = [];
var length : int = mappings.length;
for (var i : int = 0; i < length; i++)
{
var config : InjectionConfig = mappings[i][parameterTypes[i]];
if (!config)
{
throw(new InjectorError(
'Injector is missing a rule to handle injection into target ' + target +
'. Target dependency: ' + parameterTypes[i] + ', method: ' + methodName +
', parameter: ' + (i + 1)
));
}

var injection : Object = config.getResponse(injector, singletons);
parameters[i] = injection;
}
var method : Function = target[methodName];
method.apply(target, parameters);
return target;
}


/*******************************************************************************************
* protected methods *
*******************************************************************************************/
override protected function initializeInjection(node : XML, injectorMappings : Dictionary) : void
{
var nameArgs : XMLList = node.arg.(@key == 'name');
var methodNode : XML = node.parent();

methodName = methodNode.@name.toString();
mappings = [];
parameterTypes = [];

gatherParameters(methodNode, nameArgs, injectorMappings);
}

protected function gatherParameters(
methodNode : XML, nameArgs : XMLList, injectorMappings : Dictionary) : void
{
var i : int = 0;
for each (var parameter : XML in methodNode.parameter)
{
var parameterMappings : Dictionary;
var injectionName : String;
if (nameArgs[i])
{
injectionName = nameArgs[i].@value.toString();
}
if (injectionName)
{
parameterMappings = injectorMappings[injectionName];
if (!parameterMappings)
{
parameterMappings = injectorMappings[injectionName] = new Dictionary();
}
}
else
{
parameterMappings = injectorMappings;
}
mappings.push(parameterMappings);
parameterTypes.push(parameter.@type.toString());
i++;
}
}
}
}
Something went wrong with that request. Please try again.