steipete / Aspects

Delightful, simple library for aspect oriented programming in Objective-C and Swift.
https://twitter.com/steipete
MIT License
8.4k stars 1.26k forks source link

Find a bug and a serious bug #123

Open youlianchun opened 7 years ago

youlianchun commented 7 years ago

bug:The interception of class methods and instance methods is not possible. Defect: instead of replacing the original method body with AspectPositionInstead, there is nothing you can do if the original method calls the super method. So let's say I'm going to replace it with a -- [UIViewController viewDidLoad:] method. and I have an idea.

//class: @interface ObjectS : NSObject -(void)function:(NSString)str; -(void)function2:(NSString)str; @end @implementation ObjectS -(void)function:(NSString)str { NSLog(@"super"); [self function2:str]; } -(void)function2:(NSString)str { NSLog(@"%s %@", func, str); } @end

@interface Object : ObjectS -(void)function:(NSString)str; -(void)function2:(NSString)str; +(void)classFunction:(NSString*)str; @end

@implementation Object -(void)function:(NSString)str { [super function:str]; NSLog(@"%s %@", func, str); } -(void)function2:(NSString)str { [super function2:str]; NSLog(@"%s %@", func, str); } +(void)classFunction:(NSString*)str { NSLog(@"%s %@", func, str); } @end

The test code, [intercept 1] and [intercept 2], is not valid. Single execution [intercept 1] - [Object function2:] cannot be executed (replace the original execution process, which can not be replaced by a single execution - [Object function:] comparison) code is as follows: Object * obj = [[Object alloc] init];

//intercept 1

[obj aspect_hookSelector:@selector(function:) withOptions:AspectPositionInstead usingBlock:^(NSString*str){ NSLog(@"object"); } error:NULL]; [obj function:@"1"];

//intercept 2

[[Object class] aspect_hookSelector:@selector(classFunction:) withOptions:AspectPositionAfter usingBlock:^(NSString*str){ NSLog(@"Object"); } error:NULL]; [Object classFunction:@"2"];