Skip to content

Commit

Permalink
Merge pull request #2 from Ahruman/jrswizzle
Browse files Browse the repository at this point in the history
---

Deleted aliasMethod... methods, which dont compile for Tiger.
  • Loading branch information
rentzsch committed Feb 6, 2011
2 parents 3ae716b + 4379f60 commit 471314a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 54 deletions.
9 changes: 4 additions & 5 deletions JRSwizzle.h
Expand Up @@ -2,18 +2,17 @@
JRSwizzle.h
Copyright (c) 2007 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
Some rights reserved: <http://opensource.org/licenses/mit-license.php>
Modifications by Jens Ayton:
* Removed alias method family (pre ObjC2 version was broken)
***************************************************************************/

#import <Foundation/Foundation.h>

@interface NSObject (JRSwizzle)

+ (BOOL)jr_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError**)error_;
+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_;

+ (BOOL)jr_aliasMethod:(SEL)methSel_ withName:(const char*)aliasName_ error:(NSError**)error_;
+ (BOOL)jr_aliasMethod:(SEL)methSel_ withSelector:(SEL)aliasSel_ error:(NSError**)error_;

+ (BOOL)jr_aliasClassMethod:(SEL)methSel_ withName:(const char*)aliasName_ error:(NSError**)error_;
+ (BOOL)jr_aliasClassMethod:(SEL)methSel_ withSelector:(SEL)aliasSel_ error:(NSError**)error_;
@end
56 changes: 7 additions & 49 deletions JRSwizzle.m
Expand Up @@ -17,6 +17,12 @@
}
#define SetNSError(ERROR_VAR, FORMAT,...) SetNSErrorFor(__func__, ERROR_VAR, FORMAT, ##__VA_ARGS__)

#if OBJC_API_VERSION >= 2
#define GetClass(obj) object_getClass(obj)
#else
#define GetClass(obj) (obj ? obj->isa : Nil)
#endif

@implementation NSObject (JRSwizzle)

+ (BOOL)jr_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError**)error_ {
Expand Down Expand Up @@ -110,55 +116,7 @@ + (BOOL)jr_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError**)
}

+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_ {
return [object_getClass((id)self) jr_swizzleMethod:origSel_ withMethod:altSel_ error:error_];
}


+ (BOOL)jr_aliasMethod:(SEL)methSel_ withSelector:(SEL)aliasSel_ error:(NSError**)error_ {
Method method = class_getInstanceMethod(self, methSel_);
if (!method) {
SetNSError(error_, @"method %@ not found for class %@", NSStringFromSelector(methSel_), NSStringFromClass(self));
return NO;
}
Method otherMethod = class_getInstanceMethod(self, aliasSel_);
if (otherMethod) {
SetNSError(error_, @"method -[%@ %@] already exists; won't alias to -%@", NSStringFromClass(self), NSStringFromSelector(aliasSel_), NSStringFromSelector(methSel_));
return NO;
}

#if OBJC_API_VERSION >= 2
class_addMethod(self,
aliasSel_,
class_getMethodImplementation(self, methSel_),
method_getTypeEncoding(method));
return YES;
#else
struct objc_method_list *alias_list = malloc(sizeof(struct objc_method_list) + (sizeof(struct objc_method)));
alias_list->obsolete = NULL; // soothe valgrind - apparently ObjC runtime accesses this value and it shows as uninitialized in valgrind
alias_list->method_count = 1;
alias_list->method_list = alias_method;

Method alias_method = hoisted_method_list->method_list;
bcopy(method, alias_method, sizeof(struct objc_method));
alias_method->method_name = aliasSel_;

class_addMethods(self, alias_list);

return YES;
#endif
}

/* TODO: fix error generation so that these methods, rather than jr_aliasMethod:withSelector:error:,
will be reported as the method name in errors
*/
+ (BOOL)jr_aliasMethod:(SEL)methSel_ withName:(const char*)aliasName_ error:(NSError**)error_ {
return [self jr_aliasMethod:methSel_ withSelector:sel_registerName(aliasName_) error:error_];
}
+ (BOOL)jr_aliasClassMethod:(SEL)methSel_ withName:(const char*)aliasName_ error:(NSError**)error_ {
return [object_getClass((id)self) jr_aliasMethod:methSel_ withSelector:sel_registerName(aliasName_) error:error_];
}
+ (BOOL)jr_aliasClassMethod:(SEL)methSel_ withSelector:(SEL)aliasSel_ error:(NSError**)error_ {
return [object_getClass((id)self) jr_aliasMethod:methSel_ withSelector:aliasSel_ error:error_];
return [GetClass((id)self) jr_swizzleMethod:origSel_ withMethod:altSel_ error:error_];
}

@end

0 comments on commit 471314a

Please sign in to comment.