Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -396,12 +396,15 @@ /** * \brief Adds all instance methods from the specified class to the class that * is the receiver. * - * Existing methods will not be overriden, so that it behaves similar to normal - * inheritance. + * Methods implemented by the receiving class itself will not be overridden, + * however methods implemented by its superclass will. Therefore it behaves + * similar as if the specified class is the superclass of the receiver. + * + * The specified class may not use instance variables and has to use accessors. * * \param class The class from which the instance methods should be inherited */ + (void)inheritInstanceMethodsFromClass: (Class)class; Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -26,10 +26,11 @@ #include #import "OFObject.h" #import "OFArray.h" +#import "OFSet.h" #import "OFIntrospection.h" #import "OFAutoreleasePool.h" #import "OFAllocFailedException.h" #import "OFEnumerationMutationException.h" @@ -463,28 +464,43 @@ } + (void)inheritInstanceMethodsFromClass: (Class)class { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFMutableSet *set = [OFMutableSet set]; OFIntrospection *introspection; OFMethod **cArray; size_t i, count; + + introspection = [OFIntrospection introspectionWithClass: self]; + cArray = [[introspection instanceMethods] cArray]; + count = [[introspection instanceMethods] count]; + + for (i = 0; i < count; i++) + [set addObject: [cArray[i] name]]; introspection = [OFIntrospection introspectionWithClass: class]; cArray = [[introspection instanceMethods] cArray]; count = [[introspection instanceMethods] count]; for (i = 0; i < count; i++) { SEL selector; IMP implementation; + + if ([set containsObject: [cArray[i] name]]) + continue; selector = [cArray[i] selector]; implementation = [class instanceMethodForSelector: selector]; - [self addInstanceMethod: selector - withTypeEncoding: [cArray[i] typeEncoding] - implementation: implementation]; + if ([self respondsToSelector: selector]) + [self setImplementation: implementation + forInstanceMethod: selector]; + else + [self addInstanceMethod: selector + withTypeEncoding: [cArray[i] typeEncoding] + implementation: implementation]; } [pool release]; }