Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -58,10 +58,20 @@ * * \return A boolean whether the class conforms to the specified protocol */ + (BOOL)conformsTo: (Protocol*)protocol; +/** + * Replace a method implementation with another implementation. + * + * \param selector The selector of the method to replace + * \param imp The new implementation for the method + * \return The old implementation + */ ++ (IMP)setImplementation: (IMP)newimp + forMethod: (SEL)selector; + /** * Replace a method with a method from another class. * * \param selector The selector of the method to replace * \param class The class from which the new method should be taken Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -120,25 +120,22 @@ return NO; #endif } -+ (IMP)replaceMethod: (SEL)selector - withMethodFromClass: (Class)class; ++ (IMP)setImplementation: (IMP)newimp + forMethod: (SEL)selector { #ifdef __objc_INCLUDE_GNU Method_t method = class_get_instance_method(self, selector); - IMP oldimp, newimp; + IMP oldimp; if (method == NULL) @throw [OFInvalidArgumentException newWithClass: self andSelector: _cmd]; - oldimp = method_get_imp(method); - newimp = method_get_imp(class_get_instance_method(class, selector)); - - if (oldimp == (IMP)0 || newimp == (IMP)0) + if ((oldimp = method_get_imp(method)) == (IMP)0 || newimp == (IMP)0) @throw [OFInvalidArgumentException newWithClass: self andSelector: _cmd]; method->method_imp = newimp; @@ -148,19 +145,33 @@ sarray_at_put_safe(((Class)self)->dtable, (sidx)method->method_name->sel_id, method->method_imp); return oldimp; #else - Method method = class_getInstanceMethod(self, selector); - IMP imp = class_getMethodImplementation(class, selector); + Method method; - if (method == NULL || imp == NULL) + if ((method = class_getInstanceMethod(self, selector)) == NULL) @throw [OFInvalidArgumentException newWithClass: self andSelector: _cmd]; - return method_setImplementation(method, imp); + return method_setImplementation(method, newimp); +#endif +} + ++ (IMP)replaceMethod: (SEL)selector + withMethodFromClass: (Class)class; +{ + IMP newimp; + +#ifdef __objc_INCLUDE_GNU + newimp = method_get_imp(class_get_instance_method(class, selector)); +#else + newimp = class_getMethodImplementation(class, selector); #endif + + return [self setImplementation: newimp + forMethod: selector]; } - init { return self;