Repository: steipete/Aspects Branch: master Commit: 673e51e8ed61 Files: 48 Total size: 228.4 KB Directory structure: gitextract_twtegwrd/ ├── .gitignore ├── .travis.yml ├── Aspects.h ├── Aspects.m ├── Aspects.podspec ├── Aspects.xcodeproj/ │ ├── project.pbxproj │ ├── project.xcworkspace/ │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata/ │ │ └── Aspects.xccheckout │ └── xcshareddata/ │ └── xcschemes/ │ ├── Aspects-Mac.xcscheme │ ├── Aspects-iOS.xcscheme │ └── Aspects-watchOS.xcscheme ├── AspectsDemo/ │ ├── AspectsDemo/ │ │ ├── AspectsAppDelegate.h │ │ ├── AspectsAppDelegate.m │ │ ├── AspectsDemo-Info.plist │ │ ├── AspectsDemo-Prefix.pch │ │ ├── AspectsViewController.h │ │ ├── AspectsViewController.m │ │ ├── AspectsViewController.xib │ │ ├── Images.xcassets/ │ │ │ ├── AppIcon.appiconset/ │ │ │ │ └── Contents.json │ │ │ └── LaunchImage.launchimage/ │ │ │ └── Contents.json │ │ ├── en.lproj/ │ │ │ └── InfoPlist.strings │ │ └── main.m │ ├── AspectsDemo.xcodeproj/ │ │ ├── project.pbxproj │ │ ├── project.xcworkspace/ │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata/ │ │ │ └── AspectsDemo.xccheckout │ │ └── xcshareddata/ │ │ └── xcschemes/ │ │ └── AspectsDemo.xcscheme │ └── AspectsDemoTests/ │ ├── AspectsDemoTests-Info.plist │ ├── AspectsDemoTests.m │ └── en.lproj/ │ └── InfoPlist.strings ├── AspectsDemoOSX/ │ ├── AspectsDemoOSX/ │ │ ├── AspectsAppDelegate.h │ │ ├── AspectsAppDelegate.m │ │ ├── AspectsDemoOSX-Info.plist │ │ ├── AspectsDemoOSX-Prefix.pch │ │ ├── Base.lproj/ │ │ │ └── MainMenu.xib │ │ ├── Images.xcassets/ │ │ │ └── AppIcon.appiconset/ │ │ │ └── Contents.json │ │ ├── en.lproj/ │ │ │ ├── Credits.rtf │ │ │ └── InfoPlist.strings │ │ └── main.m │ ├── AspectsDemoOSX.xcodeproj/ │ │ ├── project.pbxproj │ │ ├── project.xcworkspace/ │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata/ │ │ │ └── AspectsDemoOSX.xccheckout │ │ └── xcshareddata/ │ │ └── xcschemes/ │ │ └── AspectsDemoOSX.xcscheme │ └── AspectsDemoOSXTests/ │ ├── AspectsDemoOSXTests-Info.plist │ └── en.lproj/ │ └── InfoPlist.strings ├── Info-watch.plist ├── Info.plist ├── LICENSE └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control? # # Pods/ .DS_Store xcuserdata ================================================ FILE: .travis.yml ================================================ language: objective-c script: - xcodebuild -project AspectsDemo/AspectsDemo.xcodeproj -scheme AspectsDemo -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO test - xcodebuild -project AspectsDemo/AspectsDemo.xcodeproj -scheme AspectsDemo -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Retina (64-bit),OS=8.1' test - xcodebuild -project AspectsDemoOSX/AspectsDemoOSX.xcodeproj -scheme AspectsDemoOSX test ================================================ FILE: Aspects.h ================================================ // // Aspects.h // Aspects - A delightful, simple library for aspect oriented programming. // // Copyright (c) 2014 Peter Steinberger. Licensed under the MIT license. // #import typedef NS_OPTIONS(NSUInteger, AspectOptions) { AspectPositionAfter = 0, /// Called after the original implementation (default) AspectPositionInstead = 1, /// Will replace the original implementation. AspectPositionBefore = 2, /// Called before the original implementation. AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution. }; /// Opaque Aspect Token that allows to deregister the hook. @protocol AspectToken /// Deregisters an aspect. /// @return YES if deregistration is successful, otherwise NO. - (BOOL)remove; @end /// The AspectInfo protocol is the first parameter of our block syntax. @protocol AspectInfo /// The instance that is currently hooked. - (id)instance; /// The original invocation of the hooked method. - (NSInvocation *)originalInvocation; /// All method arguments, boxed. This is lazily evaluated. - (NSArray *)arguments; @end /** Aspects uses Objective-C message forwarding to hook into messages. This will create some overhead. Don't add aspects to methods that are called a lot. Aspects is meant for view/controller code that is not called a 1000 times per second. Adding aspects returns an opaque token which can be used to deregister again. All calls are thread safe. */ @interface NSObject (Aspects) /// Adds a block of code before/instead/after the current `selector` for a specific class. /// /// @param block Aspects replicates the type signature of the method being hooked. /// The first parameter will be `id`, followed by all parameters of the method. /// These parameters are optional and will be filled to match the block signature. /// You can even use an empty block, or one that simple gets `id`. /// /// @note Hooking static methods is not supported. /// @return A token which allows to later deregister the aspect. + (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; /// Adds a block of code before/instead/after the current `selector` for a specific instance. - (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; @end typedef NS_ENUM(NSUInteger, AspectErrorCode) { AspectErrorSelectorBlacklisted, /// Selectors like release, retain, autorelease are blacklisted. AspectErrorDoesNotRespondToSelector, /// Selector could not be found. AspectErrorSelectorDeallocPosition, /// When hooking dealloc, only AspectPositionBefore is allowed. AspectErrorSelectorAlreadyHookedInClassHierarchy, /// Statically hooking the same method in subclasses is not allowed. AspectErrorFailedToAllocateClassPair, /// The runtime failed creating a class pair. AspectErrorMissingBlockSignature, /// The block misses compile time signature info and can't be called. AspectErrorIncompatibleBlockSignature, /// The block signature does not match the method or is too large. AspectErrorRemoveObjectAlreadyDeallocated = 100 /// (for removing) The object hooked is already deallocated. }; extern NSString *const AspectErrorDomain; ================================================ FILE: Aspects.m ================================================ // // Aspects.m // Aspects - A delightful, simple library for aspect oriented programming. // // Copyright (c) 2014 Peter Steinberger. Licensed under the MIT license. // #import "Aspects.h" #import #import #import #define AspectLog(...) //#define AspectLog(...) do { NSLog(__VA_ARGS__); }while(0) #define AspectLogError(...) do { NSLog(__VA_ARGS__); }while(0) // Block internals. typedef NS_OPTIONS(int, AspectBlockFlags) { AspectBlockFlagsHasCopyDisposeHelpers = (1 << 25), AspectBlockFlagsHasSignature = (1 << 30) }; typedef struct _AspectBlock { __unused Class isa; AspectBlockFlags flags; __unused int reserved; void (__unused *invoke)(struct _AspectBlock *block, ...); struct { unsigned long int reserved; unsigned long int size; // requires AspectBlockFlagsHasCopyDisposeHelpers void (*copy)(void *dst, const void *src); void (*dispose)(const void *); // requires AspectBlockFlagsHasSignature const char *signature; const char *layout; } *descriptor; // imported variables } *AspectBlockRef; @interface AspectInfo : NSObject - (id)initWithInstance:(__unsafe_unretained id)instance invocation:(NSInvocation *)invocation; @property (nonatomic, unsafe_unretained, readonly) id instance; @property (nonatomic, strong, readonly) NSArray *arguments; @property (nonatomic, strong, readonly) NSInvocation *originalInvocation; @end // Tracks a single aspect. @interface AspectIdentifier : NSObject + (instancetype)identifierWithSelector:(SEL)selector object:(id)object options:(AspectOptions)options block:(id)block error:(NSError **)error; - (BOOL)invokeWithInfo:(id)info; @property (nonatomic, assign) SEL selector; @property (nonatomic, strong) id block; @property (nonatomic, strong) NSMethodSignature *blockSignature; @property (nonatomic, weak) id object; @property (nonatomic, assign) AspectOptions options; @end // Tracks all aspects for an object/class. @interface AspectsContainer : NSObject - (void)addAspect:(AspectIdentifier *)aspect withOptions:(AspectOptions)injectPosition; - (BOOL)removeAspect:(id)aspect; - (BOOL)hasAspects; @property (atomic, copy) NSArray *beforeAspects; @property (atomic, copy) NSArray *insteadAspects; @property (atomic, copy) NSArray *afterAspects; @end @interface AspectTracker : NSObject - (id)initWithTrackedClass:(Class)trackedClass; @property (nonatomic, strong) Class trackedClass; @property (nonatomic, readonly) NSString *trackedClassName; @property (nonatomic, strong) NSMutableSet *selectorNames; @property (nonatomic, strong) NSMutableDictionary *selectorNamesToSubclassTrackers; - (void)addSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName; - (void)removeSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName; - (BOOL)subclassHasHookedSelectorName:(NSString *)selectorName; - (NSSet *)subclassTrackersHookingSelectorName:(NSString *)selectorName; @end @interface NSInvocation (Aspects) - (NSArray *)aspects_arguments; @end #define AspectPositionFilter 0x07 #define AspectError(errorCode, errorDescription) do { \ AspectLogError(@"Aspects: %@", errorDescription); \ if (error) { *error = [NSError errorWithDomain:AspectErrorDomain code:errorCode userInfo:@{NSLocalizedDescriptionKey: errorDescription}]; }}while(0) NSString *const AspectErrorDomain = @"AspectErrorDomain"; static NSString *const AspectsSubclassSuffix = @"_Aspects_"; static NSString *const AspectsMessagePrefix = @"aspects_"; @implementation NSObject (Aspects) /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Public Aspects API + (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error { return aspect_add((id)self, selector, options, block, error); } /// @return A token which allows to later deregister the aspect. - (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error { return aspect_add(self, selector, options, block, error); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Private Helper static id aspect_add(id self, SEL selector, AspectOptions options, id block, NSError **error) { NSCParameterAssert(self); NSCParameterAssert(selector); NSCParameterAssert(block); __block AspectIdentifier *identifier = nil; aspect_performLocked(^{ if (aspect_isSelectorAllowedAndTrack(self, selector, options, error)) { AspectsContainer *aspectContainer = aspect_getContainerForObject(self, selector); identifier = [AspectIdentifier identifierWithSelector:selector object:self options:options block:block error:error]; if (identifier) { [aspectContainer addAspect:identifier withOptions:options]; // Modify the class to allow message interception. aspect_prepareClassAndHookSelector(self, selector, error); } } }); return identifier; } static BOOL aspect_remove(AspectIdentifier *aspect, NSError **error) { NSCAssert([aspect isKindOfClass:AspectIdentifier.class], @"Must have correct type."); __block BOOL success = NO; aspect_performLocked(^{ id self = aspect.object; // strongify if (self) { AspectsContainer *aspectContainer = aspect_getContainerForObject(self, aspect.selector); success = [aspectContainer removeAspect:aspect]; aspect_cleanupHookedClassAndSelector(self, aspect.selector); // destroy token aspect.object = nil; aspect.block = nil; aspect.selector = NULL; }else { NSString *errrorDesc = [NSString stringWithFormat:@"Unable to deregister hook. Object already deallocated: %@", aspect]; AspectError(AspectErrorRemoveObjectAlreadyDeallocated, errrorDesc); } }); return success; } static void aspect_performLocked(dispatch_block_t block) { static OSSpinLock aspect_lock = OS_SPINLOCK_INIT; OSSpinLockLock(&aspect_lock); block(); OSSpinLockUnlock(&aspect_lock); } static SEL aspect_aliasForSelector(SEL selector) { NSCParameterAssert(selector); return NSSelectorFromString([AspectsMessagePrefix stringByAppendingFormat:@"_%@", NSStringFromSelector(selector)]); } static NSMethodSignature *aspect_blockMethodSignature(id block, NSError **error) { AspectBlockRef layout = (__bridge void *)block; if (!(layout->flags & AspectBlockFlagsHasSignature)) { NSString *description = [NSString stringWithFormat:@"The block %@ doesn't contain a type signature.", block]; AspectError(AspectErrorMissingBlockSignature, description); return nil; } void *desc = layout->descriptor; desc += 2 * sizeof(unsigned long int); if (layout->flags & AspectBlockFlagsHasCopyDisposeHelpers) { desc += 2 * sizeof(void *); } if (!desc) { NSString *description = [NSString stringWithFormat:@"The block %@ doesn't has a type signature.", block]; AspectError(AspectErrorMissingBlockSignature, description); return nil; } const char *signature = (*(const char **)desc); return [NSMethodSignature signatureWithObjCTypes:signature]; } static BOOL aspect_isCompatibleBlockSignature(NSMethodSignature *blockSignature, id object, SEL selector, NSError **error) { NSCParameterAssert(blockSignature); NSCParameterAssert(object); NSCParameterAssert(selector); BOOL signaturesMatch = YES; NSMethodSignature *methodSignature = [[object class] instanceMethodSignatureForSelector:selector]; if (blockSignature.numberOfArguments > methodSignature.numberOfArguments) { signaturesMatch = NO; }else { if (blockSignature.numberOfArguments > 1) { const char *blockType = [blockSignature getArgumentTypeAtIndex:1]; if (blockType[0] != '@') { signaturesMatch = NO; } } // Argument 0 is self/block, argument 1 is SEL or id. We start comparing at argument 2. // The block can have less arguments than the method, that's ok. if (signaturesMatch) { for (NSUInteger idx = 2; idx < blockSignature.numberOfArguments; idx++) { const char *methodType = [methodSignature getArgumentTypeAtIndex:idx]; const char *blockType = [blockSignature getArgumentTypeAtIndex:idx]; // Only compare parameter, not the optional type data. if (!methodType || !blockType || methodType[0] != blockType[0]) { signaturesMatch = NO; break; } } } } if (!signaturesMatch) { NSString *description = [NSString stringWithFormat:@"Block signature %@ doesn't match %@.", blockSignature, methodSignature]; AspectError(AspectErrorIncompatibleBlockSignature, description); return NO; } return YES; } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Class + Selector Preparation static BOOL aspect_isMsgForwardIMP(IMP impl) { return impl == _objc_msgForward #if !defined(__arm64__) || impl == (IMP)_objc_msgForward_stret #endif ; } static IMP aspect_getMsgForwardIMP(NSObject *self, SEL selector) { IMP msgForwardIMP = _objc_msgForward; #if !defined(__arm64__) // As an ugly internal runtime implementation detail in the 32bit runtime, we need to determine of the method we hook returns a struct or anything larger than id. // https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html // https://github.com/ReactiveCocoa/ReactiveCocoa/issues/783 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf (Section 5.4) Method method = class_getInstanceMethod(self.class, selector); const char *encoding = method_getTypeEncoding(method); BOOL methodReturnsStructValue = encoding[0] == _C_STRUCT_B; if (methodReturnsStructValue) { @try { NSUInteger valueSize = 0; NSGetSizeAndAlignment(encoding, &valueSize, NULL); if (valueSize == 1 || valueSize == 2 || valueSize == 4 || valueSize == 8) { methodReturnsStructValue = NO; } } @catch (__unused NSException *e) {} } if (methodReturnsStructValue) { msgForwardIMP = (IMP)_objc_msgForward_stret; } #endif return msgForwardIMP; } static void aspect_prepareClassAndHookSelector(NSObject *self, SEL selector, NSError **error) { NSCParameterAssert(selector); Class klass = aspect_hookClass(self, error); Method targetMethod = class_getInstanceMethod(klass, selector); IMP targetMethodIMP = method_getImplementation(targetMethod); if (!aspect_isMsgForwardIMP(targetMethodIMP)) { // Make a method alias for the existing method implementation, it not already copied. const char *typeEncoding = method_getTypeEncoding(targetMethod); SEL aliasSelector = aspect_aliasForSelector(selector); if (![klass instancesRespondToSelector:aliasSelector]) { __unused BOOL addedAlias = class_addMethod(klass, aliasSelector, method_getImplementation(targetMethod), typeEncoding); NSCAssert(addedAlias, @"Original implementation for %@ is already copied to %@ on %@", NSStringFromSelector(selector), NSStringFromSelector(aliasSelector), klass); } // We use forwardInvocation to hook in. class_replaceMethod(klass, selector, aspect_getMsgForwardIMP(self, selector), typeEncoding); AspectLog(@"Aspects: Installed hook for -[%@ %@].", klass, NSStringFromSelector(selector)); } } // Will undo the runtime changes made. static void aspect_cleanupHookedClassAndSelector(NSObject *self, SEL selector) { NSCParameterAssert(self); NSCParameterAssert(selector); Class klass = object_getClass(self); BOOL isMetaClass = class_isMetaClass(klass); if (isMetaClass) { klass = (Class)self; } // Check if the method is marked as forwarded and undo that. Method targetMethod = class_getInstanceMethod(klass, selector); IMP targetMethodIMP = method_getImplementation(targetMethod); if (aspect_isMsgForwardIMP(targetMethodIMP)) { // Restore the original method implementation. const char *typeEncoding = method_getTypeEncoding(targetMethod); SEL aliasSelector = aspect_aliasForSelector(selector); Method originalMethod = class_getInstanceMethod(klass, aliasSelector); IMP originalIMP = method_getImplementation(originalMethod); NSCAssert(originalMethod, @"Original implementation for %@ not found %@ on %@", NSStringFromSelector(selector), NSStringFromSelector(aliasSelector), klass); class_replaceMethod(klass, selector, originalIMP, typeEncoding); AspectLog(@"Aspects: Removed hook for -[%@ %@].", klass, NSStringFromSelector(selector)); } // Deregister global tracked selector aspect_deregisterTrackedSelector(self, selector); // Get the aspect container and check if there are any hooks remaining. Clean up if there are not. AspectsContainer *container = aspect_getContainerForObject(self, selector); if (!container.hasAspects) { // Destroy the container aspect_destroyContainerForObject(self, selector); // Figure out how the class was modified to undo the changes. NSString *className = NSStringFromClass(klass); if ([className hasSuffix:AspectsSubclassSuffix]) { Class originalClass = NSClassFromString([className stringByReplacingOccurrencesOfString:AspectsSubclassSuffix withString:@""]); NSCAssert(originalClass != nil, @"Original class must exist"); object_setClass(self, originalClass); AspectLog(@"Aspects: %@ has been restored.", NSStringFromClass(originalClass)); // We can only dispose the class pair if we can ensure that no instances exist using our subclass. // Since we don't globally track this, we can't ensure this - but there's also not much overhead in keeping it around. //objc_disposeClassPair(object.class); }else { // Class is most likely swizzled in place. Undo that. if (isMetaClass) { aspect_undoSwizzleClassInPlace((Class)self); }else if (self.class != klass) { aspect_undoSwizzleClassInPlace(klass); } } } } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Hook Class static Class aspect_hookClass(NSObject *self, NSError **error) { NSCParameterAssert(self); Class statedClass = self.class; Class baseClass = object_getClass(self); NSString *className = NSStringFromClass(baseClass); // Already subclassed if ([className hasSuffix:AspectsSubclassSuffix]) { return baseClass; // We swizzle a class object, not a single object. }else if (class_isMetaClass(baseClass)) { return aspect_swizzleClassInPlace((Class)self); // Probably a KVO'ed class. Swizzle in place. Also swizzle meta classes in place. }else if (statedClass != baseClass) { return aspect_swizzleClassInPlace(baseClass); } // Default case. Create dynamic subclass. const char *subclassName = [className stringByAppendingString:AspectsSubclassSuffix].UTF8String; Class subclass = objc_getClass(subclassName); if (subclass == nil) { subclass = objc_allocateClassPair(baseClass, subclassName, 0); if (subclass == nil) { NSString *errrorDesc = [NSString stringWithFormat:@"objc_allocateClassPair failed to allocate class %s.", subclassName]; AspectError(AspectErrorFailedToAllocateClassPair, errrorDesc); return nil; } aspect_swizzleForwardInvocation(subclass); aspect_hookedGetClass(subclass, statedClass); aspect_hookedGetClass(object_getClass(subclass), statedClass); objc_registerClassPair(subclass); } object_setClass(self, subclass); return subclass; } static NSString *const AspectsForwardInvocationSelectorName = @"__aspects_forwardInvocation:"; static void aspect_swizzleForwardInvocation(Class klass) { NSCParameterAssert(klass); // If there is no method, replace will act like class_addMethod. IMP originalImplementation = class_replaceMethod(klass, @selector(forwardInvocation:), (IMP)__ASPECTS_ARE_BEING_CALLED__, "v@:@"); if (originalImplementation) { class_addMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName), originalImplementation, "v@:@"); } AspectLog(@"Aspects: %@ is now aspect aware.", NSStringFromClass(klass)); } static void aspect_undoSwizzleForwardInvocation(Class klass) { NSCParameterAssert(klass); Method originalMethod = class_getInstanceMethod(klass, NSSelectorFromString(AspectsForwardInvocationSelectorName)); Method objectMethod = class_getInstanceMethod(NSObject.class, @selector(forwardInvocation:)); // There is no class_removeMethod, so the best we can do is to retore the original implementation, or use a dummy. IMP originalImplementation = method_getImplementation(originalMethod ?: objectMethod); class_replaceMethod(klass, @selector(forwardInvocation:), originalImplementation, "v@:@"); AspectLog(@"Aspects: %@ has been restored.", NSStringFromClass(klass)); } static void aspect_hookedGetClass(Class class, Class statedClass) { NSCParameterAssert(class); NSCParameterAssert(statedClass); Method method = class_getInstanceMethod(class, @selector(class)); IMP newIMP = imp_implementationWithBlock(^(id self) { return statedClass; }); class_replaceMethod(class, @selector(class), newIMP, method_getTypeEncoding(method)); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Swizzle Class In Place static void _aspect_modifySwizzledClasses(void (^block)(NSMutableSet *swizzledClasses)) { static NSMutableSet *swizzledClasses; static dispatch_once_t pred; dispatch_once(&pred, ^{ swizzledClasses = [NSMutableSet new]; }); @synchronized(swizzledClasses) { block(swizzledClasses); } } static Class aspect_swizzleClassInPlace(Class klass) { NSCParameterAssert(klass); NSString *className = NSStringFromClass(klass); _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) { if (![swizzledClasses containsObject:className]) { aspect_swizzleForwardInvocation(klass); [swizzledClasses addObject:className]; } }); return klass; } static void aspect_undoSwizzleClassInPlace(Class klass) { NSCParameterAssert(klass); NSString *className = NSStringFromClass(klass); _aspect_modifySwizzledClasses(^(NSMutableSet *swizzledClasses) { if ([swizzledClasses containsObject:className]) { aspect_undoSwizzleForwardInvocation(klass); [swizzledClasses removeObject:className]; } }); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Aspect Invoke Point // This is a macro so we get a cleaner stack trace. #define aspect_invoke(aspects, info) \ for (AspectIdentifier *aspect in aspects) {\ [aspect invokeWithInfo:info];\ if (aspect.options & AspectOptionAutomaticRemoval) { \ aspectsToRemove = [aspectsToRemove?:@[] arrayByAddingObject:aspect]; \ } \ } // This is the swizzled forwardInvocation: method. static void __ASPECTS_ARE_BEING_CALLED__(__unsafe_unretained NSObject *self, SEL selector, NSInvocation *invocation) { NSCParameterAssert(self); NSCParameterAssert(invocation); SEL originalSelector = invocation.selector; SEL aliasSelector = aspect_aliasForSelector(invocation.selector); invocation.selector = aliasSelector; AspectsContainer *objectContainer = objc_getAssociatedObject(self, aliasSelector); AspectsContainer *classContainer = aspect_getContainerForClass(object_getClass(self), aliasSelector); AspectInfo *info = [[AspectInfo alloc] initWithInstance:self invocation:invocation]; NSArray *aspectsToRemove = nil; // Before hooks. aspect_invoke(classContainer.beforeAspects, info); aspect_invoke(objectContainer.beforeAspects, info); // Instead hooks. BOOL respondsToAlias = YES; if (objectContainer.insteadAspects.count || classContainer.insteadAspects.count) { aspect_invoke(classContainer.insteadAspects, info); aspect_invoke(objectContainer.insteadAspects, info); }else { Class klass = object_getClass(invocation.target); do { if ((respondsToAlias = [klass instancesRespondToSelector:aliasSelector])) { [invocation invoke]; break; } }while (!respondsToAlias && (klass = class_getSuperclass(klass))); } // After hooks. aspect_invoke(classContainer.afterAspects, info); aspect_invoke(objectContainer.afterAspects, info); // If no hooks are installed, call original implementation (usually to throw an exception) if (!respondsToAlias) { invocation.selector = originalSelector; SEL originalForwardInvocationSEL = NSSelectorFromString(AspectsForwardInvocationSelectorName); if ([self respondsToSelector:originalForwardInvocationSEL]) { ((void( *)(id, SEL, NSInvocation *))objc_msgSend)(self, originalForwardInvocationSEL, invocation); }else { [self doesNotRecognizeSelector:invocation.selector]; } } // Remove any hooks that are queued for deregistration. [aspectsToRemove makeObjectsPerformSelector:@selector(remove)]; } #undef aspect_invoke /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Aspect Container Management // Loads or creates the aspect container. static AspectsContainer *aspect_getContainerForObject(NSObject *self, SEL selector) { NSCParameterAssert(self); SEL aliasSelector = aspect_aliasForSelector(selector); AspectsContainer *aspectContainer = objc_getAssociatedObject(self, aliasSelector); if (!aspectContainer) { aspectContainer = [AspectsContainer new]; objc_setAssociatedObject(self, aliasSelector, aspectContainer, OBJC_ASSOCIATION_RETAIN); } return aspectContainer; } static AspectsContainer *aspect_getContainerForClass(Class klass, SEL selector) { NSCParameterAssert(klass); AspectsContainer *classContainer = nil; do { classContainer = objc_getAssociatedObject(klass, selector); if (classContainer.hasAspects) break; }while ((klass = class_getSuperclass(klass))); return classContainer; } static void aspect_destroyContainerForObject(id self, SEL selector) { NSCParameterAssert(self); SEL aliasSelector = aspect_aliasForSelector(selector); objc_setAssociatedObject(self, aliasSelector, nil, OBJC_ASSOCIATION_RETAIN); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Selector Blacklist Checking static NSMutableDictionary *aspect_getSwizzledClassesDict() { static NSMutableDictionary *swizzledClassesDict; static dispatch_once_t pred; dispatch_once(&pred, ^{ swizzledClassesDict = [NSMutableDictionary new]; }); return swizzledClassesDict; } static BOOL aspect_isSelectorAllowedAndTrack(NSObject *self, SEL selector, AspectOptions options, NSError **error) { static NSSet *disallowedSelectorList; static dispatch_once_t pred; dispatch_once(&pred, ^{ disallowedSelectorList = [NSSet setWithObjects:@"retain", @"release", @"autorelease", @"forwardInvocation:", nil]; }); // Check against the blacklist. NSString *selectorName = NSStringFromSelector(selector); if ([disallowedSelectorList containsObject:selectorName]) { NSString *errorDescription = [NSString stringWithFormat:@"Selector %@ is blacklisted.", selectorName]; AspectError(AspectErrorSelectorBlacklisted, errorDescription); return NO; } // Additional checks. AspectOptions position = options&AspectPositionFilter; if ([selectorName isEqualToString:@"dealloc"] && position != AspectPositionBefore) { NSString *errorDesc = @"AspectPositionBefore is the only valid position when hooking dealloc."; AspectError(AspectErrorSelectorDeallocPosition, errorDesc); return NO; } if (![self respondsToSelector:selector] && ![self.class instancesRespondToSelector:selector]) { NSString *errorDesc = [NSString stringWithFormat:@"Unable to find selector -[%@ %@].", NSStringFromClass(self.class), selectorName]; AspectError(AspectErrorDoesNotRespondToSelector, errorDesc); return NO; } // Search for the current class and the class hierarchy IF we are modifying a class object if (class_isMetaClass(object_getClass(self))) { Class klass = [self class]; NSMutableDictionary *swizzledClassesDict = aspect_getSwizzledClassesDict(); Class currentClass = [self class]; AspectTracker *tracker = swizzledClassesDict[currentClass]; if ([tracker subclassHasHookedSelectorName:selectorName]) { NSSet *subclassTracker = [tracker subclassTrackersHookingSelectorName:selectorName]; NSSet *subclassNames = [subclassTracker valueForKey:@"trackedClassName"]; NSString *errorDescription = [NSString stringWithFormat:@"Error: %@ already hooked subclasses: %@. A method can only be hooked once per class hierarchy.", selectorName, subclassNames]; AspectError(AspectErrorSelectorAlreadyHookedInClassHierarchy, errorDescription); return NO; } do { tracker = swizzledClassesDict[currentClass]; if ([tracker.selectorNames containsObject:selectorName]) { if (klass == currentClass) { // Already modified and topmost! return YES; } NSString *errorDescription = [NSString stringWithFormat:@"Error: %@ already hooked in %@. A method can only be hooked once per class hierarchy.", selectorName, NSStringFromClass(currentClass)]; AspectError(AspectErrorSelectorAlreadyHookedInClassHierarchy, errorDescription); return NO; } } while ((currentClass = class_getSuperclass(currentClass))); // Add the selector as being modified. currentClass = klass; AspectTracker *subclassTracker = nil; do { tracker = swizzledClassesDict[currentClass]; if (!tracker) { tracker = [[AspectTracker alloc] initWithTrackedClass:currentClass]; swizzledClassesDict[(id)currentClass] = tracker; } if (subclassTracker) { [tracker addSubclassTracker:subclassTracker hookingSelectorName:selectorName]; } else { [tracker.selectorNames addObject:selectorName]; } // All superclasses get marked as having a subclass that is modified. subclassTracker = tracker; }while ((currentClass = class_getSuperclass(currentClass))); } else { return YES; } return YES; } static void aspect_deregisterTrackedSelector(id self, SEL selector) { if (!class_isMetaClass(object_getClass(self))) return; NSMutableDictionary *swizzledClassesDict = aspect_getSwizzledClassesDict(); NSString *selectorName = NSStringFromSelector(selector); Class currentClass = [self class]; AspectTracker *subclassTracker = nil; do { AspectTracker *tracker = swizzledClassesDict[currentClass]; if (subclassTracker) { [tracker removeSubclassTracker:subclassTracker hookingSelectorName:selectorName]; } else { [tracker.selectorNames removeObject:selectorName]; } if (tracker.selectorNames.count == 0 && tracker.selectorNamesToSubclassTrackers) { [swizzledClassesDict removeObjectForKey:currentClass]; } subclassTracker = tracker; }while ((currentClass = class_getSuperclass(currentClass))); } @end @implementation AspectTracker - (id)initWithTrackedClass:(Class)trackedClass { if (self = [super init]) { _trackedClass = trackedClass; _selectorNames = [NSMutableSet new]; _selectorNamesToSubclassTrackers = [NSMutableDictionary new]; } return self; } - (BOOL)subclassHasHookedSelectorName:(NSString *)selectorName { return self.selectorNamesToSubclassTrackers[selectorName] != nil; } - (void)addSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName { NSMutableSet *trackerSet = self.selectorNamesToSubclassTrackers[selectorName]; if (!trackerSet) { trackerSet = [NSMutableSet new]; self.selectorNamesToSubclassTrackers[selectorName] = trackerSet; } [trackerSet addObject:subclassTracker]; } - (void)removeSubclassTracker:(AspectTracker *)subclassTracker hookingSelectorName:(NSString *)selectorName { NSMutableSet *trackerSet = self.selectorNamesToSubclassTrackers[selectorName]; [trackerSet removeObject:subclassTracker]; if (trackerSet.count == 0) { [self.selectorNamesToSubclassTrackers removeObjectForKey:selectorName]; } } - (NSSet *)subclassTrackersHookingSelectorName:(NSString *)selectorName { NSMutableSet *hookingSubclassTrackers = [NSMutableSet new]; for (AspectTracker *tracker in self.selectorNamesToSubclassTrackers[selectorName]) { if ([tracker.selectorNames containsObject:selectorName]) { [hookingSubclassTrackers addObject:tracker]; } [hookingSubclassTrackers unionSet:[tracker subclassTrackersHookingSelectorName:selectorName]]; } return hookingSubclassTrackers; } - (NSString *)trackedClassName { return NSStringFromClass(self.trackedClass); } - (NSString *)description { return [NSString stringWithFormat:@"<%@: %@, trackedClass: %@, selectorNames:%@, subclass selector names: %@>", self.class, self, NSStringFromClass(self.trackedClass), self.selectorNames, self.selectorNamesToSubclassTrackers.allKeys]; } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - NSInvocation (Aspects) @implementation NSInvocation (Aspects) // Thanks to the ReactiveCocoa team for providing a generic solution for this. - (id)aspect_argumentAtIndex:(NSUInteger)index { const char *argType = [self.methodSignature getArgumentTypeAtIndex:index]; // Skip const type qualifier. if (argType[0] == _C_CONST) argType++; #define WRAP_AND_RETURN(type) do { type val = 0; [self getArgument:&val atIndex:(NSInteger)index]; return @(val); } while (0) if (strcmp(argType, @encode(id)) == 0 || strcmp(argType, @encode(Class)) == 0) { __autoreleasing id returnObj; [self getArgument:&returnObj atIndex:(NSInteger)index]; return returnObj; } else if (strcmp(argType, @encode(SEL)) == 0) { SEL selector = 0; [self getArgument:&selector atIndex:(NSInteger)index]; return NSStringFromSelector(selector); } else if (strcmp(argType, @encode(Class)) == 0) { __autoreleasing Class theClass = Nil; [self getArgument:&theClass atIndex:(NSInteger)index]; return theClass; // Using this list will box the number with the appropriate constructor, instead of the generic NSValue. } else if (strcmp(argType, @encode(char)) == 0) { WRAP_AND_RETURN(char); } else if (strcmp(argType, @encode(int)) == 0) { WRAP_AND_RETURN(int); } else if (strcmp(argType, @encode(short)) == 0) { WRAP_AND_RETURN(short); } else if (strcmp(argType, @encode(long)) == 0) { WRAP_AND_RETURN(long); } else if (strcmp(argType, @encode(long long)) == 0) { WRAP_AND_RETURN(long long); } else if (strcmp(argType, @encode(unsigned char)) == 0) { WRAP_AND_RETURN(unsigned char); } else if (strcmp(argType, @encode(unsigned int)) == 0) { WRAP_AND_RETURN(unsigned int); } else if (strcmp(argType, @encode(unsigned short)) == 0) { WRAP_AND_RETURN(unsigned short); } else if (strcmp(argType, @encode(unsigned long)) == 0) { WRAP_AND_RETURN(unsigned long); } else if (strcmp(argType, @encode(unsigned long long)) == 0) { WRAP_AND_RETURN(unsigned long long); } else if (strcmp(argType, @encode(float)) == 0) { WRAP_AND_RETURN(float); } else if (strcmp(argType, @encode(double)) == 0) { WRAP_AND_RETURN(double); } else if (strcmp(argType, @encode(BOOL)) == 0) { WRAP_AND_RETURN(BOOL); } else if (strcmp(argType, @encode(bool)) == 0) { WRAP_AND_RETURN(BOOL); } else if (strcmp(argType, @encode(char *)) == 0) { WRAP_AND_RETURN(const char *); } else if (strcmp(argType, @encode(void (^)(void))) == 0) { __unsafe_unretained id block = nil; [self getArgument:&block atIndex:(NSInteger)index]; return [block copy]; } else { NSUInteger valueSize = 0; NSGetSizeAndAlignment(argType, &valueSize, NULL); unsigned char valueBytes[valueSize]; [self getArgument:valueBytes atIndex:(NSInteger)index]; return [NSValue valueWithBytes:valueBytes objCType:argType]; } return nil; #undef WRAP_AND_RETURN } - (NSArray *)aspects_arguments { NSMutableArray *argumentsArray = [NSMutableArray array]; for (NSUInteger idx = 2; idx < self.methodSignature.numberOfArguments; idx++) { [argumentsArray addObject:[self aspect_argumentAtIndex:idx] ?: NSNull.null]; } return [argumentsArray copy]; } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - AspectIdentifier @implementation AspectIdentifier + (instancetype)identifierWithSelector:(SEL)selector object:(id)object options:(AspectOptions)options block:(id)block error:(NSError **)error { NSCParameterAssert(block); NSCParameterAssert(selector); NSMethodSignature *blockSignature = aspect_blockMethodSignature(block, error); // TODO: check signature compatibility, etc. if (!aspect_isCompatibleBlockSignature(blockSignature, object, selector, error)) { return nil; } AspectIdentifier *identifier = nil; if (blockSignature) { identifier = [AspectIdentifier new]; identifier.selector = selector; identifier.block = block; identifier.blockSignature = blockSignature; identifier.options = options; identifier.object = object; // weak } return identifier; } - (BOOL)invokeWithInfo:(id)info { NSInvocation *blockInvocation = [NSInvocation invocationWithMethodSignature:self.blockSignature]; NSInvocation *originalInvocation = info.originalInvocation; NSUInteger numberOfArguments = self.blockSignature.numberOfArguments; // Be extra paranoid. We already check that on hook registration. if (numberOfArguments > originalInvocation.methodSignature.numberOfArguments) { AspectLogError(@"Block has too many arguments. Not calling %@", info); return NO; } // The `self` of the block will be the AspectInfo. Optional. if (numberOfArguments > 1) { [blockInvocation setArgument:&info atIndex:1]; } void *argBuf = NULL; for (NSUInteger idx = 2; idx < numberOfArguments; idx++) { const char *type = [originalInvocation.methodSignature getArgumentTypeAtIndex:idx]; NSUInteger argSize; NSGetSizeAndAlignment(type, &argSize, NULL); if (!(argBuf = reallocf(argBuf, argSize))) { AspectLogError(@"Failed to allocate memory for block invocation."); return NO; } [originalInvocation getArgument:argBuf atIndex:idx]; [blockInvocation setArgument:argBuf atIndex:idx]; } [blockInvocation invokeWithTarget:self.block]; if (argBuf != NULL) { free(argBuf); } return YES; } - (NSString *)description { return [NSString stringWithFormat:@"<%@: %p, SEL:%@ object:%@ options:%tu block:%@ (#%tu args)>", self.class, self, NSStringFromSelector(self.selector), self.object, self.options, self.block, self.blockSignature.numberOfArguments]; } - (BOOL)remove { return aspect_remove(self, NULL); } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - AspectsContainer @implementation AspectsContainer - (BOOL)hasAspects { return self.beforeAspects.count > 0 || self.insteadAspects.count > 0 || self.afterAspects.count > 0; } - (void)addAspect:(AspectIdentifier *)aspect withOptions:(AspectOptions)options { NSParameterAssert(aspect); NSUInteger position = options&AspectPositionFilter; switch (position) { case AspectPositionBefore: self.beforeAspects = [(self.beforeAspects ?:@[]) arrayByAddingObject:aspect]; break; case AspectPositionInstead: self.insteadAspects = [(self.insteadAspects?:@[]) arrayByAddingObject:aspect]; break; case AspectPositionAfter: self.afterAspects = [(self.afterAspects ?:@[]) arrayByAddingObject:aspect]; break; } } - (BOOL)removeAspect:(id)aspect { for (NSString *aspectArrayName in @[NSStringFromSelector(@selector(beforeAspects)), NSStringFromSelector(@selector(insteadAspects)), NSStringFromSelector(@selector(afterAspects))]) { NSArray *array = [self valueForKey:aspectArrayName]; NSUInteger index = [array indexOfObjectIdenticalTo:aspect]; if (array && index != NSNotFound) { NSMutableArray *newArray = [NSMutableArray arrayWithArray:array]; [newArray removeObjectAtIndex:index]; [self setValue:newArray forKey:aspectArrayName]; return YES; } } return NO; } - (NSString *)description { return [NSString stringWithFormat:@"<%@: %p, before:%@, instead:%@, after:%@>", self.class, self, self.beforeAspects, self.insteadAspects, self.afterAspects]; } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - AspectInfo @implementation AspectInfo @synthesize arguments = _arguments; - (id)initWithInstance:(__unsafe_unretained id)instance invocation:(NSInvocation *)invocation { NSCParameterAssert(instance); NSCParameterAssert(invocation); if (self = [super init]) { _instance = instance; _originalInvocation = invocation; } return self; } - (NSArray *)arguments { // Lazily evaluate arguments, boxing is expensive. if (!_arguments) { _arguments = self.originalInvocation.aspects_arguments; } return _arguments; } @end ================================================ FILE: Aspects.podspec ================================================ Pod::Spec.new do |s| s.name = "Aspects" s.version = "1.4.1" s.summary = "Delightful, simple library for aspect oriented programming." s.homepage = "https://github.com/steipete/Aspects" s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { "Peter Steinberger" => "steipete@gmail.com" } s.ios.deployment_target = '6.0' s.osx.deployment_target = '10.7' s.source = { :git => "https://github.com/steipete/Aspects.git", :tag => "#{s.version}" } s.source_files = 'Aspects.{h,m}' s.requires_arc = true; s.social_media_url = "https://twitter.com/steipete" end ================================================ FILE: Aspects.xcodeproj/project.pbxproj ================================================ // !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 46; objects = { /* Begin PBXBuildFile section */ 34B986851B1DE2BE00DE719D /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B986841B1DE2BE00DE719D /* Aspects.m */; }; 34B986861B1DE2E300DE719D /* Aspects.h in Headers */ = {isa = PBXBuildFile; fileRef = 34B9866C1B1DE0CE00DE719D /* Aspects.h */; settings = {ATTRIBUTES = (Public, ); }; }; 34B986A51B1E491000DE719D /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B986841B1DE2BE00DE719D /* Aspects.m */; }; 34B986A61B1E491300DE719D /* Aspects.h in Headers */ = {isa = PBXBuildFile; fileRef = 34B9866C1B1DE0CE00DE719D /* Aspects.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7376F8CD1B8EA767009CAB74 /* Aspects.h in Headers */ = {isa = PBXBuildFile; fileRef = 34B9866C1B1DE0CE00DE719D /* Aspects.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7376F8D61B8EB1FA009CAB74 /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B986841B1DE2BE00DE719D /* Aspects.m */; settings = {ASSET_TAGS = (); }; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 34B986671B1DE0CE00DE719D /* Aspects.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Aspects.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 34B9866B1B1DE0CE00DE719D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34B9866C1B1DE0CE00DE719D /* Aspects.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Aspects.h; sourceTree = ""; }; 34B986841B1DE2BE00DE719D /* Aspects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Aspects.m; sourceTree = ""; }; 34B9868C1B1E48CD00DE719D /* Aspects.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Aspects.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7376F8C31B8EA670009CAB74 /* Aspects.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Aspects.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7376F8C71B8EA670009CAB74 /* Info-watch.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-watch.plist"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 34B986631B1DE0CE00DE719D /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; 34B986881B1E48CD00DE719D /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; 7376F8BF1B8EA670009CAB74 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 34B9865D1B1DE0CE00DE719D = { isa = PBXGroup; children = ( 34B986691B1DE0CE00DE719D /* Aspects */, 34B986681B1DE0CE00DE719D /* Products */, ); sourceTree = ""; }; 34B986681B1DE0CE00DE719D /* Products */ = { isa = PBXGroup; children = ( 34B986671B1DE0CE00DE719D /* Aspects.framework */, 34B9868C1B1E48CD00DE719D /* Aspects.framework */, 7376F8C31B8EA670009CAB74 /* Aspects.framework */, ); name = Products; sourceTree = ""; }; 34B986691B1DE0CE00DE719D /* Aspects */ = { isa = PBXGroup; children = ( 34B9866C1B1DE0CE00DE719D /* Aspects.h */, 34B986841B1DE2BE00DE719D /* Aspects.m */, 34B9866A1B1DE0CE00DE719D /* Supporting Files */, ); name = Aspects; sourceTree = ""; }; 34B9866A1B1DE0CE00DE719D /* Supporting Files */ = { isa = PBXGroup; children = ( 34B9866B1B1DE0CE00DE719D /* Info.plist */, 7376F8C71B8EA670009CAB74 /* Info-watch.plist */, ); name = "Supporting Files"; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ 34B986641B1DE0CE00DE719D /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( 34B986861B1DE2E300DE719D /* Aspects.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; 34B986891B1E48CD00DE719D /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( 34B986A61B1E491300DE719D /* Aspects.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; 7376F8C01B8EA670009CAB74 /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( 7376F8CD1B8EA767009CAB74 /* Aspects.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ 34B986661B1DE0CE00DE719D /* Aspects-iOS */ = { isa = PBXNativeTarget; buildConfigurationList = 34B9867D1B1DE0CE00DE719D /* Build configuration list for PBXNativeTarget "Aspects-iOS" */; buildPhases = ( 34B986621B1DE0CE00DE719D /* Sources */, 34B986631B1DE0CE00DE719D /* Frameworks */, 34B986641B1DE0CE00DE719D /* Headers */, 34B986651B1DE0CE00DE719D /* Resources */, ); buildRules = ( ); dependencies = ( ); name = "Aspects-iOS"; productName = Aspects; productReference = 34B986671B1DE0CE00DE719D /* Aspects.framework */; productType = "com.apple.product-type.framework"; }; 34B9868B1B1E48CD00DE719D /* Aspects-Mac */ = { isa = PBXNativeTarget; buildConfigurationList = 34B9869F1B1E48CE00DE719D /* Build configuration list for PBXNativeTarget "Aspects-Mac" */; buildPhases = ( 34B986871B1E48CD00DE719D /* Sources */, 34B986881B1E48CD00DE719D /* Frameworks */, 34B986891B1E48CD00DE719D /* Headers */, 34B9868A1B1E48CD00DE719D /* Resources */, ); buildRules = ( ); dependencies = ( ); name = "Aspects-Mac"; productName = Aspects; productReference = 34B9868C1B1E48CD00DE719D /* Aspects.framework */; productType = "com.apple.product-type.framework"; }; 7376F8C21B8EA670009CAB74 /* Aspects-watchOS */ = { isa = PBXNativeTarget; buildConfigurationList = 7376F8CA1B8EA670009CAB74 /* Build configuration list for PBXNativeTarget "Aspects-watchOS" */; buildPhases = ( 7376F8BE1B8EA670009CAB74 /* Sources */, 7376F8BF1B8EA670009CAB74 /* Frameworks */, 7376F8C01B8EA670009CAB74 /* Headers */, 7376F8C11B8EA670009CAB74 /* Resources */, ); buildRules = ( ); dependencies = ( ); name = "Aspects-watchOS"; productName = "Aspects-watchOS"; productReference = 7376F8C31B8EA670009CAB74 /* Aspects.framework */; productType = "com.apple.product-type.framework"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 34B9865E1B1DE0CE00DE719D /* Project object */ = { isa = PBXProject; attributes = { LastUpgradeCheck = 0630; ORGANIZATIONNAME = "Peter Steinberger"; TargetAttributes = { 34B986661B1DE0CE00DE719D = { CreatedOnToolsVersion = 6.3.1; }; 34B9868B1B1E48CD00DE719D = { CreatedOnToolsVersion = 6.3.1; }; 7376F8C21B8EA670009CAB74 = { CreatedOnToolsVersion = 7.0; }; }; }; buildConfigurationList = 34B986611B1DE0CE00DE719D /* Build configuration list for PBXProject "Aspects" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, ); mainGroup = 34B9865D1B1DE0CE00DE719D; productRefGroup = 34B986681B1DE0CE00DE719D /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( 34B986661B1DE0CE00DE719D /* Aspects-iOS */, 34B9868B1B1E48CD00DE719D /* Aspects-Mac */, 7376F8C21B8EA670009CAB74 /* Aspects-watchOS */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ 34B986651B1DE0CE00DE719D /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; 34B9868A1B1E48CD00DE719D /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; 7376F8C11B8EA670009CAB74 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ 34B986621B1DE0CE00DE719D /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 34B986851B1DE2BE00DE719D /* Aspects.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 34B986871B1E48CD00DE719D /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 34B986A51B1E491000DE719D /* Aspects.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 7376F8BE1B8EA670009CAB74 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 7376F8D61B8EB1FA009CAB74 /* Aspects.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ 34B9867B1B1DE0CE00DE719D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Debug; }; 34B9867C1B1DE0CE00DE719D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; name = Release; }; 34B9867E1B1DE0CE00DE719D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "$(SRCROOT)/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = Aspects; SKIP_INSTALL = YES; }; name = Debug; }; 34B9867F1B1DE0CE00DE719D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "$(SRCROOT)/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = Aspects; SKIP_INSTALL = YES; }; name = Release; }; 34B986A01B1E48CE00DE719D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { COMBINE_HIDPI_IMAGES = YES; DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_VERSION = A; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); INFOPLIST_FILE = "$(SRCROOT)/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.7; PRODUCT_NAME = Aspects; SDKROOT = macosx; SKIP_INSTALL = YES; }; name = Debug; }; 34B986A11B1E48CE00DE719D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; FRAMEWORK_VERSION = A; INFOPLIST_FILE = "$(SRCROOT)/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.7; PRODUCT_NAME = Aspects; SDKROOT = macosx; SKIP_INSTALL = YES; }; name = Release; }; 7376F8C81B8EA670009CAB74 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; ENABLE_TESTABILITY = YES; INFOPLIST_FILE = "Info-watch.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.pspdfkit.Aspects-watchOS"; PRODUCT_NAME = Aspects; SDKROOT = watchos; SKIP_INSTALL = YES; TARGETED_DEVICE_FAMILY = 4; WATCHOS_DEPLOYMENT_TARGET = 2.0; }; name = Debug; }; 7376F8C91B8EA670009CAB74 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "Info-watch.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.pspdfkit.Aspects-watchOS"; PRODUCT_NAME = Aspects; SDKROOT = watchos; SKIP_INSTALL = YES; TARGETED_DEVICE_FAMILY = 4; WATCHOS_DEPLOYMENT_TARGET = 2.0; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ 34B986611B1DE0CE00DE719D /* Build configuration list for PBXProject "Aspects" */ = { isa = XCConfigurationList; buildConfigurations = ( 34B9867B1B1DE0CE00DE719D /* Debug */, 34B9867C1B1DE0CE00DE719D /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 34B9867D1B1DE0CE00DE719D /* Build configuration list for PBXNativeTarget "Aspects-iOS" */ = { isa = XCConfigurationList; buildConfigurations = ( 34B9867E1B1DE0CE00DE719D /* Debug */, 34B9867F1B1DE0CE00DE719D /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 34B9869F1B1E48CE00DE719D /* Build configuration list for PBXNativeTarget "Aspects-Mac" */ = { isa = XCConfigurationList; buildConfigurations = ( 34B986A01B1E48CE00DE719D /* Debug */, 34B986A11B1E48CE00DE719D /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 7376F8CA1B8EA670009CAB74 /* Build configuration list for PBXNativeTarget "Aspects-watchOS" */ = { isa = XCConfigurationList; buildConfigurations = ( 7376F8C81B8EA670009CAB74 /* Debug */, 7376F8C91B8EA670009CAB74 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; rootObject = 34B9865E1B1DE0CE00DE719D /* Project object */; } ================================================ FILE: Aspects.xcodeproj/project.xcworkspace/contents.xcworkspacedata ================================================ ================================================ FILE: Aspects.xcodeproj/project.xcworkspace/xcshareddata/Aspects.xccheckout ================================================ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier 4228F3E1-0517-43F3-919E-B521720D6F24 IDESourceControlProjectName Aspects IDESourceControlProjectOriginsDictionary 8B3366FBF9699A8CCB764F44E854D1F695C54A83 https://github.com/steipete/Aspects.git IDESourceControlProjectPath Aspects.xcodeproj IDESourceControlProjectRelativeInstallPathDictionary 8B3366FBF9699A8CCB764F44E854D1F695C54A83 ../.. IDESourceControlProjectURL https://github.com/steipete/Aspects.git IDESourceControlProjectVersion 111 IDESourceControlProjectWCCIdentifier 8B3366FBF9699A8CCB764F44E854D1F695C54A83 IDESourceControlProjectWCConfigurations IDESourceControlRepositoryExtensionIdentifierKey public.vcs.git IDESourceControlWCCIdentifierKey 8B3366FBF9699A8CCB764F44E854D1F695C54A83 IDESourceControlWCCName Aspects ================================================ FILE: Aspects.xcodeproj/xcshareddata/xcschemes/Aspects-Mac.xcscheme ================================================ ================================================ FILE: Aspects.xcodeproj/xcshareddata/xcschemes/Aspects-iOS.xcscheme ================================================ ================================================ FILE: Aspects.xcodeproj/xcshareddata/xcschemes/Aspects-watchOS.xcscheme ================================================ ================================================ FILE: AspectsDemo/AspectsDemo/AspectsAppDelegate.h ================================================ // // AspectsAppDelegate.h // AspectsDemo // // Created by Peter Steinberger on 03/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // @import UIKit; @interface AspectsAppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end ================================================ FILE: AspectsDemo/AspectsDemo/AspectsAppDelegate.m ================================================ // // AspectsAppDelegate.m // AspectsDemo // // Created by Peter Steinberger on 03/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import "AspectsAppDelegate.h" #import "AspectsViewController.h" #import "Aspects.h" @implementation AspectsAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { AspectsViewController *aspectsController = [AspectsViewController new]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:aspectsController]; [self.window makeKeyAndVisible]; // Ignore hooks when we are testing. if (!NSClassFromString(@"XCTestCase")) { [aspectsController aspect_hookSelector:@selector(buttonPressed:) withOptions:0 usingBlock:^(id info, id sender) { NSLog(@"Button was pressed by: %@", sender); } error:NULL]; [aspectsController aspect_hookSelector:@selector(viewWillLayoutSubviews) withOptions:0 usingBlock:^{ NSLog(@"Controller is layouting!"); } error:NULL]; } return YES; } @end ================================================ FILE: AspectsDemo/AspectsDemo/AspectsDemo-Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleDisplayName ${PRODUCT_NAME} CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier com.pspdfkit.aspects.${PRODUCT_NAME:rfc1034identifier} CFBundleInfoDictionaryVersion 6.0 CFBundleName ${PRODUCT_NAME} CFBundlePackageType APPL CFBundleShortVersionString 1.0 CFBundleSignature ???? CFBundleVersion 1.0 LSRequiresIPhoneOS UIRequiredDeviceCapabilities armv7 UISupportedInterfaceOrientations UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UISupportedInterfaceOrientations~ipad UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight ================================================ FILE: AspectsDemo/AspectsDemo/AspectsDemo-Prefix.pch ================================================ // // Prefix header // // The contents of this file are implicitly included at the beginning of every source file. // #import #ifndef __IPHONE_3_0 #warning "This project uses features only available in iOS SDK 3.0 and later." #endif #ifdef __OBJC__ #import #import #endif ================================================ FILE: AspectsDemo/AspectsDemo/AspectsViewController.h ================================================ // // AspectsViewController.h // AspectsDemo // // Created by Peter Steinberger on 05/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import @interface AspectsViewController : UIViewController - (IBAction)buttonPressed:(id)sender; @end ================================================ FILE: AspectsDemo/AspectsDemo/AspectsViewController.m ================================================ // // AspectsViewController.m // AspectsDemo // // Created by Peter Steinberger on 05/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import "AspectsViewController.h" #import "Aspects.h" @implementation AspectsViewController - (IBAction)buttonPressed:(id)sender { UIViewController *testController = [[UIImagePickerController alloc] init]; testController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentViewController:testController animated:YES completion:NULL]; // We are interested in being notified when the controller is being dismissed. [testController aspect_hookSelector:@selector(viewWillDisappear:) withOptions:0 usingBlock:^(id info, BOOL animated) { UIViewController *controller = [info instance]; if (controller.isBeingDismissed || controller.isMovingFromParentViewController) { [[[UIAlertView alloc] initWithTitle:@"Popped" message:@"Hello from Aspects" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show]; } } error:NULL]; // Hooking dealloc is delicate, only AspectPositionBefore will work here. [testController aspect_hookSelector:NSSelectorFromString(@"dealloc") withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"Controller is about to be deallocated: %@", [info instance]); } error:NULL]; } @end ================================================ FILE: AspectsDemo/AspectsDemo/AspectsViewController.xib ================================================ ================================================ FILE: AspectsDemo/AspectsDemo/Images.xcassets/AppIcon.appiconset/Contents.json ================================================ { "images" : [ { "idiom" : "iphone", "size" : "29x29", "scale" : "2x" }, { "idiom" : "iphone", "size" : "40x40", "scale" : "2x" }, { "idiom" : "iphone", "size" : "60x60", "scale" : "2x" }, { "idiom" : "ipad", "size" : "29x29", "scale" : "1x" }, { "idiom" : "ipad", "size" : "29x29", "scale" : "2x" }, { "idiom" : "ipad", "size" : "40x40", "scale" : "1x" }, { "idiom" : "ipad", "size" : "40x40", "scale" : "2x" }, { "idiom" : "ipad", "size" : "76x76", "scale" : "1x" }, { "idiom" : "ipad", "size" : "76x76", "scale" : "2x" } ], "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: AspectsDemo/AspectsDemo/Images.xcassets/LaunchImage.launchimage/Contents.json ================================================ { "images" : [ { "orientation" : "portrait", "idiom" : "iphone", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "2x" }, { "orientation" : "portrait", "idiom" : "iphone", "subtype" : "retina4", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "2x" }, { "orientation" : "portrait", "idiom" : "ipad", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "1x" }, { "orientation" : "landscape", "idiom" : "ipad", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "1x" }, { "orientation" : "portrait", "idiom" : "ipad", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "2x" }, { "orientation" : "landscape", "idiom" : "ipad", "extent" : "full-screen", "minimum-system-version" : "7.0", "scale" : "2x" } ], "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: AspectsDemo/AspectsDemo/en.lproj/InfoPlist.strings ================================================ /* Localized versions of Info.plist keys */ ================================================ FILE: AspectsDemo/AspectsDemo/main.m ================================================ // // main.m // AspectsDemo // // Created by Peter Steinberger on 03/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import #import "AspectsAppDelegate.h" int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AspectsAppDelegate class])); } } ================================================ FILE: AspectsDemo/AspectsDemo.xcodeproj/project.pbxproj ================================================ // !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 46; objects = { /* Begin PBXBuildFile section */ 78573EF519155A2E000D3B00 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF419155A2E000D3B00 /* Foundation.framework */; }; 78573EF719155A2E000D3B00 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF619155A2E000D3B00 /* CoreGraphics.framework */; }; 78573EF919155A2E000D3B00 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF819155A2E000D3B00 /* UIKit.framework */; }; 78573EFF19155A2E000D3B00 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 78573EFD19155A2E000D3B00 /* InfoPlist.strings */; }; 78573F0119155A2E000D3B00 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F0019155A2E000D3B00 /* main.m */; }; 78573F0519155A2E000D3B00 /* AspectsAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F0419155A2E000D3B00 /* AspectsAppDelegate.m */; }; 78573F0719155A2E000D3B00 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 78573F0619155A2E000D3B00 /* Images.xcassets */; }; 78573F0E19155A2E000D3B00 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573F0D19155A2E000D3B00 /* XCTest.framework */; }; 78573F0F19155A2E000D3B00 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF419155A2E000D3B00 /* Foundation.framework */; }; 78573F1019155A2E000D3B00 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78573EF819155A2E000D3B00 /* UIKit.framework */; }; 78573F1819155A2E000D3B00 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 78573F1619155A2E000D3B00 /* InfoPlist.strings */; }; 78573F1A19155A2E000D3B00 /* AspectsDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F1919155A2E000D3B00 /* AspectsDemoTests.m */; }; 78573F2519155A74000D3B00 /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 78573F2319155A74000D3B00 /* Aspects.m */; }; 78D7D77119177C8E002EB314 /* AspectsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 78D7D76F19177C8E002EB314 /* AspectsViewController.m */; }; 78D7D77219177C8E002EB314 /* AspectsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 78D7D77019177C8E002EB314 /* AspectsViewController.xib */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ 78573F1119155A2E000D3B00 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 78573EE919155A2E000D3B00 /* Project object */; proxyType = 1; remoteGlobalIDString = 78573EF019155A2E000D3B00; remoteInfo = AspectsDemo; }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ 78573EF119155A2E000D3B00 /* AspectsDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AspectsDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 78573EF419155A2E000D3B00 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 78573EF619155A2E000D3B00 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 78573EF819155A2E000D3B00 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 78573EFC19155A2E000D3B00 /* AspectsDemo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "AspectsDemo-Info.plist"; sourceTree = ""; }; 78573EFE19155A2E000D3B00 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 78573F0019155A2E000D3B00 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 78573F0219155A2E000D3B00 /* AspectsDemo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AspectsDemo-Prefix.pch"; sourceTree = ""; }; 78573F0319155A2E000D3B00 /* AspectsAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AspectsAppDelegate.h; sourceTree = ""; }; 78573F0419155A2E000D3B00 /* AspectsAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AspectsAppDelegate.m; sourceTree = ""; }; 78573F0619155A2E000D3B00 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 78573F0C19155A2E000D3B00 /* AspectsDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AspectsDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 78573F0D19155A2E000D3B00 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 78573F1519155A2E000D3B00 /* AspectsDemoTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "AspectsDemoTests-Info.plist"; sourceTree = ""; }; 78573F1719155A2E000D3B00 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 78573F1919155A2E000D3B00 /* AspectsDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AspectsDemoTests.m; sourceTree = ""; }; 78573F2319155A74000D3B00 /* Aspects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Aspects.m; path = ../../Aspects.m; sourceTree = ""; }; 78573F2419155A74000D3B00 /* Aspects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Aspects.h; path = ../../Aspects.h; sourceTree = ""; }; 78D7D76E19177C8E002EB314 /* AspectsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AspectsViewController.h; sourceTree = ""; }; 78D7D76F19177C8E002EB314 /* AspectsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AspectsViewController.m; sourceTree = ""; }; 78D7D77019177C8E002EB314 /* AspectsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = AspectsViewController.xib; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 78573EEE19155A2E000D3B00 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 78573EF719155A2E000D3B00 /* CoreGraphics.framework in Frameworks */, 78573EF919155A2E000D3B00 /* UIKit.framework in Frameworks */, 78573EF519155A2E000D3B00 /* Foundation.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; 78573F0919155A2E000D3B00 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 78573F0E19155A2E000D3B00 /* XCTest.framework in Frameworks */, 78573F1019155A2E000D3B00 /* UIKit.framework in Frameworks */, 78573F0F19155A2E000D3B00 /* Foundation.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 78573EE819155A2E000D3B00 = { isa = PBXGroup; children = ( 78573EFA19155A2E000D3B00 /* AspectsDemo */, 78573F1319155A2E000D3B00 /* AspectsDemoTests */, 78573EF319155A2E000D3B00 /* Frameworks */, 78573EF219155A2E000D3B00 /* Products */, ); sourceTree = ""; }; 78573EF219155A2E000D3B00 /* Products */ = { isa = PBXGroup; children = ( 78573EF119155A2E000D3B00 /* AspectsDemo.app */, 78573F0C19155A2E000D3B00 /* AspectsDemoTests.xctest */, ); name = Products; sourceTree = ""; }; 78573EF319155A2E000D3B00 /* Frameworks */ = { isa = PBXGroup; children = ( 78573EF419155A2E000D3B00 /* Foundation.framework */, 78573EF619155A2E000D3B00 /* CoreGraphics.framework */, 78573EF819155A2E000D3B00 /* UIKit.framework */, 78573F0D19155A2E000D3B00 /* XCTest.framework */, ); name = Frameworks; sourceTree = ""; }; 78573EFA19155A2E000D3B00 /* AspectsDemo */ = { isa = PBXGroup; children = ( 78573F2419155A74000D3B00 /* Aspects.h */, 78573F2319155A74000D3B00 /* Aspects.m */, 78573F0319155A2E000D3B00 /* AspectsAppDelegate.h */, 78573F0419155A2E000D3B00 /* AspectsAppDelegate.m */, 78573F0619155A2E000D3B00 /* Images.xcassets */, 78573EFB19155A2E000D3B00 /* Supporting Files */, 78D7D76E19177C8E002EB314 /* AspectsViewController.h */, 78D7D76F19177C8E002EB314 /* AspectsViewController.m */, 78D7D77019177C8E002EB314 /* AspectsViewController.xib */, ); path = AspectsDemo; sourceTree = ""; }; 78573EFB19155A2E000D3B00 /* Supporting Files */ = { isa = PBXGroup; children = ( 78573EFC19155A2E000D3B00 /* AspectsDemo-Info.plist */, 78573EFD19155A2E000D3B00 /* InfoPlist.strings */, 78573F0019155A2E000D3B00 /* main.m */, 78573F0219155A2E000D3B00 /* AspectsDemo-Prefix.pch */, ); name = "Supporting Files"; sourceTree = ""; }; 78573F1319155A2E000D3B00 /* AspectsDemoTests */ = { isa = PBXGroup; children = ( 78573F1919155A2E000D3B00 /* AspectsDemoTests.m */, 78573F1419155A2E000D3B00 /* Supporting Files */, ); path = AspectsDemoTests; sourceTree = ""; }; 78573F1419155A2E000D3B00 /* Supporting Files */ = { isa = PBXGroup; children = ( 78573F1519155A2E000D3B00 /* AspectsDemoTests-Info.plist */, 78573F1619155A2E000D3B00 /* InfoPlist.strings */, ); name = "Supporting Files"; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ 78573EF019155A2E000D3B00 /* AspectsDemo */ = { isa = PBXNativeTarget; buildConfigurationList = 78573F1D19155A2E000D3B00 /* Build configuration list for PBXNativeTarget "AspectsDemo" */; buildPhases = ( 78573EED19155A2E000D3B00 /* Sources */, 78573EEE19155A2E000D3B00 /* Frameworks */, 78573EEF19155A2E000D3B00 /* Resources */, ); buildRules = ( ); dependencies = ( ); name = AspectsDemo; productName = AspectsDemo; productReference = 78573EF119155A2E000D3B00 /* AspectsDemo.app */; productType = "com.apple.product-type.application"; }; 78573F0B19155A2E000D3B00 /* AspectsDemoTests */ = { isa = PBXNativeTarget; buildConfigurationList = 78573F2019155A2E000D3B00 /* Build configuration list for PBXNativeTarget "AspectsDemoTests" */; buildPhases = ( 78573F0819155A2E000D3B00 /* Sources */, 78573F0919155A2E000D3B00 /* Frameworks */, 78573F0A19155A2E000D3B00 /* Resources */, ); buildRules = ( ); dependencies = ( 78573F1219155A2E000D3B00 /* PBXTargetDependency */, ); name = AspectsDemoTests; productName = AspectsDemoTests; productReference = 78573F0C19155A2E000D3B00 /* AspectsDemoTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 78573EE919155A2E000D3B00 /* Project object */ = { isa = PBXProject; attributes = { CLASSPREFIX = Aspects; LastUpgradeCheck = 0510; ORGANIZATIONNAME = "PSPDFKit GmbH"; TargetAttributes = { 78573F0B19155A2E000D3B00 = { TestTargetID = 78573EF019155A2E000D3B00; }; }; }; buildConfigurationList = 78573EEC19155A2E000D3B00 /* Build configuration list for PBXProject "AspectsDemo" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, ); mainGroup = 78573EE819155A2E000D3B00; productRefGroup = 78573EF219155A2E000D3B00 /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( 78573EF019155A2E000D3B00 /* AspectsDemo */, 78573F0B19155A2E000D3B00 /* AspectsDemoTests */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ 78573EEF19155A2E000D3B00 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( 78573EFF19155A2E000D3B00 /* InfoPlist.strings in Resources */, 78573F0719155A2E000D3B00 /* Images.xcassets in Resources */, 78D7D77219177C8E002EB314 /* AspectsViewController.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; 78573F0A19155A2E000D3B00 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( 78573F1819155A2E000D3B00 /* InfoPlist.strings in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ 78573EED19155A2E000D3B00 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 78D7D77119177C8E002EB314 /* AspectsViewController.m in Sources */, 78573F0519155A2E000D3B00 /* AspectsAppDelegate.m in Sources */, 78573F2519155A74000D3B00 /* Aspects.m in Sources */, 78573F0119155A2E000D3B00 /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 78573F0819155A2E000D3B00 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 78573F1A19155A2E000D3B00 /* AspectsDemoTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ 78573F1219155A2E000D3B00 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 78573EF019155A2E000D3B00 /* AspectsDemo */; targetProxy = 78573F1119155A2E000D3B00 /* PBXContainerItemProxy */; }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ 78573EFD19155A2E000D3B00 /* InfoPlist.strings */ = { isa = PBXVariantGroup; children = ( 78573EFE19155A2E000D3B00 /* en */, ); name = InfoPlist.strings; sourceTree = ""; }; 78573F1619155A2E000D3B00 /* InfoPlist.strings */ = { isa = PBXVariantGroup; children = ( 78573F1719155A2E000D3B00 /* en */, ); name = InfoPlist.strings; sourceTree = ""; }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ 78573F1B19155A2E000D3B00 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 6.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; WARNING_CFLAGS = ( "-Wall", "-Wextra", "-Wno-unused-parameter", "-Wno-sign-compare", "-Wdocumentation", "-Wno-static-in-inline", "-Wno-objc-missing-property-synthesis", "-Wcast-align", "-Wmissing-declarations", "-Wmissing-prototypes", "-Woverlength-strings", "-Wshadow", "-Wundeclared-selector", "-Wunreachable-code", "-Wformat=2", ); }; name = Debug; }; 78573F1C19155A2E000D3B00 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = YES; ENABLE_NS_ASSERTIONS = NO; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 6.0; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; WARNING_CFLAGS = ( "-Wall", "-Wextra", "-Wno-unused-parameter", "-Wno-sign-compare", "-Wdocumentation", "-Wno-static-in-inline", "-Wno-objc-missing-property-synthesis", "-Wcast-align", "-Wmissing-declarations", "-Wmissing-prototypes", "-Woverlength-strings", "-Wshadow", "-Wundeclared-selector", "-Wunreachable-code", "-Wformat=2", ); }; name = Release; }; 78573F1E19155A2E000D3B00 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; INFOPLIST_FILE = "AspectsDemo/AspectsDemo-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; name = Debug; }; 78573F1F19155A2E000D3B00 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; INFOPLIST_FILE = "AspectsDemo/AspectsDemo-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; name = Release; }; 78573F2119155A2E000D3B00 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/AspectsDemo.app/AspectsDemo"; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", "$(inherited)", "$(DEVELOPER_FRAMEWORKS_DIR)", ); GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); INFOPLIST_FILE = "AspectsDemoTests/AspectsDemoTests-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; WRAPPER_EXTENSION = xctest; }; name = Debug; }; 78573F2219155A2E000D3B00 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/AspectsDemo.app/AspectsDemo"; FRAMEWORK_SEARCH_PATHS = ( "$(SDKROOT)/Developer/Library/Frameworks", "$(inherited)", "$(DEVELOPER_FRAMEWORKS_DIR)", ); GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemo/AspectsDemo-Prefix.pch"; INFOPLIST_FILE = "AspectsDemoTests/AspectsDemoTests-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; WRAPPER_EXTENSION = xctest; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ 78573EEC19155A2E000D3B00 /* Build configuration list for PBXProject "AspectsDemo" */ = { isa = XCConfigurationList; buildConfigurations = ( 78573F1B19155A2E000D3B00 /* Debug */, 78573F1C19155A2E000D3B00 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 78573F1D19155A2E000D3B00 /* Build configuration list for PBXNativeTarget "AspectsDemo" */ = { isa = XCConfigurationList; buildConfigurations = ( 78573F1E19155A2E000D3B00 /* Debug */, 78573F1F19155A2E000D3B00 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 78573F2019155A2E000D3B00 /* Build configuration list for PBXNativeTarget "AspectsDemoTests" */ = { isa = XCConfigurationList; buildConfigurations = ( 78573F2119155A2E000D3B00 /* Debug */, 78573F2219155A2E000D3B00 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; rootObject = 78573EE919155A2E000D3B00 /* Project object */; } ================================================ FILE: AspectsDemo/AspectsDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata ================================================ ================================================ FILE: AspectsDemo/AspectsDemo.xcodeproj/project.xcworkspace/xcshareddata/AspectsDemo.xccheckout ================================================ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier AEBB4B3B-FCA8-4B2C-A364-473DA52FD1F1 IDESourceControlProjectName AspectsDemo IDESourceControlProjectOriginsDictionary BFDD7EB6-262A-4289-BF40-2E5B677EEC58 https://github.com/steipete/Aspects.git IDESourceControlProjectPath AspectsDemo/AspectsDemo.xcodeproj/project.xcworkspace IDESourceControlProjectRelativeInstallPathDictionary BFDD7EB6-262A-4289-BF40-2E5B677EEC58 ../../.. IDESourceControlProjectURL https://github.com/steipete/Aspects.git IDESourceControlProjectVersion 110 IDESourceControlProjectWCCIdentifier BFDD7EB6-262A-4289-BF40-2E5B677EEC58 IDESourceControlProjectWCConfigurations IDESourceControlRepositoryExtensionIdentifierKey public.vcs.git IDESourceControlWCCIdentifierKey BFDD7EB6-262A-4289-BF40-2E5B677EEC58 IDESourceControlWCCName Aspects ================================================ FILE: AspectsDemo/AspectsDemo.xcodeproj/xcshareddata/xcschemes/AspectsDemo.xcscheme ================================================ ================================================ FILE: AspectsDemo/AspectsDemoTests/AspectsDemoTests-Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier com.pspdfkit.aspects.${PRODUCT_NAME:rfc1034identifier} CFBundleInfoDictionaryVersion 6.0 CFBundlePackageType BNDL CFBundleShortVersionString 1.0 CFBundleSignature ???? CFBundleVersion 1 ================================================ FILE: AspectsDemo/AspectsDemoTests/AspectsDemoTests.m ================================================ // // AspectsDemoTests.m // AspectsDemoTests // // Created by Peter Steinberger on 03/05/14. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import #import #import "Aspects.h" @interface TestClass : NSObject @property (nonatomic, copy) NSString *string; @property (nonatomic, assign) BOOL kvoTestCalled; - (void)testCall; - (void)testCallAndExecuteBlock:(dispatch_block_t)block; - (double)callReturnsDouble; - (long long)callReturnsLongLong; @end @implementation TestClass - (void)testCall { NSLog(@"Original call"); } - (void)testCallAndExecuteBlock:(dispatch_block_t)block { if (block) block(); } - (CGRect)testThatReturnsAStruct { return CGRectMake(100, 100, 100, 100); } - (double)callReturnsDouble { return 1.5; } - (long long)callReturnsLongLong { return 99; } @end @interface TestWithCustomForwardInvocation : NSObject @property (nonatomic, assign) BOOL forwardInvocationCalled; - (void)test; @end @implementation TestWithCustomForwardInvocation - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { if (aSelector == NSSelectorFromString(@"non_existing_selector")) { return [NSMethodSignature signatureWithObjCTypes:"v@:"]; } return [super methodSignatureForSelector:aSelector]; } - (void)forwardInvocation:(NSInvocation *)anInvocation { NSLog(@"Custom!!!"); self.forwardInvocationCalled = YES; if (anInvocation.selector != NSSelectorFromString(@"non_existing_selector")) { [super forwardInvocation:anInvocation]; } } - (void)test { NSLog(@"%s", __PRETTY_FUNCTION__); } @end @interface AspectsTests : XCTestCase @end @implementation AspectsTests /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test Block Signature - (void)testMatchingBlockSignature { TestClass *testClass = [TestClass new]; __block BOOL called = NO; id aspect = [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^(id info) { called = YES; } error:NULL]; [testClass testCall]; XCTAssertTrue(called, @"Flag must have been set."); TestClass *testClass2 = [TestClass new]; called = NO; [testClass2 testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testMatchingBlockSignature2 { TestClass *testClass = [TestClass new]; __block BOOL called = NO; id aspect = [testClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionAfter usingBlock:^{ called = YES; } error:NULL]; [testClass testCallAndExecuteBlock:NULL]; XCTAssertTrue(called, @"Flag must have been set."); TestClass *testClass2 = [TestClass new]; called = NO; [testClass2 testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testTooLargeBlockSignature { TestClass *testClass = [TestClass new]; NSError *error = nil; __block BOOL called = NO; id aspect = [testClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionAfter usingBlock:^(id info, id test, id foo, id bar) { called = YES; } error:&error]; [testClass testCallAndExecuteBlock:NULL]; XCTAssertNil(aspect); XCTAssertTrue(error.code == AspectErrorIncompatibleBlockSignature); XCTAssertFalse(called, @"Flag must have not been set."); TestClass *testClass2 = [TestClass new]; called = NO; [testClass2 testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); } - (void)testMismatchingSignature { TestClass *testClass = [TestClass new]; NSError *error = nil; __block BOOL called = NO; id aspect = [testClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionAfter usingBlock:^(NSUInteger foobar) { called = YES; } error:&error]; [testClass testCallAndExecuteBlock:NULL]; XCTAssertNil(aspect); XCTAssertTrue(error.code == AspectErrorIncompatibleBlockSignature); XCTAssertFalse(called, @"Flag must have not been set."); TestClass *testClass2 = [TestClass new]; called = NO; [testClass2 testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Generic Hook Tests - (void)testCALayerExploding { __block BOOL called = NO; id globalAspect = [CALayer aspect_hookSelector:@selector(name) withOptions:AspectPositionAfter usingBlock:^(id info) { NSLog(@"Hello from %@", info.instance); called = YES; } error:NULL]; // We had some branches where this blew up already. CALayer *test = [CALayer new]; XCTAssertNotNil(test); [test name]; XCTAssertTrue(called, @"Flag needs to be called."); XCTAssertTrue([globalAspect remove]); } - (void)testInsteadHook { // Test object replacement. CALayer *testObject = [CALayer new]; testObject.name = @"Default text"; XCTAssertEqualObjects(testObject.name, @"Default text", @"Must match"); id aspect = [testObject aspect_hookSelector:@selector(name) withOptions:AspectPositionInstead usingBlock:^(id info) { NSString *customText = @"Custom Text"; [[info originalInvocation] setReturnValue:&customText]; } error:NULL]; XCTAssertEqualObjects(testObject.name, @"Custom Text", @"Must match"); // Test second object, and ensure that this doesn't change the override of the first object. CALayer *testObject2 = [CALayer new]; testObject2.name = @"Default text2"; XCTAssertEqualObjects(testObject2.name, @"Default text2", @"Must match"); id aspect2 = [testObject2 aspect_hookSelector:@selector(name) withOptions:AspectPositionInstead usingBlock:^(id info) { NSString *customText = @"Custom Text2"; [[info originalInvocation] setReturnValue:&customText]; } error:NULL]; XCTAssertEqualObjects(testObject2.name, @"Custom Text2", @"Must match"); // Globally override. id globalAspect = [CALayer aspect_hookSelector:@selector(name) withOptions:AspectPositionAfter usingBlock:^(id info) { NSString *customText = @"Global"; [[info originalInvocation] setReturnValue:&customText]; } error:NULL]; XCTAssertEqualObjects(testObject2.name, @"Global", @"Must match"); CALayer *testObject3 = [CALayer new]; XCTAssertEqualObjects(testObject3.name, @"Global", @"Must match"); testObject3.name = @"Test"; XCTAssertEqualObjects(testObject3.name, @"Global", @"Must match"); XCTAssertTrue([globalAspect remove], @"Must work"); XCTAssertEqualObjects(testObject3.name, @"Test", @"Must match"); // Test that removing an aspect returns the original. XCTAssertEqualObjects(testObject.name, @"Custom Text", @"Must match"); XCTAssertTrue([aspect remove], @"Must return YES"); XCTAssertEqualObjects(testObject.name, @"Default text", @"Must match"); XCTAssertFalse([aspect remove], @"Must return NO"); XCTAssertTrue([aspect2 remove], @"Must be able to deregister"); } - (void)testAspectsCalledPerObject { TestClass *testClass = [TestClass new]; __block BOOL called = NO; id aspect = [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^(id info) { called = YES; } error:NULL]; [testClass testCall]; XCTAssertTrue(called, @"Flag must have been set."); TestClass *testClass2 = [TestClass new]; called = NO; [testClass2 testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testExecutionOrderAndMultipleRegistation { TestClass *testClass = [TestClass new]; __block BOOL called_before = NO; __block BOOL called_after = NO; __block BOOL called_after2 = NO; id aspect_before = [TestClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionBefore usingBlock:^(id info, id block) { called_before = YES; } error:NULL]; id aspect_after = [TestClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionAfter usingBlock:^(id info, id block) { called_after2 = YES; } error:NULL]; id aspect_after2 = [TestClass aspect_hookSelector:@selector(testCallAndExecuteBlock:) withOptions:AspectPositionAfter usingBlock:^(id info, id block) { called_after = YES; } error:NULL]; [testClass testCallAndExecuteBlock:^{ XCTAssertTrue(called_before, @"Flag must have been set."); XCTAssertFalse(called_after, @"Flag must have not been set."); XCTAssertFalse(called_after2, @"Flag must have not been set."); }]; XCTAssertTrue(called_before, @"Flag must have been set."); XCTAssertTrue(called_after, @"Flag must have been set."); XCTAssertTrue(called_after2, @"Flag must have been set."); XCTAssertTrue([aspect_after remove], @"Must be able to deregister"); XCTAssertTrue([aspect_before remove], @"Must be able to deregister"); XCTAssertTrue([aspect_after2 remove], @"Must be able to deregister"); XCTAssertFalse([aspect_after remove], @"Must not be able to deregister twice"); XCTAssertFalse([aspect_before remove], @"Must not be able to deregister twice"); XCTAssertFalse([aspect_after2 remove], @"Must not be able to deregister twice"); } - (void)testExample { TestClass *testClass = [TestClass new]; TestClass *testClass2 = [TestClass new]; __block BOOL testCallCalled = NO; id aspectToken = [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; [testClass2 testCallAndExecuteBlock:^{ [testClass testCall]; }]; XCTAssertTrue(testCallCalled, @"Calling testCallAndExecuteBlock must call testCall"); XCTAssertTrue([aspectToken remove], @"Must be able to deregister"); } - (void)testStructReturn { TestClass *testClass = [TestClass new]; CGRect rect = [testClass testThatReturnsAStruct]; id aspect = [testClass aspect_hookSelector:@selector(testThatReturnsAStruct) withOptions:AspectPositionAfter usingBlock:^(id info) { } error:NULL]; CGRect rectHooked = [testClass testThatReturnsAStruct]; XCTAssertTrue(CGRectEqualToRect(rect, rectHooked), @"Must be equal"); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testDoubleReturn { TestClass *testClass = [TestClass new]; double d1 = [testClass callReturnsDouble]; __block BOOL testCallCalled = NO; id aspect = [testClass aspect_hookSelector:@selector(callReturnsDouble) withOptions:AspectPositionAfter usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; double d2 = [testClass callReturnsDouble]; XCTAssertEqual(d1, d2, @"Must be equal"); XCTAssertTrue(testCallCalled, @"Must call hook"); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testDoubleReturnInstead { TestClass *testClass = [TestClass new]; double previousExpectedValue = [testClass callReturnsDouble]; double expectedValue = 3.5; id aspect = [testClass aspect_hookSelector:@selector(callReturnsDouble) withOptions:AspectPositionInstead usingBlock:^(id info){ double toReturn = 3.5; void *ptr = &toReturn; [info.originalInvocation setReturnValue:ptr]; }error:NULL]; double actualValue = [testClass callReturnsDouble]; XCTAssertNotEqual(previousExpectedValue, actualValue, @"Must not return what it returned before we called our Instead"); XCTAssertEqual(expectedValue, actualValue, @"Must be equal"); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testLongLongReturn { TestClass *testClass = [TestClass new]; long long d1 = [testClass callReturnsLongLong]; __block BOOL testCallCalled = NO; id aspect = [testClass aspect_hookSelector:@selector(callReturnsLongLong) withOptions:AspectPositionAfter usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; long long d2 = [testClass callReturnsLongLong]; XCTAssertEqual(d1, d2, @"Must be equal"); XCTAssertTrue(testCallCalled, @"Must call hook"); XCTAssertTrue([aspect remove], @"Must be able to deregister"); } - (void)testHookReleaseIsNotAllowed { TestClass *testClass = [TestClass new]; __block BOOL testCallCalled = NO; id aspectToken = [testClass aspect_hookSelector:NSSelectorFromString(@"release") withOptions:AspectPositionAfter usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; XCTAssertNil(aspectToken, @"Token must be nil"); [testClass testCall]; XCTAssertFalse(testCallCalled, @"Release should not be hookable"); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test dealloc hooking // Hooking for deallic is delicate, but should work for AspectPositionBefore and AspectPositionAfter. - (void)testDeallocHooking { TestClass *testClass = [TestClass new]; __block BOOL testCallCalled = NO; __block id aspectToken = [testClass aspect_hookSelector:NSSelectorFromString(@"dealloc") withOptions:AspectPositionBefore usingBlock:^(id info) { testCallCalled = YES; NSLog(@"called from dealloc"); } error:NULL]; XCTAssertNotNil(aspectToken, @"Must return a token."); testClass = nil; XCTAssertTrue(testCallCalled, @"Dealloc-hook must work."); } // Replacing dealloc should not work. - (void)testDeallocReplacing { TestClass *testClass = [TestClass new]; NSError *error; __block BOOL deallocCalled = NO; id aspectToken = [testClass aspect_hookSelector:NSSelectorFromString(@"dealloc") withOptions:AspectPositionInstead usingBlock:^(id info) { deallocCalled = YES; NSLog(@"called from dealloc"); } error:&error]; XCTAssertNil(aspectToken, @"Must NOT return a token."); XCTAssertEqual(error.code, AspectErrorSelectorDeallocPosition, @"Error must be correct"); testClass = nil; XCTAssertFalse(deallocCalled, @"Dealloc-hook must not work."); } - (void)testInvalidSelectorHooking { TestClass *testClass = [TestClass new]; NSError *error; __block id aspectToken = [testClass aspect_hookSelector:NSSelectorFromString(@"fakeSelector") withOptions:AspectPositionBefore usingBlock:^(id info) { } error:&error]; XCTAssertNil(aspectToken, @"Must return nil token."); XCTAssertEqual(error.code, AspectErrorDoesNotRespondToSelector, @"Error code must match"); } - (void)testInvalidGlobalSelectorHooking { NSError *error; __block id aspectToken = [TestClass aspect_hookSelector:NSSelectorFromString(@"fakeSelector2") withOptions:AspectPositionBefore usingBlock:^(id info) { } error:&error]; XCTAssertNil(aspectToken, @"Must return nil token."); XCTAssertEqual(error.code, AspectErrorDoesNotRespondToSelector, @"Error code must match"); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test Deregistration - (void)testInstanceTokenDeregistration { TestClass *testClass = [TestClass new]; __block BOOL testCallCalled = NO; id aspectToken = [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionInstead usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; XCTAssertNotNil(aspectToken, @"Must return a token."); [testClass testCall]; XCTAssertTrue(testCallCalled, @"Hook must work."); XCTAssertNotEqualObjects(testClass.class, object_getClass(testClass), @"Object must have a custom subclass."); XCTAssertTrue([aspectToken remove], @"Deregistration must work"); XCTAssertEqualObjects(testClass.class, object_getClass(testClass), @"Object must not have a custom subclass."); testCallCalled = NO; [testClass testCall]; XCTAssertFalse(testCallCalled, @"Hook must no longer work."); XCTAssertFalse([aspectToken remove], @"Deregistration must not work twice"); } - (void)testGlobalTokenDeregistrationWithCustomForwardInvocation { TestWithCustomForwardInvocation *testClass = [TestWithCustomForwardInvocation new]; Method originalForwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); IMP originalForwardInvocationIMP = method_getImplementation(originalForwardInvocationMethod); // Test that forwardInvocation points to NSObject. { Method objectMethod = class_getInstanceMethod(TestWithCustomForwardInvocation.class, @selector(forwardInvocation:)); XCTAssertEqual(method_getImplementation(originalForwardInvocationMethod), method_getImplementation(objectMethod), @"Implementations must be equal"); } __block BOOL testCalled = NO; id token = [TestWithCustomForwardInvocation aspect_hookSelector:@selector(test) withOptions:AspectPositionInstead usingBlock:^(id info) { testCalled = YES; } error:NULL]; XCTAssertNotNil(token, @"Must return a token."); [testClass test]; XCTAssertTrue(testCalled, @"Hook must work."); XCTAssertEqualObjects(testClass.class, object_getClass(testClass), @"Object must not have a custom subclass."); // Test that forwardInvocation points to our own implementation. { Method forwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); XCTAssertNotEqual(method_getImplementation(forwardInvocationMethod), originalForwardInvocationIMP, @"Implementations must not be equal"); } XCTAssertTrue([token remove], @"Deregistration must work"); // Test that forwardInvocation (again) points to NSObject and thus is correctly restored. { Method forwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); XCTAssertEqual(method_getImplementation(forwardInvocationMethod), originalForwardInvocationIMP, @"Implementations must be equal"); } testCalled = NO; [testClass test]; XCTAssertFalse(testCalled, @"Hook must no longer work."); XCTAssertFalse([token remove], @"Deregistration must not work twice"); } - (void)testGlobalTokenDeregistration { TestClass *testClass = [TestClass new]; // Test that forwardInvocation points to NSObject. { Method forwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); Method objectMethod = class_getInstanceMethod(NSObject.class, @selector(forwardInvocation:)); XCTAssertEqual(method_getImplementation(forwardInvocationMethod), method_getImplementation(objectMethod), @"Implementations must be equal"); } __block BOOL testCallCalled = NO; id token = [TestClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionInstead usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; XCTAssertNotNil(token, @"Must return a token."); [testClass testCall]; XCTAssertTrue(testCallCalled, @"Hook must work."); XCTAssertEqualObjects(testClass.class, object_getClass(testClass), @"Object must not have a custom subclass."); // Test that forwardInvocation points to our own implementation. { Method forwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); Method objectMethod = class_getInstanceMethod(NSObject.class, @selector(forwardInvocation:)); XCTAssertNotEqual(method_getImplementation(forwardInvocationMethod), method_getImplementation(objectMethod), @"Implementations must not be equal"); } XCTAssertTrue([token remove], @"Deregistration must work"); // Test that forwardInvocation (again) points to NSObject and thus is correctly restored. { Method forwardInvocationMethod = class_getInstanceMethod(testClass.class, @selector(forwardInvocation:)); Method objectMethod = class_getInstanceMethod(NSObject.class, @selector(forwardInvocation:)); XCTAssertEqual(method_getImplementation(forwardInvocationMethod), method_getImplementation(objectMethod), @"Implementations must be equal"); } testCallCalled = NO; [testClass testCall]; XCTAssertFalse(testCallCalled, @"Hook must no longer work."); XCTAssertFalse([token remove], @"Deregistration must not work twice"); } - (void)testSimpleDeregistration { TestClass *testClass = [TestClass new]; __block BOOL called = NO; id aspectToken = [TestClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^(id info) { called = YES; } error:NULL]; [testClass testCall]; XCTAssertTrue(called, @"Flag must have been set."); called = NO; XCTAssertTrue([aspectToken remove], @"Must allow deregistration"); [testClass testCall]; XCTAssertFalse(called, @"Flag must have been NOT set."); } - (void)testAutoDeregistration { TestClass *testClass = [TestClass new]; __block BOOL testCallCalled = NO; id aspectToken = [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter|AspectOptionAutomaticRemoval usingBlock:^(id info) { testCallCalled = YES; } error:NULL]; [testClass testCall]; XCTAssertTrue(testCallCalled, @"Must be set to YES"); testCallCalled = NO; [testClass testCall]; XCTAssertFalse(testCallCalled, @"Must be set to NO"); XCTAssertFalse([aspectToken remove], @"Must not able to deregister again"); } /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test KVO - (void)testKVOCoexistance { TestClass *testClass = [TestClass new]; __block BOOL hookCalled = NO; id aspectToken = [testClass aspect_hookSelector:@selector(setString:) withOptions:AspectPositionAfter usingBlock:^(id info, NSString *string) { NSLog(@"Aspect hook!"); hookCalled = YES; } error:NULL]; [testClass addObserver:self forKeyPath:NSStringFromSelector(@selector(string)) options:0 context:_cmd]; XCTAssertFalse(testClass.kvoTestCalled, @"KVO must be not set"); testClass.string = @"test"; XCTAssertTrue(hookCalled, @"Hook must be called"); XCTAssertTrue(testClass.kvoTestCalled, @"KVO must work"); [testClass removeObserver:self forKeyPath:NSStringFromSelector(@selector(string)) context:_cmd]; hookCalled = NO; testClass.kvoTestCalled = NO; testClass.string = @"test2"; XCTAssertTrue(hookCalled, @"Hook must be called"); XCTAssertFalse(testClass.kvoTestCalled, @"KVO must no longer work"); XCTAssertTrue([aspectToken remove], @"Must be able to deregister"); } // TODO: Pre-registeded KVO is currently not working. //- (void)testKVOCoexistanceWithPreregisteredKVO { // TestClass *testClass = [TestClass new]; // XCTAssertFalse(testClass.kvoTestCalled, @"KVO must be not set"); // [testClass addObserver:self forKeyPath:NSStringFromSelector(@selector(string)) options:0 context:_cmd]; // testClass.string = @"test"; // XCTAssertTrue(testClass.kvoTestCalled, @"KVO must work"); // // __block BOOL hookCalled = NO; // [testClass aspect_hookSelector:@selector(setString:) withOptions:AspectPositionAfter usingBlock:^(id instance, NSArray *arguments) { // NSLog(@"Aspect hook!"); // hookCalled = YES; // }]; // // XCTAssertFalse(testClass.kvoTestCalled, @"KVO must be not set"); // testClass.string = @"test"; // XCTAssertTrue(hookCalled, @"Hook must be called"); // XCTAssertTrue(testClass.kvoTestCalled, @"KVO must work"); // [testClass removeObserver:self forKeyPath:NSStringFromSelector(@selector(string)) context:_cmd]; // hookCalled = NO; // testClass.kvoTestCalled = NO; // testClass.string = @"test2"; // XCTAssertTrue(hookCalled, @"Hook must be called"); // XCTAssertFalse(testClass.kvoTestCalled, @"KVO must no longer work"); //} - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"KVO!"); ((TestClass *)object).kvoTestCalled = YES; } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test that a custom forwardInvocation: is being called. @interface AspectsForwardInvocationTests : XCTestCase @end @implementation AspectsForwardInvocationTests - (void)testEnsureForwardInvocationIsCalled { TestWithCustomForwardInvocation *testClass = [TestWithCustomForwardInvocation new]; XCTAssertFalse(testClass.forwardInvocationCalled, @"Must have not called custom forwardInvocation:"); id aspectToken = [TestWithCustomForwardInvocation aspect_hookSelector:@selector(test) withOptions:AspectPositionInstead usingBlock:^(id info) { NSLog(@"Aspect hook called"); } error:NULL]; [testClass test]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [testClass performSelector:NSSelectorFromString(@"non_existing_selector")]; #pragma clang diagnostic pop XCTAssertTrue(testClass.forwardInvocationCalled, @"Must have called custom forwardInvocation:"); XCTAssertTrue([aspectToken remove], @"Must be able to deregister"); } @end /////////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Test Selector Mangling @interface A : NSObject - (void)foo; @end @implementation A - (void)foo { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)bar { NSLog(@"%s", __PRETTY_FUNCTION__); } @end @interface B : A @end @implementation B - (void)foo { NSLog(@"%s", __PRETTY_FUNCTION__); [super foo]; } - (void)bar { NSLog(@"%s", __PRETTY_FUNCTION__); [super bar]; } @end @interface C : NSObject - (void)foo; @end @implementation C - (void)foo { NSLog(@"%s", __PRETTY_FUNCTION__); } @end @interface AspectsSelectorTests : XCTestCase @end @implementation AspectsSelectorTests //- (void)testSelectorMangling { // __block BOOL A_aspect_called = NO; // __block BOOL B_aspect_called = NO; // [B aspect_hookSelector:@selector(foo) withOptions:AspectPositionBefore usingBlock:^(id instance, NSArray *arguments) { // NSLog(@"before -[B foo]"); // B_aspect_called = YES; // }]; // [A aspect_hookSelector:@selector(foo) withOptions:AspectPositionBefore usingBlock:^(id instance, NSArray *arguments) { // NSLog(@"before -[A foo]"); // A_aspect_called = YES; // }]; // // B *b = [B new]; // [b foo]; // // XCTAssertTrue(B_aspect_called, @"B aspect should be called"); // XCTAssertFalse(A_aspect_called, @"A aspect should not be called"); //} // TODO: Since tests change the runtime, it's hard to clean up. - (void)testSelectorMangling2 { __block BOOL A_aspect_called = NO; __block BOOL B_aspect_called = NO; __block BOOL C_aspect_called = NO; id aspectToken1 = [A aspect_hookSelector:@selector(foo) withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"before -[A foo]"); A_aspect_called = YES; } error:NULL]; XCTAssertNotNil(aspectToken1, @"Must return a token"); id aspectToken2 = [B aspect_hookSelector:@selector(foo) withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"before -[B foo]"); B_aspect_called = YES; } error:NULL]; XCTAssertNil(aspectToken2, @"Must not return a token"); // a sibling and it's subclasses should be able to hook the same selector id aspectToken3 = [C aspect_hookSelector:@selector(foo) withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"before -[C foo]"); C_aspect_called = YES; } error:NULL]; XCTAssertNotNil(aspectToken3, @"Must return a token"); B *b = [B new]; [b foo]; // TODO: A is not yet called, we can't detect the target IMP for an invocation. XCTAssertTrue(A_aspect_called, @"A aspect should be called"); XCTAssertFalse(B_aspect_called, @"B aspect should not be called"); XCTAssertFalse(C_aspect_called, @"C aspect should not be called"); C *c = [C new]; [c foo]; XCTAssertTrue(C_aspect_called, @"C aspect should be called"); XCTAssertTrue([aspectToken1 remove], @"Must be able to deregister"); XCTAssertTrue([aspectToken3 remove], @"Must be able to deregister"); } - (void)testSelectorMangling3 { __block BOOL A_aspect_called = NO; __block BOOL B_aspect_called = NO; id aspectToken1 = [B aspect_hookSelector:@selector(bar) withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"before -[B bar]"); B_aspect_called = YES; } error:NULL]; XCTAssertNotNil(aspectToken1, @"Must return a token"); // if a subclass already hooks this selector we shouldn't be able to hook it in a superclass id aspectToken2 = [A aspect_hookSelector:@selector(bar) withOptions:AspectPositionBefore usingBlock:^(id info) { NSLog(@"before -[A bar]"); A_aspect_called = YES; } error:NULL]; XCTAssertNil(aspectToken2, @"Must not return a token"); B *b = [B new]; [b bar]; XCTAssertFalse(A_aspect_called, @"A aspect should not be called"); XCTAssertTrue(B_aspect_called, @"B aspect should be called"); XCTAssertTrue([aspectToken1 remove], @"Must be able to deregister"); } @end ================================================ FILE: AspectsDemo/AspectsDemoTests/en.lproj/InfoPlist.strings ================================================ /* Localized versions of Info.plist keys */ ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/AspectsAppDelegate.h ================================================ // // AspectsAppDelegate.h // AspectsDemoOSX // // Created by Ash Furrow on 2014-05-05. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import @interface AspectsAppDelegate : NSObject @property (assign) IBOutlet NSWindow *window; @end ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/AspectsAppDelegate.m ================================================ // // AspectsAppDelegate.m // AspectsDemoOSX // // Created by Ash Furrow on 2014-05-05. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import "AspectsAppDelegate.h" #import "Aspects.h" @implementation AspectsAppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Ignore hooks when we are testing. if (!NSClassFromString(@"XCTestCase")) { [self.window aspect_hookSelector:@selector(displayIfNeeded) withOptions:0 usingBlock:^(id instance, NSArray *arguments) { NSLog(@"Window is displayed!"); } error:NULL]; } } @end ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/AspectsDemoOSX-Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIconFile CFBundleIdentifier com.pspdfkit.aspects.${PRODUCT_NAME:rfc1034identifier} CFBundleInfoDictionaryVersion 6.0 CFBundleName ${PRODUCT_NAME} CFBundlePackageType APPL CFBundleShortVersionString 1.0 CFBundleSignature ???? CFBundleVersion 1 LSMinimumSystemVersion ${MACOSX_DEPLOYMENT_TARGET} NSHumanReadableCopyright Copyright © 2014 PSPDFKit GmbH. All rights reserved. NSMainNibFile MainMenu NSPrincipalClass NSApplication ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/AspectsDemoOSX-Prefix.pch ================================================ // // Prefix header // // The contents of this file are implicitly included at the beginning of every source file. // #ifdef __OBJC__ #import #endif ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/Base.lproj/MainMenu.xib ================================================ Default Left to Right Right to Left Default Left to Right Right to Left ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/Images.xcassets/AppIcon.appiconset/Contents.json ================================================ { "images" : [ { "idiom" : "mac", "size" : "16x16", "scale" : "1x" }, { "idiom" : "mac", "size" : "16x16", "scale" : "2x" }, { "idiom" : "mac", "size" : "32x32", "scale" : "1x" }, { "idiom" : "mac", "size" : "32x32", "scale" : "2x" }, { "idiom" : "mac", "size" : "128x128", "scale" : "1x" }, { "idiom" : "mac", "size" : "128x128", "scale" : "2x" }, { "idiom" : "mac", "size" : "256x256", "scale" : "1x" }, { "idiom" : "mac", "size" : "256x256", "scale" : "2x" }, { "idiom" : "mac", "size" : "512x512", "scale" : "1x" }, { "idiom" : "mac", "size" : "512x512", "scale" : "2x" } ], "info" : { "version" : 1, "author" : "xcode" } } ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/en.lproj/Credits.rtf ================================================ {\rtf0\ansi{\fonttbl\f0\fswiss Helvetica;} {\colortbl;\red255\green255\blue255;} \paperw9840\paperh8400 \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural \f0\b\fs24 \cf0 Engineering: \b0 \ Some people\ \ \b Human Interface Design: \b0 \ Some other people\ \ \b Testing: \b0 \ Hopefully not nobody\ \ \b Documentation: \b0 \ Whoever\ \ \b With special thanks to: \b0 \ Mom\ } ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/en.lproj/InfoPlist.strings ================================================ /* Localized versions of Info.plist keys */ ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX/main.m ================================================ // // main.m // AspectsDemoOSX // // Created by Ash Furrow on 2014-05-05. // Copyright (c) 2014 PSPDFKit GmbH. All rights reserved. // #import int main(int argc, const char * argv[]) { return NSApplicationMain(argc, argv); } ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX.xcodeproj/project.pbxproj ================================================ // !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 46; objects = { /* Begin PBXBuildFile section */ 5E435B8E1917A2910028B862 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E435B8D1917A2910028B862 /* Cocoa.framework */; }; 5E435B981917A2910028B862 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5E435B961917A2910028B862 /* InfoPlist.strings */; }; 5E435B9A1917A2910028B862 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E435B991917A2910028B862 /* main.m */; }; 5E435B9E1917A2910028B862 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 5E435B9C1917A2910028B862 /* Credits.rtf */; }; 5E435BA11917A2910028B862 /* AspectsAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E435BA01917A2910028B862 /* AspectsAppDelegate.m */; }; 5E435BA41917A2910028B862 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5E435BA21917A2910028B862 /* MainMenu.xib */; }; 5E435BA61917A2910028B862 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E435BA51917A2910028B862 /* Images.xcassets */; }; 5E435BAD1917A2920028B862 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E435BAC1917A2920028B862 /* XCTest.framework */; }; 5E435BAE1917A2920028B862 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E435B8D1917A2910028B862 /* Cocoa.framework */; }; 5E435BB61917A2920028B862 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5E435BB41917A2920028B862 /* InfoPlist.strings */; }; 5E435BC31917A2BE0028B862 /* Aspects.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E435BC21917A2BE0028B862 /* Aspects.m */; }; 78BAEF4C191E3AD5006DABAF /* AspectsDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 78BAEF4B191E3AD5006DABAF /* AspectsDemoTests.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ 5E435BAF1917A2920028B862 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 5E435B821917A2910028B862 /* Project object */; proxyType = 1; remoteGlobalIDString = 5E435B891917A2910028B862; remoteInfo = AspectsDemoOSX; }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ 5E435B8A1917A2910028B862 /* AspectsDemoOSX.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AspectsDemoOSX.app; sourceTree = BUILT_PRODUCTS_DIR; }; 5E435B8D1917A2910028B862 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 5E435B901917A2910028B862 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; 5E435B911917A2910028B862 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; 5E435B921917A2910028B862 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 5E435B951917A2910028B862 /* AspectsDemoOSX-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "AspectsDemoOSX-Info.plist"; sourceTree = ""; }; 5E435B971917A2910028B862 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 5E435B991917A2910028B862 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 5E435B9B1917A2910028B862 /* AspectsDemoOSX-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AspectsDemoOSX-Prefix.pch"; sourceTree = ""; }; 5E435B9D1917A2910028B862 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = ""; }; 5E435B9F1917A2910028B862 /* AspectsAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AspectsAppDelegate.h; sourceTree = ""; }; 5E435BA01917A2910028B862 /* AspectsAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AspectsAppDelegate.m; sourceTree = ""; }; 5E435BA31917A2910028B862 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 5E435BA51917A2910028B862 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 5E435BAB1917A2920028B862 /* AspectsDemoOSXTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AspectsDemoOSXTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 5E435BAC1917A2920028B862 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 5E435BB31917A2920028B862 /* AspectsDemoOSXTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "AspectsDemoOSXTests-Info.plist"; sourceTree = ""; }; 5E435BB51917A2920028B862 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 5E435BC11917A2BE0028B862 /* Aspects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Aspects.h; path = ../../Aspects.h; sourceTree = ""; }; 5E435BC21917A2BE0028B862 /* Aspects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Aspects.m; path = ../../Aspects.m; sourceTree = ""; }; 78BAEF4B191E3AD5006DABAF /* AspectsDemoTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AspectsDemoTests.m; path = ../../AspectsDemo/AspectsDemoTests/AspectsDemoTests.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ 5E435B871917A2910028B862 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 5E435B8E1917A2910028B862 /* Cocoa.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; 5E435BA81917A2920028B862 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( 5E435BAE1917A2920028B862 /* Cocoa.framework in Frameworks */, 5E435BAD1917A2920028B862 /* XCTest.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 5E435B811917A2910028B862 = { isa = PBXGroup; children = ( 5E435B931917A2910028B862 /* AspectsDemoOSX */, 5E435BB11917A2920028B862 /* AspectsDemoOSXTests */, 5E435B8C1917A2910028B862 /* Frameworks */, 5E435B8B1917A2910028B862 /* Products */, ); sourceTree = ""; }; 5E435B8B1917A2910028B862 /* Products */ = { isa = PBXGroup; children = ( 5E435B8A1917A2910028B862 /* AspectsDemoOSX.app */, 5E435BAB1917A2920028B862 /* AspectsDemoOSXTests.xctest */, ); name = Products; sourceTree = ""; }; 5E435B8C1917A2910028B862 /* Frameworks */ = { isa = PBXGroup; children = ( 5E435B8D1917A2910028B862 /* Cocoa.framework */, 5E435BAC1917A2920028B862 /* XCTest.framework */, 5E435B8F1917A2910028B862 /* Other Frameworks */, ); name = Frameworks; sourceTree = ""; }; 5E435B8F1917A2910028B862 /* Other Frameworks */ = { isa = PBXGroup; children = ( 5E435B901917A2910028B862 /* AppKit.framework */, 5E435B911917A2910028B862 /* CoreData.framework */, 5E435B921917A2910028B862 /* Foundation.framework */, ); name = "Other Frameworks"; sourceTree = ""; }; 5E435B931917A2910028B862 /* AspectsDemoOSX */ = { isa = PBXGroup; children = ( 5E435BC11917A2BE0028B862 /* Aspects.h */, 5E435BC21917A2BE0028B862 /* Aspects.m */, 5E435B9F1917A2910028B862 /* AspectsAppDelegate.h */, 5E435BA01917A2910028B862 /* AspectsAppDelegate.m */, 5E435BA21917A2910028B862 /* MainMenu.xib */, 5E435BA51917A2910028B862 /* Images.xcassets */, 5E435B941917A2910028B862 /* Supporting Files */, ); path = AspectsDemoOSX; sourceTree = ""; }; 5E435B941917A2910028B862 /* Supporting Files */ = { isa = PBXGroup; children = ( 5E435B951917A2910028B862 /* AspectsDemoOSX-Info.plist */, 5E435B961917A2910028B862 /* InfoPlist.strings */, 5E435B991917A2910028B862 /* main.m */, 5E435B9B1917A2910028B862 /* AspectsDemoOSX-Prefix.pch */, 5E435B9C1917A2910028B862 /* Credits.rtf */, ); name = "Supporting Files"; sourceTree = ""; }; 5E435BB11917A2920028B862 /* AspectsDemoOSXTests */ = { isa = PBXGroup; children = ( 78BAEF4B191E3AD5006DABAF /* AspectsDemoTests.m */, 5E435BB21917A2920028B862 /* Supporting Files */, ); path = AspectsDemoOSXTests; sourceTree = ""; }; 5E435BB21917A2920028B862 /* Supporting Files */ = { isa = PBXGroup; children = ( 5E435BB31917A2920028B862 /* AspectsDemoOSXTests-Info.plist */, 5E435BB41917A2920028B862 /* InfoPlist.strings */, ); name = "Supporting Files"; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ 5E435B891917A2910028B862 /* AspectsDemoOSX */ = { isa = PBXNativeTarget; buildConfigurationList = 5E435BBB1917A2920028B862 /* Build configuration list for PBXNativeTarget "AspectsDemoOSX" */; buildPhases = ( 5E435B861917A2910028B862 /* Sources */, 5E435B871917A2910028B862 /* Frameworks */, 5E435B881917A2910028B862 /* Resources */, ); buildRules = ( ); dependencies = ( ); name = AspectsDemoOSX; productName = AspectsDemoOSX; productReference = 5E435B8A1917A2910028B862 /* AspectsDemoOSX.app */; productType = "com.apple.product-type.application"; }; 5E435BAA1917A2920028B862 /* AspectsDemoOSXTests */ = { isa = PBXNativeTarget; buildConfigurationList = 5E435BBE1917A2920028B862 /* Build configuration list for PBXNativeTarget "AspectsDemoOSXTests" */; buildPhases = ( 5E435BA71917A2920028B862 /* Sources */, 5E435BA81917A2920028B862 /* Frameworks */, 5E435BA91917A2920028B862 /* Resources */, ); buildRules = ( ); dependencies = ( 5E435BB01917A2920028B862 /* PBXTargetDependency */, ); name = AspectsDemoOSXTests; productName = AspectsDemoOSXTests; productReference = 5E435BAB1917A2920028B862 /* AspectsDemoOSXTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ 5E435B821917A2910028B862 /* Project object */ = { isa = PBXProject; attributes = { CLASSPREFIX = Aspects; LastUpgradeCheck = 0510; ORGANIZATIONNAME = "PSPDFKit GmbH"; TargetAttributes = { 5E435BAA1917A2920028B862 = { TestTargetID = 5E435B891917A2910028B862; }; }; }; buildConfigurationList = 5E435B851917A2910028B862 /* Build configuration list for PBXProject "AspectsDemoOSX" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, Base, ); mainGroup = 5E435B811917A2910028B862; productRefGroup = 5E435B8B1917A2910028B862 /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( 5E435B891917A2910028B862 /* AspectsDemoOSX */, 5E435BAA1917A2920028B862 /* AspectsDemoOSXTests */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ 5E435B881917A2910028B862 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( 5E435B981917A2910028B862 /* InfoPlist.strings in Resources */, 5E435BA61917A2910028B862 /* Images.xcassets in Resources */, 5E435B9E1917A2910028B862 /* Credits.rtf in Resources */, 5E435BA41917A2910028B862 /* MainMenu.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; 5E435BA91917A2920028B862 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( 5E435BB61917A2920028B862 /* InfoPlist.strings in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ 5E435B861917A2910028B862 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 5E435BA11917A2910028B862 /* AspectsAppDelegate.m in Sources */, 5E435B9A1917A2910028B862 /* main.m in Sources */, 5E435BC31917A2BE0028B862 /* Aspects.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 5E435BA71917A2920028B862 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( 78BAEF4C191E3AD5006DABAF /* AspectsDemoTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ 5E435BB01917A2920028B862 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 5E435B891917A2910028B862 /* AspectsDemoOSX */; targetProxy = 5E435BAF1917A2920028B862 /* PBXContainerItemProxy */; }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ 5E435B961917A2910028B862 /* InfoPlist.strings */ = { isa = PBXVariantGroup; children = ( 5E435B971917A2910028B862 /* en */, ); name = InfoPlist.strings; sourceTree = ""; }; 5E435B9C1917A2910028B862 /* Credits.rtf */ = { isa = PBXVariantGroup; children = ( 5E435B9D1917A2910028B862 /* en */, ); name = Credits.rtf; sourceTree = ""; }; 5E435BA21917A2910028B862 /* MainMenu.xib */ = { isa = PBXVariantGroup; children = ( 5E435BA31917A2910028B862 /* Base */, ); name = MainMenu.xib; sourceTree = ""; }; 5E435BB41917A2920028B862 /* InfoPlist.strings */ = { isa = PBXVariantGroup; children = ( 5E435BB51917A2920028B862 /* en */, ); name = InfoPlist.strings; sourceTree = ""; }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ 5E435BB91917A2920028B862 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_ENABLE_OBJC_EXCEPTIONS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; MACOSX_DEPLOYMENT_TARGET = 10.9; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; }; name = Debug; }; 5E435BBA1917A2920028B862 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; ENABLE_NS_ASSERTIONS = NO; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_ENABLE_OBJC_EXCEPTIONS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; MACOSX_DEPLOYMENT_TARGET = 10.9; SDKROOT = macosx; }; name = Release; }; 5E435BBC1917A2920028B862 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemoOSX/AspectsDemoOSX-Prefix.pch"; INFOPLIST_FILE = "AspectsDemoOSX/AspectsDemoOSX-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; name = Debug; }; 5E435BBD1917A2920028B862 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; COMBINE_HIDPI_IMAGES = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemoOSX/AspectsDemoOSX-Prefix.pch"; INFOPLIST_FILE = "AspectsDemoOSX/AspectsDemoOSX-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; name = Release; }; 5E435BBF1917A2920028B862 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/AspectsDemoOSX.app/Contents/MacOS/AspectsDemoOSX"; COMBINE_HIDPI_IMAGES = YES; FRAMEWORK_SEARCH_PATHS = ( "$(DEVELOPER_FRAMEWORKS_DIR)", "$(inherited)", ); GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemoOSX/AspectsDemoOSX-Prefix.pch"; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", ); INFOPLIST_FILE = "AspectsDemoOSXTests/AspectsDemoOSXTests-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; WRAPPER_EXTENSION = xctest; }; name = Debug; }; 5E435BC01917A2920028B862 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/AspectsDemoOSX.app/Contents/MacOS/AspectsDemoOSX"; COMBINE_HIDPI_IMAGES = YES; FRAMEWORK_SEARCH_PATHS = ( "$(DEVELOPER_FRAMEWORKS_DIR)", "$(inherited)", ); GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "AspectsDemoOSX/AspectsDemoOSX-Prefix.pch"; INFOPLIST_FILE = "AspectsDemoOSXTests/AspectsDemoOSXTests-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; WRAPPER_EXTENSION = xctest; }; name = Release; }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ 5E435B851917A2910028B862 /* Build configuration list for PBXProject "AspectsDemoOSX" */ = { isa = XCConfigurationList; buildConfigurations = ( 5E435BB91917A2920028B862 /* Debug */, 5E435BBA1917A2920028B862 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 5E435BBB1917A2920028B862 /* Build configuration list for PBXNativeTarget "AspectsDemoOSX" */ = { isa = XCConfigurationList; buildConfigurations = ( 5E435BBC1917A2920028B862 /* Debug */, 5E435BBD1917A2920028B862 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; 5E435BBE1917A2920028B862 /* Build configuration list for PBXNativeTarget "AspectsDemoOSXTests" */ = { isa = XCConfigurationList; buildConfigurations = ( 5E435BBF1917A2920028B862 /* Debug */, 5E435BC01917A2920028B862 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; rootObject = 5E435B821917A2910028B862 /* Project object */; } ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX.xcodeproj/project.xcworkspace/contents.xcworkspacedata ================================================ ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX.xcodeproj/project.xcworkspace/xcshareddata/AspectsDemoOSX.xccheckout ================================================ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier BE9DB880-2611-435A-9782-57D047D30C1A IDESourceControlProjectName AspectsDemoOSX IDESourceControlProjectOriginsDictionary BFDD7EB6-262A-4289-BF40-2E5B677EEC58 https://github.com/steipete/Aspects.git IDESourceControlProjectPath AspectsDemoOSX/AspectsDemoOSX.xcodeproj/project.xcworkspace IDESourceControlProjectRelativeInstallPathDictionary BFDD7EB6-262A-4289-BF40-2E5B677EEC58 ../../.. IDESourceControlProjectURL https://github.com/steipete/Aspects.git IDESourceControlProjectVersion 110 IDESourceControlProjectWCCIdentifier BFDD7EB6-262A-4289-BF40-2E5B677EEC58 IDESourceControlProjectWCConfigurations IDESourceControlRepositoryExtensionIdentifierKey public.vcs.git IDESourceControlWCCIdentifierKey BFDD7EB6-262A-4289-BF40-2E5B677EEC58 IDESourceControlWCCName Aspects ================================================ FILE: AspectsDemoOSX/AspectsDemoOSX.xcodeproj/xcshareddata/xcschemes/AspectsDemoOSX.xcscheme ================================================ ================================================ FILE: AspectsDemoOSX/AspectsDemoOSXTests/AspectsDemoOSXTests-Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier com.pspdfkit.aspects.${PRODUCT_NAME:rfc1034identifier} CFBundleInfoDictionaryVersion 6.0 CFBundlePackageType BNDL CFBundleShortVersionString 1.0 CFBundleSignature ???? CFBundleVersion 1 ================================================ FILE: AspectsDemoOSX/AspectsDemoOSXTests/en.lproj/InfoPlist.strings ================================================ /* Localized versions of Info.plist keys */ ================================================ FILE: Info-watch.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType FMWK CFBundleShortVersionString 1.0 CFBundleSignature ???? CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass ================================================ FILE: Info.plist ================================================ CFBundleDevelopmentRegion en CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier com.pspdfkit.$(PRODUCT_NAME:rfc1034identifier) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType FMWK CFBundleShortVersionString 1.4.1 CFBundleSignature ???? CFBundleVersion $(CURRENT_PROJECT_VERSION) NSHumanReadableCopyright Copyright (c) 2014 Peter Steinberger. Licensed under the MIT license. NSPrincipalClass ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2014-2015 Peter Steinberger, steipete@gmail.com 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. ================================================ FILE: README.md ================================================ Aspects v1.4.2 🪝 - AOP for Objective-C (10k+ stars) [![Build Status](https://travis-ci.org/steipete/Aspects.svg?branch=master)](https://travis-ci.org/steipete/Aspects) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) ============== A delightful, simple library for aspect oriented programming by [@steipete](http://twitter.com/steipete). **Think of Aspects as method swizzling on steroids. It allows you to add code to existing methods per class or per instance**, whilst thinking of the insertion point e.g. before/instead/after. Aspects automatically deals with calling super and is easier to use than regular method swizzling. Aspects hooks deep into the class hierarchy and creates dynamic subclasses, much like KVO. There's known issues with this approach, and to this date (February 2019) **I STRICTLY DO NOT RECOMMEND TO USE Aspects IN PRODUCTION CODE**. We use it for partial test mocks in, [PSPDFKit, an iOS PDF framework that ships with apps like Dropbox or Evernote](http://pspdfkit.com), it's also very useful for quickly hacking something up. Aspects uses `_objc_msgForward` which causes issues with other code that uses message forwarding. Aspects extends `NSObject` with the following methods: ``` objc /// Adds a block of code before/instead/after the current `selector` for a specific class. /// /// @param block Aspects replicates the type signature of the method being hooked. /// The first parameter will be `id`, followed by all parameters of the method. /// These parameters are optional and will be filled to match the block signature. /// You can even use an empty block, or one that simple gets `id`. /// /// @note Hooking static methods is not supported. /// @return A token which allows to later deregister the aspect. + (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; /// Adds a block of code before/instead/after the current `selector` for a specific instance. - (id)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; /// Deregister an aspect. /// @return YES if deregistration is successful, otherwise NO. id aspect = ...; [aspect remove]; ``` Adding aspects returns an opaque token of type `AspectToken` which can be used to deregister again. All calls are thread-safe. Aspects uses Objective-C message forwarding to hook into messages. This will create some overhead. Don't add aspects to methods that are called a lot. Aspects is meant for view/controller code that is not called 1000 times per second. Aspects calls and matches block arguments. Blocks without arguments are supported as well. The first block argument will be of type `id`. When to use Aspects ------------------- Aspect-oriented programming (AOP) is used to encapsulate "cross-cutting" concerns. These are the kind of requirements that *cut-across* many modules in your system, and so cannot be encapsulated using normal object oriented programming. Some examples of these kinds of requirements: * *Whenever* a user invokes a method on the service client, security should be checked. * *Whenever* a user interacts with the store, a genius suggestion should be presented, based on their interaction. * *All* calls should be logged. If we implemented the above requirements using regular OOP there'd be some drawbacks: Good OOP says a class should have a single responsibility, however adding on extra *cross-cutting* requirements means a class that is taking on other responsibilites. For example you might have a **StoreClient** that is supposed to be all about making purchases from an online store. Add in some cross-cutting requirements and it might also have to take on the roles of logging, security and recommendations. This is not great because: * Our StoreClient is now harder to understand and maintain. * These cross-cutting requirements are duplicated and spread throughout our app. AOP lets us modularize these cross-cutting requirements, and then cleanly identify all of the places they should be applied. As shown in the examples above cross-cutting requirements can be either technical or business focused in nature. ## Here are some concrete examples: Aspects can be used to **dynamically add logging** for debug builds only: ``` objc [UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo, BOOL animated) { NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated); } error:NULL]; ``` ------------------- It can be used to greatly simplify your analytics setup: https://github.com/orta/ARAnalytics ------------------- You can check if methods are really being called in your test cases: ``` objc - (void)testExample { TestClass *testClass = [TestClass new]; TestClass *testClass2 = [TestClass new]; __block BOOL testCallCalled = NO; [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^{ testCallCalled = YES; } error:NULL]; [testClass2 testCallAndExecuteBlock:^{ [testClass testCall]; } error:NULL]; XCTAssertTrue(testCallCalled, @"Calling testCallAndExecuteBlock must call testCall"); } ``` ------------------- It can be really useful for debugging. Here I was curious when exactly the tap gesture changed state: ``` objc [_singleTapGesture aspect_hookSelector:@selector(setState:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo) { NSLog(@"%@: %@", aspectInfo.instance, aspectInfo.arguments); } error:NULL]; ``` ------------------- Another convenient use case is adding handlers for classes that you don't own. I've written it for use in [PSPDFKit](http://pspdfkit.com), where we require notifications when a view controller is being dismissed modally. This includes UIKit view controllers like `MFMailComposeViewController` and `UIImagePickerController`. We could have created subclasses for each of these controllers, but this would be quite a lot of unnecessary code. Aspects gives you a simpler solution for this problem: ``` objc @implementation UIViewController (DismissActionHook) // Will add a dismiss action once the controller gets dismissed. - (void)pspdf_addWillDismissAction:(void (^)(void))action { PSPDFAssert(action != NULL); [self aspect_hookSelector:@selector(viewWillDisappear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo) { if ([aspectInfo.instance isBeingDismissed]) { action(); } } error:NULL]; } @end ``` Debugging --------- Aspects identifies itself nicely in the stack trace, so it's easy to see if a method has been hooked: Using Aspects with non-void return types ---------------------------------------- You can use the invocation object to customize the return value: ``` objc [PSPDFDrawView aspect_hookSelector:@selector(shouldProcessTouches:withEvent:) withOptions:AspectPositionInstead usingBlock:^(id info, NSSet *touches, UIEvent *event) { // Call original implementation. BOOL processTouches; NSInvocation *invocation = info.originalInvocation; [invocation invoke]; [invocation getReturnValue:&processTouches]; if (processTouches) { processTouches = pspdf_stylusShouldProcessTouches(touches, event); [invocation setReturnValue:&processTouches]; } } error:NULL]; ``` Installation ------------ The simplest option is to use `pod "Aspects"`. You can also add the two files `Aspects.h/m` to your project. There are no further requirements. Compatibility and Limitations ----------------------------- Aspects uses quite some runtime trickery to achieve what it does. You can mostly mix this with regular method swizzling. An important limitation is that for class-based hooking, a method can only be hooked once within the subclass hierarchy. [See #2](https://github.com/steipete/Aspects/issues/2) This does not apply for objects that are hooked. Aspects creates a dynamic subclass here and has full control. KVO works if observers are created after your calls `aspect_hookSelector:` It most likely will crash the other way around. Still looking for workarounds here - any help appreciated. Because of ugly implementation details on the ObjC runtime, methods that return unions that also contain structs might not work correctly unless this code runs on the arm64 runtime. Credits ------- The idea to use `_objc_msgForward` and parts of the `NSInvocation` argument selection is from the excellent [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) from the GitHub guys. [This article](http://codeshaker.blogspot.co.at/2012/01/aop-delivered.html) explains how it works under the hood. Supported iOS & SDK Versions ----------------------------- * Aspects requires ARC. * Aspects is tested with iOS 7+ and OS X 10.7 or higher. License ------- MIT licensed, Copyright (c) 2014 Peter Steinberger, steipete@gmail.com, [@steipete](http://twitter.com/steipete) Release Notes ----------------- Version 1.4.2 - Allow to hook different subclasses. - Smaller tweaks. Version 1.4.1 - Rename error codes. Version 1.4.0 - Add support for block signatures that match method signatures. (thanks to @nickynick) Version 1.3.1 - Add support for OS X 10.7 or higher. (thanks to @ashfurrow) Version 1.3.0 - Add automatic deregistration. - Checks if the selector exists before trying to hook. - Improved dealloc hooking. (no more unsafe_unretained needed) - Better examples. - Always log errors. Version 1.2.0 - Adds error parameter. - Improvements in subclassing registration tracking. Version 1.1.0 - Renamed the files from NSObject+Aspects.m/h to just Aspects.m/h. - Removing now works via calling `remove` on the aspect token. - Allow hooking dealloc. - Fixes infinite loop if the same method is hooked for multiple classes. Hooking will only work for one class in the hierarchy. - Additional checks to prevent things like hooking retain/release/autorelease or forwardInvocation: - The original implementation of forwardInvocation is now correctly preserved. - Classes are properly cleaned up and restored to the original state after the last hook is deregistered. - Lots and lots of new test cases! Version 1.0.1 - Minor tweaks and documentation improvements. Version 1.0.0 - Initial release