23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
-
-
-
|
#include <string.h>
#include <unistd.h>
#include <assert.h>
#import "OFObject.h"
#import "OFArray.h"
#import "OFSet.h"
#import "OFIntrospection.h"
#import "OFAutoreleasePool.h"
#import "OFAllocFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFMemoryNotPartOfObjectException.h"
|
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
@throw [OFNotImplementedException newWithClass: self
selector: _cmd];
#endif
}
+ (void)inheritMethodsFromClass: (Class)class
{
OFAutoreleasePool *pool;
OFMutableSet *classMethods, *instanceMethods;
Class superclass = [self superclass];
OFIntrospection *introspection;
OFMethod **cArray;
size_t i, count;
if ([self isSubclassOfClass: class])
return;
pool = [[OFAutoreleasePool alloc] init];
#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)
classMethods = [OFMutableSet set];
instanceMethods = [OFMutableSet set];
Method *methodList;
introspection = [OFIntrospection introspectionWithClass: self];
unsigned i, count;
cArray = [[introspection classMethods] cArray];
count = [[introspection classMethods] count];
for (i = 0; i < count; i++)
[classMethods addObject: [cArray[i] name]];
methodList = class_copyMethodList(((OFObject*)class)->isa, &count);
cArray = [[introspection instanceMethods] cArray];
count = [[introspection instanceMethods] count];
@try {
for (i = 0; i < count; i++)
[instanceMethods addObject: [cArray[i] name]];
introspection = [OFIntrospection introspectionWithClass: class];
cArray = [[introspection classMethods] cArray];
count = [[introspection classMethods] count];
for (i = 0; i < count; i++) {
SEL selector;
IMP implementation;
for (i = 0; i < count; i++) {
SEL selector = method_getName(methodList[i]);
IMP implementation;
/*
* Don't replace methods implemented in receiving class.
*/
if ([classMethods containsObject: [cArray[i] name]])
continue;
if ([self methodForSelector: selector] !=
[superclass methodForSelector: selector])
continue;
selector = [cArray[i] selector];
implementation = [class methodForSelector: selector];
implementation = [class methodForSelector: selector];
if ([self respondsToSelector: selector])
[self setImplementation: implementation
forClassMethod: selector];
else
[self addClassMethod: selector
withTypeEncoding: [cArray[i] typeEncoding]
implementation: implementation];
}
cArray = [[introspection instanceMethods] cArray];
if ([self respondsToSelector: selector])
[self setImplementation: implementation
forClassMethod: selector];
else {
const char *typeEncoding =
method_getTypeEncoding(methodList[i]);
[self addClassMethod: selector
withTypeEncoding: typeEncoding
implementation: implementation];
}
}
} @finally {
free(methodList);
count = [[introspection instanceMethods] count];
for (i = 0; i < count; i++) {
SEL selector;
IMP implementation;
}
methodList = class_copyMethodList(class, &count);
@try {
for (i = 0; i < count; i++) {
SEL selector = method_getName(methodList[i]);
IMP implementation;
/*
* Don't replace methods implemented in receiving class.
*/
if ([self instanceMethodForSelector: selector] !=
if ([instanceMethods containsObject: [cArray[i] name]])
continue;
[superclass instanceMethodForSelector: selector])
continue;
implementation =
[class instanceMethodForSelector: selector];
if ([self instancesRespondToSelector: selector])
[self setImplementation: implementation
forInstanceMethod: selector];
else {
const char *typeEncoding =
method_getTypeEncoding(methodList[i]);
[self addInstanceMethod: selector
withTypeEncoding: typeEncoding
implementation: implementation];
}
}
} @finally {
free(methodList);
}
#elif defined(OF_OLD_GNU_RUNTIME)
MethodList_t methodList;
for (methodList = class->class_pointer->methods;
methodList != NULL; methodList = methodList->method_next) {
int i;
for (i = 0; i < methodList->method_count; i++) {
SEL selector = methodList->method_list[i].method_name;
IMP implementation;
/*
* Don't replace methods implemented in receiving class.
*/
if ([self instanceMethodForSelector: selector] !=
[superclass instanceMethodForSelector: selector])
continue;
implementation =
[class instanceMethodForSelector: selector];
if ([self instancesRespondToSelector: selector])
[self setImplementation: implementation
selector = [cArray[i] selector];
implementation = [class instanceMethodForSelector: selector];
forClassMethod: selector];
else {
const char *typeEncoding =
methodList->method_list[i].method_types;
[self addClassMethod: selector
withTypeEncoding: typeEncoding
implementation: implementation];
}
}
}
for (methodList = class->methods; methodList != NULL;
methodList = methodList->method_next) {
int i;
for (i = 0; i < methodList->method_count; i++) {
SEL selector = methodList->method_list[i].method_name;
IMP implementation;
/*
* Don't replace methods implemented in receiving class.
*/
if ([self instanceMethodForSelector: selector] !=
[superclass instanceMethodForSelector: selector])
continue;
implementation =
[class instanceMethodForSelector: selector];
if ([self instancesRespondToSelector: selector])
[self setImplementation: implementation
forInstanceMethod: selector];
else
[self addInstanceMethod: selector
withTypeEncoding: [cArray[i] typeEncoding]
implementation: implementation];
}
[pool release];
if ([self instancesRespondToSelector: selector])
[self setImplementation: implementation
forInstanceMethod: selector];
else {
const char *typeEncoding =
methodList->method_list[i].method_types;
[self addInstanceMethod: selector
withTypeEncoding: typeEncoding
implementation: implementation];
}
}
}
#else
@throw [OFNotImplementedException newWithClass: self
selector: _cmd];
#endif
[self inheritMethodsFromClass: [class superclass]];
}
- init
{
return self;
|