Skip to content

Instantly share code, notes, and snippets.

@tmiz
Created December 20, 2011 03:13
Show Gist options
  • Save tmiz/1500069 to your computer and use it in GitHub Desktop.
Save tmiz/1500069 to your computer and use it in GitHub Desktop.
interposing on objc sample
// (C)2011 to3oo mi2ukam1 tmiz.net
//
// interposing on objc sample
// $ g++ main.m -framework cocoa
// $ ./a.out
// 2011-12-20 12:12:37.159 a.out[533:903] hello
// 2011-12-20 12:12:37.164 a.out[533:903] world
#include <cocoa/cocoa.h>
#include <objc/objc-class.h>
// --- example instead of existing class
@interface AObject : NSObject {
};
-(void)sayHello;
@end
@implementation AObject
-(void)sayHello
{
NSLog(@"hello");
}
@end
//
@implementation AObject(DYNA)
-(void)sayHello_new
{
[self sayHello_new];
NSLog(@"world");
}
@end
void interpose()
{
Class aobj_class = objc_getClass("AObject");
Method a = class_getInstanceMethod(aobj_class,
@selector(sayHello));
Method b = class_getInstanceMethod(aobj_class,
@selector(sayHello_new));
method_exchangeImplementations(a, b);
}
int main() {
interpose();
id aobj = [[AObject alloc] init];
[aobj sayHello];
[aobj release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment