]> err.no Git - sope/blob - sope-appserver/NGXmlRpc/NSObject+Reflection.m
fixed copyrights for 2005
[sope] / sope-appserver / NGXmlRpc / NSObject+Reflection.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "common.h"
23 #import <objc/objc.h>
24 #import <objc/objc-api.h>
25 #import <objc/Protocol.h>
26
27 #if NeXT_RUNTIME || APPLE_RUNTIME
28 #  import <objc/objc.h>
29 #  import <objc/objc-class.h>
30 #else
31 #  import <objc/encoding.h>
32 #endif
33
34 @implementation NSObject(Reflection)
35
36 + (void)_addSelectorsOfClassToArray:(NSMutableSet *)_sels {
37 #if GNU_RUNTIME
38   MethodList_t methods;
39   
40   for (methods = ((Class)self)->methods; methods;
41        methods = methods->method_next) {
42     int i;
43     
44     for (i = 0; i < methods->method_count; i++) {
45       Method_t internalMethod;
46       SEL      sel;
47       NSString *selName;
48       
49       internalMethod = &(methods->method_list[i]);
50       sel     = internalMethod->method_name;
51       selName = NSStringFromSelector(sel);
52       
53       if ([selName length] == 0) {
54         NSLog(@"WARNING(%s): did not get selector for method 0x%08X",
55               __PRETTY_FUNCTION__, internalMethod);
56         continue;
57       }
58       
59       [_sels addObject:selName];
60     }
61   }
62 #else
63   struct objc_method_list *mlist;
64   void *iterator = NULL;
65   
66   //NSLog(@"adding selectors of class: %@", NSStringFromClass(self));
67   
68   while ((mlist = class_nextMethodList(self, &iterator)) != NULL) {
69     int mcount;
70     
71     //NSLog(@"  processing %i selectors ...", mlist->method_count);
72     
73     for (mcount = mlist->method_count; mcount > 0; mcount--) {
74       NSString *selName;
75       SEL sel;
76       
77       if ((sel = mlist->method_list[mcount - 1].method_name) == NULL)
78         continue;
79       
80       selName = NSStringFromSelector(sel);
81       if ([selName length] == 0) {
82         NSLog(@"WARNING(%s): did not get selector for method 0x%08X",
83               __PRETTY_FUNCTION__, mlist->method_list[mcount - 1]);
84         continue;
85       }
86       [_sels addObject:selName];
87     }
88   }
89 #endif
90 }
91
92 + (NSArray *)classImplementsSelectors {
93   NSMutableSet *a;
94   
95   a = [[[NSMutableSet alloc] initWithCapacity:32] autorelease];
96   [self _addSelectorsOfClassToArray:a];
97   return [a allObjects];
98 }
99
100 + (NSArray *)instancesRespondToSelectors {
101   NSMutableSet *a;
102   Class clazz;
103   
104   a = [NSMutableSet setWithCapacity:128];
105   for (clazz = self; clazz; clazz = [clazz superclass])
106     [clazz _addSelectorsOfClassToArray:a];
107   return [a allObjects];
108 }
109
110 - (NSArray *)respondsToSelectors {
111   return [[self class] instancesRespondToSelectors];
112 }
113
114 @end /* NSObject(Reflection) */