Skip to content

Commit

Permalink
Change [TyphoonParameterInjectedWithStringRepresentation resolveTypeW…
Browse files Browse the repository at this point in the history
…ith:instanceOrClass] to just resolveType
  • Loading branch information
Jasper Blues committed Feb 2, 2014
1 parent 47f5c09 commit 67fd2fc
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 49 deletions.
46 changes: 42 additions & 4 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Source/Definition/Initializer/TyphoonInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef enum

- (id)initWithSelector:(SEL)initializer isClassMethodStrategy:(TyphoonComponentInitializerIsClassMethod)isClassMethod;

- (TyphoonDefinition*)definition;

- (NSArray*)injectedParameters;

/* ====================================================================================================================================== */
Expand Down
5 changes: 5 additions & 0 deletions Source/Definition/Initializer/TyphoonInitializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ - (void)dealloc
/* ====================================================================================================================================== */
#pragma mark - Interface Methods

- (TyphoonDefinition*)definition
{
return _definition;
}

- (NSArray*)injectedParameters
{
return [_injectedParameters copy];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
* - requiredType is set
* - The parameter is an object type. (If the parameter is an object type, classOrProtocol must be set explicitly).
*/
- (TyphoonTypeDescriptor*)resolveTypeWith:(id)classOrInstance;
- (TyphoonTypeDescriptor*)resolveType;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#import "TyphoonInitializer.h"
#import "TyphoonTypeDescriptor.h"
#import "TyphoonIntrospectionUtils.h"
#import "TyphoonInitializer+InstanceBuilder.h"
#import "TyphoonDefinition.h"


@implementation TyphoonParameterInjectedWithStringRepresentation
Expand All @@ -40,30 +42,23 @@ - (id)initWithIndex:(NSUInteger)index value:(NSString*)value requiredTypeOrNil:(
/* ====================================================================================================================================== */
#pragma mark - Interface Methods

- (TyphoonTypeDescriptor*)resolveTypeWith:(id)classOrInstance
- (TyphoonTypeDescriptor*)resolveType
{
if (_requiredType)
{
return [TyphoonTypeDescriptor descriptorWithClassOrProtocol:_requiredType];
}
else
{
BOOL isClass = class_isMetaClass(object_getClass(classOrInstance));
Class clazz;
if (isClass)
{
clazz = classOrInstance;
}
else
{
clazz = [classOrInstance class];
}
BOOL isClass = [_initializer isClassMethod];
Class clazz = [_initializer.definition type];
NSArray* typeCodes = [TyphoonIntrospectionUtils typeCodesForSelector:_initializer.selector ofClass:clazz isClassMethod:isClass];

if ([[typeCodes objectAtIndex:_index] isEqualToString:@"@"])
{
[NSException raise:NSInvalidArgumentException
format:@"Unless the type is primitive (int, BOOL, etc), initializer injection requires the required class to be specified. Eg: <argument parameterName=\"string\" value=\"http://dev.foobar.com/service/\" required-class=\"NSString\" />"];
format:@"Unless the type is primitive (int, BOOL, etc), initializer injection requires the required class to be specified. "
"Eg: <argument parameterName=\"string\" value=\"http://dev.foobar.com/service/\" required-class=\"NSString\" />"];
}
return [TyphoonTypeDescriptor descriptorWithTypeCode:[typeCodes objectAtIndex:_index]];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

@interface TyphoonDefinition (InstanceBuilder)

- (void)setType:(Class)type;

- (NSString*)factoryReference;

- (void)setFactoryReference:(NSString*)factoryReference;
Expand Down
11 changes: 9 additions & 2 deletions Source/Definition/Internal/TyphoonDefinition+InstanceBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
////////////////////////////////////////////////////////////////////////////////

#import "TyphoonLinkerCategoryBugFix.h"

TYPHOON_LINK_CATEGORY(TyphoonDefinition_InstanceBuilder)

#import "TyphoonDefinition+InstanceBuilder.h"
Expand All @@ -26,6 +27,12 @@ @implementation TyphoonDefinition (InstanceBuilder)
/* ====================================================================================================================================== */
#pragma mark - Initialization & Destruction

- (void)setType:(Class)type
{
_type = type;
}


- (NSString*)factoryReference
{
return _factoryReference;
Expand All @@ -47,10 +54,10 @@ - (NSSet*)componentsInjectedByValue;
}


- (void)injectProperty:(SEL)selector withReference:(NSString *)reference
- (void)injectProperty:(SEL)selector withReference:(NSString*)reference
{
[_injectedProperties addObject:[[TyphoonPropertyInjectedByReference alloc]
initWithName:NSStringFromSelector(selector) reference:reference]];
initWithName:NSStringFromSelector(selector) reference:reference]];
}

- (NSSet*)propertiesInjectedByValue
Expand Down
55 changes: 27 additions & 28 deletions Source/Factory/Internal/TyphoonComponentFactory+InstanceBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ - (id)newInstanceWithDefinition:(TyphoonDefinition*)definition

if (definition.factoryReference)
{
// misleading - this is not the instance. this is an instance of a separate class that will create the instance of the class we care
// about.
initTarget = [self componentForKey:definition.factoryReference]; // clears currently resolving.
initTarget = [self componentForKey:definition.factoryReference];
[definition setType:[initTarget class]];
}
else if (definition.initializer.isClassMethod)
{
Expand Down Expand Up @@ -200,7 +199,7 @@ - (void)doBeforePropertyInjectionOn:(id <TyphoonIntrospectiveNSObject>)instance
{
if ([instance respondsToSelector:@selector(beforePropertiesSet)])
{
[(id <TyphoonPropertyInjectionDelegate>)instance beforePropertiesSet];
[(id <TyphoonPropertyInjectionDelegate>) instance beforePropertiesSet];
}

if ([instance respondsToSelector:definition.beforePropertyInjection])
Expand All @@ -214,24 +213,25 @@ - (void)doPropertyInjection:(id <TyphoonIntrospectiveNSObject>)instance property
TyphoonTypeDescriptor* propertyType = [instance typeForPropertyWithName:property.name];
AssertTypeDescriptionForPropertyOnInstance(propertyType, property, instance);

TyphoonPropertyInjectionLazyValue lazyValue = ^id {
TyphoonPropertyInjectionLazyValue lazyValue = ^id
{
return [self valueToInjectProperty:property withType:propertyType onInstance:instance];
};

if (![instance respondsToSelector:@selector(shouldInjectProperty:withType:lazyValue:)] ||
[(id <TyphoonPropertyInjectionInternalDelegate>)instance
shouldInjectProperty:property withType:propertyType lazyValue:lazyValue])
[(id <TyphoonPropertyInjectionInternalDelegate>) instance shouldInjectProperty:property withType:propertyType lazyValue:lazyValue])
{
id valueToInject = lazyValue();

if (valueToInject)
{
[(NSObject*)instance injectValue:valueToInject forPropertyName:property.name withType:propertyType];
[(NSObject*) instance injectValue:valueToInject forPropertyName:property.name withType:propertyType];
}
}
}

- (id)valueToInjectProperty:(TyphoonAbstractInjectedProperty*)property withType:(TyphoonTypeDescriptor*)type onInstance:(id <TyphoonIntrospectiveNSObject>)instance
- (id)valueToInjectProperty:(TyphoonAbstractInjectedProperty*)property withType:(TyphoonTypeDescriptor*)type
onInstance:(id <TyphoonIntrospectiveNSObject>)instance
{
id valueToInject = nil;

Expand All @@ -247,7 +247,7 @@ - (id)valueToInjectProperty:(TyphoonAbstractInjectedProperty*)property withType:
}
else if (property.injectionType == TyphoonPropertyInjectionTypeByReference)
{
TyphoonPropertyInjectedByReference* byReference = (TyphoonPropertyInjectedByReference*)property;
TyphoonPropertyInjectedByReference* byReference = (TyphoonPropertyInjectedByReference*) property;
[self evaluateCircularDependency:byReference.reference propertyName:property.name instance:instance];

if (![self propertyIsCircular:property onInstance:instance])
Expand All @@ -257,7 +257,7 @@ - (id)valueToInjectProperty:(TyphoonAbstractInjectedProperty*)property withType:
}
else if (property.injectionType == TyphoonPropertyInjectionTypeByFactoryReference)
{
TyphoonPropertyInjectedByFactoryReference* byReference = (TyphoonPropertyInjectedByFactoryReference*)property;
TyphoonPropertyInjectedByFactoryReference* byReference = (TyphoonPropertyInjectedByFactoryReference*) property;
[self evaluateCircularDependency:byReference.reference propertyName:property.name instance:instance];

if (![self propertyIsCircular:property onInstance:instance])
Expand All @@ -268,23 +268,23 @@ - (id)valueToInjectProperty:(TyphoonAbstractInjectedProperty*)property withType:
}
else if (property.injectionType == TyphoonPropertyInjectionTypeAsCollection)
{
valueToInject = [self buildCollectionFor:(TyphoonPropertyInjectedAsCollection*)property instance:instance];
valueToInject = [self buildCollectionFor:(TyphoonPropertyInjectedAsCollection*) property instance:instance];
}
else if (property.injectionType == TyphoonPropertyInjectionTypeAsObjectInstance)
{
valueToInject = ((TyphoonPropertyInjectedAsObjectInstance*)property).objectInstance;
valueToInject = ((TyphoonPropertyInjectedAsObjectInstance*) property).objectInstance;
}
else if (property.injectionType == TyphoonPropertyInjectionTypeAsStringRepresentation)
{
TyphoonPropertyInjectedWithStringRepresentation* valueProperty = (TyphoonPropertyInjectedWithStringRepresentation*)property;
TyphoonPropertyInjectedWithStringRepresentation* valueProperty = (TyphoonPropertyInjectedWithStringRepresentation*) property;
valueToInject = [self valueFromTextValue:valueProperty.textValue requiredType:type];
}

return valueToInject;
}

- (void)evaluateCircularDependency:(NSString*)componentKey propertyName:(NSString*)propertyName
instance:(id <TyphoonIntrospectiveNSObject>)instance;
instance:(id <TyphoonIntrospectiveNSObject>)instance;
{
if ([self alreadyResolvingKey:componentKey])
{
Expand All @@ -304,13 +304,13 @@ - (void)injectCircularDependenciesOn:(id <TyphoonIntrospectiveNSObject>)instance
NSMutableDictionary* circularDependentProperties = [instance circularDependentProperties];
for (NSString* propertyName in [circularDependentProperties allKeys])
{
id propertyValue = [(NSObject*)instance valueForKey:propertyName];
id propertyValue = [(NSObject*) instance valueForKey:propertyName];
if (!propertyValue)
{
NSString* componentKey = [circularDependentProperties objectForKey:propertyName];
[[_stack peekForKey:componentKey] addInstanceCompleteBlock:^(id reference)
{
[(NSObject*)instance setValue:reference forKey:propertyName];
[(NSObject*) instance setValue:reference forKey:propertyName];
}];

}
Expand All @@ -321,7 +321,7 @@ - (void)doAfterPropertyInjectionOn:(id <TyphoonIntrospectiveNSObject>)instance w
{
if ([instance respondsToSelector:@selector(afterPropertiesSet)])
{
[(id <TyphoonPropertyInjectionDelegate>)instance afterPropertiesSet];
[(id <TyphoonPropertyInjectionDelegate>) instance afterPropertiesSet];
}

if ([instance respondsToSelector:definition.afterPropertyInjection])
Expand All @@ -344,7 +344,7 @@ - (NSInvocation*)invocationToInit:(id)instanceOrClass withInitializer:(TyphoonIn
{
if (parameter.type == TyphoonParameterInjectionTypeReference)
{
TyphoonParameterInjectedByReference* byReference = (TyphoonParameterInjectedByReference*)parameter;
TyphoonParameterInjectedByReference* byReference = (TyphoonParameterInjectedByReference*) parameter;

if ([[_stack peekForKey:byReference.reference] isInitializingInstance])
{
Expand All @@ -356,13 +356,12 @@ - (NSInvocation*)invocationToInit:(id)instanceOrClass withInitializer:(TyphoonIn
}
else if (parameter.type == TyphoonParameterInjectionTypeStringRepresentation)
{
TyphoonParameterInjectedWithStringRepresentation* byValue = (TyphoonParameterInjectedWithStringRepresentation*)parameter;
[self setArgumentFor:invocation index:byValue.index + 2 textValue:byValue.textValue
requiredType:[byValue resolveTypeWith:instanceOrClass]];
TyphoonParameterInjectedWithStringRepresentation* byString = (TyphoonParameterInjectedWithStringRepresentation*) parameter;
[self setArgumentFor:invocation index:byString.index + 2 textValue:byString.textValue requiredType:[byString resolveType]];
}
else if (parameter.type == TyphoonParameterInjectionTypeObjectInstance)
{
TyphoonParameterInjectedWithObjectInstance* byInstance = (TyphoonParameterInjectedWithObjectInstance*)parameter;
TyphoonParameterInjectedWithObjectInstance* byInstance = (TyphoonParameterInjectedWithObjectInstance*) parameter;
id value = byInstance.value;
BOOL isValuesIsWrapper = [value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSValue class]];

Expand All @@ -377,7 +376,7 @@ - (NSInvocation*)invocationToInit:(id)instanceOrClass withInitializer:(TyphoonIn
}
else if (parameter.type == TyphoonParameterInjectionTypeAsCollection)
{
TyphoonParameterInjectedAsCollection* asCollection = (TyphoonParameterInjectedAsCollection*)parameter;
TyphoonParameterInjectedAsCollection* asCollection = (TyphoonParameterInjectedAsCollection*) parameter;
id collection = [self buildCollectionWithValues:asCollection.values requiredType:asCollection.collectionType];
[invocation setArgument:&collection atIndex:parameter.index + 2];
}
Expand Down Expand Up @@ -414,7 +413,7 @@ - (void)setPrimitiveArgumentForInvocation:(NSInvocation*)invocation index:(NSUIn
}

- (void)setArgumentFor:(NSInvocation*)invocation index:(NSUInteger)index1 textValue:(NSString*)textValue
requiredType:(TyphoonTypeDescriptor*)requiredType
requiredType:(TyphoonTypeDescriptor*)requiredType
{
if (requiredType.isPrimitive)
{
Expand All @@ -430,7 +429,7 @@ - (void)setArgumentFor:(NSInvocation*)invocation index:(NSUInteger)index1 textVa
}

- (id)buildCollectionFor:(TyphoonPropertyInjectedAsCollection*)propertyInjectedAsCollection
instance:(id <TyphoonIntrospectiveNSObject>)instance
instance:(id <TyphoonIntrospectiveNSObject>)instance
{
TyphoonCollectionType type = [propertyInjectedAsCollection resolveCollectionTypeWith:instance];
return [self buildCollectionWithValues:[propertyInjectedAsCollection values] requiredType:type];
Expand All @@ -444,13 +443,13 @@ - (id)buildCollectionWithValues:(NSArray*)values requiredType:(TyphoonCollection
{
if (value.type == TyphoonCollectionValueTypeByReference)
{
TyphoonByReferenceCollectionValue* byReferenceValue = (TyphoonByReferenceCollectionValue*)value;
TyphoonByReferenceCollectionValue* byReferenceValue = (TyphoonByReferenceCollectionValue*) value;
id reference = [self componentForKey:byReferenceValue.componentName];
[collection addObject:reference];
}
else if (value.type == TyphoonCollectionValueTypeConvertedText)
{
TyphoonTypeConvertedCollectionValue* typeConvertedValue = (TyphoonTypeConvertedCollectionValue*)value;
TyphoonTypeConvertedCollectionValue* typeConvertedValue = (TyphoonTypeConvertedCollectionValue*) value;
TyphoonTypeDescriptor* descriptor = [TyphoonTypeDescriptor descriptorWithClassOrProtocol:typeConvertedValue.requiredType];
id <TyphoonTypeConverter> converter = [[TyphoonTypeConverterRegistry shared] converterFor:descriptor];
id converted = [converter convert:typeConvertedValue.textValue];
Expand Down
4 changes: 2 additions & 2 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = macosx;
USER_HEADER_SEARCH_PATHS = "\"${PROJECT_DIR}/../\"/** \"${PROJECT_DIR}/Tests/Pods\"/**";
USER_HEADER_SEARCH_PATHS = "\"${PROJECT_DIR}/../\"/** \"${PROJECT_DIR}/Tests/Pods\"/** \"${PROJECT_DIR}/Tests/Pods\"/**";
WRAPPER_EXTENSION = octest;
};
name = Debug;
Expand Down Expand Up @@ -1880,7 +1880,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = macosx;
USER_HEADER_SEARCH_PATHS = "\"${PROJECT_DIR}/../\"/** \"${PROJECT_DIR}/Tests/Pods\"/**";
USER_HEADER_SEARCH_PATHS = "\"${PROJECT_DIR}/../\"/** \"${PROJECT_DIR}/Tests/Pods\"/** \"${PROJECT_DIR}/Tests/Pods\"/**";
WRAPPER_EXTENSION = octest;
};
name = Release;
Expand Down

0 comments on commit 67fd2fc

Please sign in to comment.