Skip to content

Commit

Permalink
Moved circular init exception into TyphoonStackElement instance getter (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarbarev committed Feb 4, 2014
1 parent 2d1dec7 commit 56e3100
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "TyphoonTypeConverterRegistry.h"
#import "TyphoonPrimitiveTypeConverter.h"
#import "TyphoonTypeDescriptor.h"
#import "TyphoonStackElement.h"
#import "NSValue+TCFInstanceBuilder.h"

TYPHOON_LINK_CATEGORY(TyphoonInitializer_InstanceBuilder)
Expand Down Expand Up @@ -124,7 +125,7 @@ - (void)configureInvocation:(NSInvocation*)invocation withFactory:(TyphoonCompon
if (parameter.type == TyphoonParameterInjectionTypeReference)
{
TyphoonParameterInjectedByReference* byReference = (TyphoonParameterInjectedByReference*) parameter;
[[factory stack] peekInstanceForKey:byReference.reference]; //Raises circular dependencies exception if already initializing.
[[[factory stack] peekForKey:byReference.reference] instance]; //Raises circular dependencies exception if already initializing.
id reference = [factory componentForKey:byReference.reference];
[invocation setArgument:&reference atIndex:parameter.index + 2];
}
Expand Down
5 changes: 0 additions & 5 deletions Source/Factory/Internal/TyphoonCallStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@

- (TyphoonStackElement*)peekForKey:(NSString*)key;

/**
* Peeks instance for the given key. If the key represents an instance undergoing initializer injection, raises a circular init exception.
*/
- (id)peekInstanceForKey:(NSString*)key;

- (BOOL)isResolvingKey:(NSString*)key;

- (BOOL)isEmpty;
Expand Down
15 changes: 0 additions & 15 deletions Source/Factory/Internal/TyphoonCallStack.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ - (TyphoonStackElement*)peekForKey:(NSString*)key
return nil;
}

- (id)peekInstanceForKey:(NSString*)key
{
TyphoonStackElement* stackElement = [self peekForKey:key];

if ([stackElement isInitializingInstance])
{
[NSException raise:@"CircularInitializerDependence"
format:@"The object for key %@ is currently initializing, but was specified as init dependency in another object",
stackElement.key];
}

return stackElement.instance;
}


- (BOOL)isEmpty
{
return ([_storage count] == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ - (void)injectAssemblyOnInstance:(id <TyphoonComponentFactoryAware>)instance

- (id)buildSharedInstanceForDefinition:(TyphoonDefinition*)definition
{
id instance = [_stack peekInstanceForKey:definition.key];
id instance = [_stack peekForKey:definition.key].instance;
if (instance)
{
return instance;
Expand Down
2 changes: 1 addition & 1 deletion Source/Factory/Internal/TyphoonStackElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef void(^TyphoonInstanceCompleteBlock)(id instance);
@interface TyphoonStackElement : NSObject

@property(nonatomic, strong, readonly) NSString* key;
@property(nonatomic, strong, readonly) id instance;
@property(nonatomic, strong, readonly) id instance; /* Raises a circular init exception if instance in initializing state. */

+ (instancetype)itemWithKey:(NSString*)key;

Expand Down
12 changes: 12 additions & 0 deletions Source/Factory/Internal/TyphoonStackElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@implementation TyphoonStackElement
{
NSMutableSet* completeBlocks;
id _instance;
}

+ (instancetype)itemWithKey:(NSString*)key
Expand All @@ -41,6 +42,17 @@ - (BOOL)isInitializingInstance
return _instance == nil;
}

- (id)instance
{
if ([self isInitializingInstance])
{
[NSException raise:@"CircularInitializerDependence"
format:@"The object for key %@ is currently initializing, but was specified as init dependency in another object",
self.key];
}
return _instance;
}

- (void)addInstanceCompleteBlock:(TyphoonInstanceCompleteBlock)completeBlock
{
NSParameterAssert(completeBlock);
Expand Down

0 comments on commit 56e3100

Please sign in to comment.