Skip to content

fix crash while injecting property that implements a protocol #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Objection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
4B9D310713DF613600C81C45 /* JSObjection.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B9D310213DF613500C81C45 /* JSObjection.m */; };
4B9D311413DF79FE00C81C45 /* JSObjectionUtils.h in Copy Header Files */ = {isa = PBXBuildFile; fileRef = 4B9D30FE13DF5FAC00C81C45 /* JSObjectionUtils.h */; };
4B9D311513DF79FE00C81C45 /* JSObjection.h in Copy Header Files */ = {isa = PBXBuildFile; fileRef = 4B9D310113DF613500C81C45 /* JSObjection.h */; };
4BA620D01C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BA620CF1C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m */; };
4BA620D11C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BA620CF1C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m */; };
4BA6DD1F12AAB3CD00CFFD70 /* JSObjectionInjectorEntry.h in Copy Header Files */ = {isa = PBXBuildFile; fileRef = 4BED512F12AA8BF300CA6B36 /* JSObjectionInjectorEntry.h */; };
4BA6DD2012AAB3CD00CFFD70 /* JSObjectionInjector.h in Copy Header Files */ = {isa = PBXBuildFile; fileRef = 4BED513012AA8BF300CA6B36 /* JSObjectionInjector.h */; };
4BA6DD2112AAB3CD00CFFD70 /* JSObjectionBindingEntry.h in Copy Header Files */ = {isa = PBXBuildFile; fileRef = 4BED513112AA8BF300CA6B36 /* JSObjectionBindingEntry.h */; };
Expand Down Expand Up @@ -325,6 +327,7 @@
4B9D30FE13DF5FAC00C81C45 /* JSObjectionUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSObjectionUtils.h; sourceTree = "<group>"; };
4B9D310113DF613500C81C45 /* JSObjection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSObjection.h; sourceTree = "<group>"; };
4B9D310213DF613500C81C45 /* JSObjection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSObjection.m; sourceTree = "<group>"; };
4BA620CF1C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InjectImplementsProtocolSpecs.m; sourceTree = "<group>"; };
4BA9CFF412AA86E600674F0E /* OCHamcrest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OCHamcrest.framework; path = Vendor/OCHamcrest.framework; sourceTree = "<group>"; };
4BB07D741668679A00D9BA1E /* AddAndRemoveModulesSpecs.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AddAndRemoveModulesSpecs.m; sourceTree = "<group>"; };
4BB428A116D3205C00710F1E /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -551,6 +554,7 @@
4B95819612AE593F00CBF1EB /* ModuleUsageSpecs.m */,
4B4289301567F3EC004F73D3 /* InitializerSpecs.m */,
4BB07D741668679A00D9BA1E /* AddAndRemoveModulesSpecs.m */,
4BA620CF1C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m */,
);
path = Specs;
sourceTree = "<group>";
Expand Down Expand Up @@ -880,6 +884,7 @@
4B42585F13F56D41006BC001 /* InheritanceSpecs.m in Sources */,
4B42586013F56D41006BC001 /* ModuleUsageSpecs.m in Sources */,
4B42586113F56D41006BC001 /* JSObjectionProviderEntry.m in Sources */,
4BA620D01C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m in Sources */,
4B42586213F56D41006BC001 /* JSObjectionEntry.m in Sources */,
4B42586313F56D41006BC001 /* NSObject+Objection.m in Sources */,
4B42586413F56D41006BC001 /* JSObjectionUtils.m in Sources */,
Expand Down Expand Up @@ -912,6 +917,7 @@
4B4258D313F56F45006BC001 /* InheritanceSpecs.m in Sources */,
4B4258D413F56F45006BC001 /* ModuleUsageSpecs.m in Sources */,
4B4258D513F56F45006BC001 /* JSObjectionProviderEntry.m in Sources */,
4BA620D11C06BEAE00DB87B6 /* InjectImplementsProtocolSpecs.m in Sources */,
4B4258D613F56F45006BC001 /* JSObjectionEntry.m in Sources */,
4B4258D713F56F45006BC001 /* NSObject+Objection.m in Sources */,
4B4258D813F56F45006BC001 /* JSObjectionUtils.m in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions Source/JSObjectionInjector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#import <Foundation/Foundation.h>
#import "JSObjectionModule.h"

@protocol JSObjectionInjectorSelectors

@optional
+ (NSSet *)objectionRequires;
+ (NSDictionary *)objectionRequiresNames;

@end

@interface JSObjectionInjector : NSObject

- (instancetype)initWithContext:(NSDictionary *)theGlobalContext;
Expand Down
7 changes: 7 additions & 0 deletions Source/JSObjectionInjectorEntry.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#import <Foundation/Foundation.h>
#import "JSObjectionEntry.h"

@protocol JSObjectionInjectorEntrySelectors

@optional
+ (id)objectionInitializer;

@end

@interface JSObjectionInjectorEntry : JSObjectionEntry

@property (nonatomic, readonly) Class classEntry;
Expand Down
4 changes: 2 additions & 2 deletions Source/JSObjectionInjectorEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ - (id)buildObject:(NSArray *)arguments initializer: (SEL) initializer {
}

- (SEL)initializerForObject {
return NSSelectorFromString([[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionInitializerKey]);
return NSSelectorFromString([[self.classEntry objectionInitializer] objectForKey:JSObjectionInitializerKey]);
}

- (NSArray *)argumentsForObject:(NSArray *)givenArguments {
return givenArguments.count > 0 ? givenArguments : [[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionDefaultArgumentsKey];
return givenArguments.count > 0 ? givenArguments : [[self.classEntry objectionInitializer] objectForKey:JSObjectionDefaultArgumentsKey];
}


Expand Down
14 changes: 9 additions & 5 deletions Source/JSObjectionUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t pr
classOrProtocol = objc_getProtocol([classOrProtocolName UTF8String]);
propertyInfo.type = JSObjectionTypeProtocol;
} else {
if ([classOrProtocolName hasSuffix:@">"]) {
classOrProtocolName = [classOrProtocolName substringToIndex:[classOrProtocolName rangeOfString:@"<"].location];
}

classOrProtocol = NSClassFromString(classOrProtocolName);
propertyInfo.type = JSObjectionTypeClass;
}
Expand All @@ -50,7 +54,7 @@ static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t pr
static NSSet* BuildDependenciesForClass(Class klass, NSSet *requirements) {
Class superClass = class_getSuperclass([klass class]);
if([superClass respondsToSelector:@selector(objectionRequires)]) {
NSSet *parentsRequirements = [superClass performSelector:@selector(objectionRequires)];
NSSet *parentsRequirements = [superClass objectionRequires];
NSMutableSet *dependencies = [NSMutableSet setWithSet:parentsRequirements];
[dependencies unionSet:requirements];
requirements = dependencies;
Expand All @@ -61,7 +65,7 @@ static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t pr
static NSDictionary* BuildNamedDependenciesForClass(Class klass, NSDictionary *namedRequirements) {
Class superClass = class_getSuperclass([klass class]);
if([superClass respondsToSelector:@selector(objectionRequiresNames)]) {
NSDictionary *parentsNamedRequirements = [superClass performSelector:@selector(objectionRequiresNames)];
NSDictionary *parentsNamedRequirements = [superClass objectionRequiresNames];
NSMutableDictionary *namedDependencies = [NSMutableDictionary dictionaryWithDictionary:parentsNamedRequirements];
[namedDependencies addEntriesFromDictionary:namedRequirements];
namedRequirements = namedDependencies;
Expand Down Expand Up @@ -146,7 +150,7 @@ static void _validateObjectReturnedFromInjector(id *theObject, JSObjectionProper

static void InjectDependenciesIntoProperties(JSObjectionInjector *injector, Class klass, id object) {
if ([klass respondsToSelector:@selector(objectionRequires)]) {
NSSet *properties = [klass performSelector:@selector(objectionRequires)];
NSSet *properties = [klass objectionRequires];
NSMutableDictionary *propertiesDictionary = [NSMutableDictionary dictionaryWithCapacity:properties.count];
for (NSString *propertyName in properties) {
JSObjectionPropertyInfo propertyInfo;
Expand All @@ -161,7 +165,7 @@ static void InjectDependenciesIntoProperties(JSObjectionInjector *injector, Clas
}

if ([klass respondsToSelector:@selector(objectionRequiresNames)]) {
NSDictionary *namedProperties = [klass performSelector:@selector(objectionRequiresNames)];
NSDictionary *namedProperties = [klass objectionRequiresNames];
NSMutableDictionary *propertiesDictionary = [NSMutableDictionary dictionaryWithCapacity:namedProperties.count];
for (NSString *namedPropertyKey in [namedProperties allKeys]) {
NSString* propertyName = [namedProperties valueForKey:namedPropertyKey];
Expand All @@ -177,7 +181,7 @@ static void InjectDependenciesIntoProperties(JSObjectionInjector *injector, Clas
}

if ([object respondsToSelector:@selector(awakeFromObjection)]) {
[object performSelector:@selector(awakeFromObjection)];
[object awakeFromObjection];
}
}

Expand Down
37 changes: 37 additions & 0 deletions Specs/InjectImplementsProtocolSpecs.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#import "SpecHelper.h"
#import "Fixtures.h"
#import "ModuleFixtures.h"

@interface NewCarModule : JSObjectionModule
@end

@implementation NewCarModule
-(void)configure {
[self bindClass:[UnregisteredCar class] toClass:[Car class]];
}
@end

@interface CarOwner : NSObject
@property (nonatomic, strong) Car<UnregisteredProtocol> *car;
@end

@implementation CarOwner
objection_requires(@"car")
@end

QuickSpecBegin(InjectImplementsProtocolSpecs)

__block JSObjectionInjector *injector = nil;
__block CarOwner *carDriver = nil;

beforeEach(^{
injector = [JSObjection createInjector:[NewCarModule new]];
carDriver = [CarOwner new];
});

it(@"injects property car that implements UnregisteredProtocol", ^{
[injector injectDependencies:carDriver];
assertThat(carDriver.car, is(instanceOf([UnregisteredCar class])));
});

QuickSpecEnd