]> err.no Git - sope/blob - sope-appserver/NGXmlRpc/NSObject+Reflection.m
fixed build with gstep-make 1.9.2
[sope] / sope-appserver / NGXmlRpc / NSObject+Reflection.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "common.h"
24 #import <objc/objc.h>
25 #import <objc/objc-api.h>
26 #import <objc/Protocol.h>
27
28 #if NeXT_RUNTIME || APPLE_RUNTIME
29 #  import <objc/objc.h>
30 #  import <objc/objc-class.h>
31 #else
32 #  import <objc/encoding.h>
33 #endif
34
35 @implementation NSObject(Reflection)
36
37 + (void)_addSelectorsOfClassToArray:(NSMutableSet *)_sels {
38 #if GNU_RUNTIME
39   MethodList_t methods;
40   
41   for (methods = ((Class)self)->methods; methods;
42        methods = methods->method_next) {
43     int i;
44     
45     for (i = 0; i < methods->method_count; i++) {
46       Method_t internalMethod;
47       SEL      sel;
48       NSString *selName;
49       
50       internalMethod = &(methods->method_list[i]);
51       sel     = internalMethod->method_name;
52       selName = NSStringFromSelector(sel);
53       
54       if ([selName length] == 0) {
55         NSLog(@"WARNING(%s): did not get selector for method 0x%08X",
56               __PRETTY_FUNCTION__, internalMethod);
57         continue;
58       }
59       
60       [_sels addObject:selName];
61     }
62   }
63 #else
64   struct objc_method_list *mlist;
65   void *iterator = NULL;
66   
67   //NSLog(@"adding selectors of class: %@", NSStringFromClass(self));
68   
69   while ((mlist = class_nextMethodList(self, &iterator)) != NULL) {
70     int mcount;
71     
72     //NSLog(@"  processing %i selectors ...", mlist->method_count);
73     
74     for (mcount = mlist->method_count; mcount > 0; mcount--) {
75       NSString *selName;
76       SEL sel;
77       
78       if ((sel = mlist->method_list[mcount - 1].method_name) == NULL)
79         continue;
80       
81       selName = NSStringFromSelector(sel);
82       if ([selName length] == 0) {
83         NSLog(@"WARNING(%s): did not get selector for method 0x%08X",
84               __PRETTY_FUNCTION__, mlist->method_list[mcount - 1]);
85         continue;
86       }
87       [_sels addObject:selName];
88     }
89   }
90 #endif
91 }
92
93 + (NSArray *)classImplementsSelectors {
94   NSMutableSet *a;
95   
96   a = [[[NSMutableSet alloc] initWithCapacity:32] autorelease];
97   [self _addSelectorsOfClassToArray:a];
98   return [a allObjects];
99 }
100
101 + (NSArray *)instancesRespondToSelectors {
102   NSMutableSet *a;
103   Class clazz;
104   
105   a = [NSMutableSet setWithCapacity:128];
106   for (clazz = self; clazz; clazz = [clazz superclass])
107     [clazz _addSelectorsOfClassToArray:a];
108   return [a allObjects];
109 }
110
111 - (NSArray *)respondsToSelectors {
112   return [[self class] instancesRespondToSelectors];
113 }
114
115 @end /* NSObject(Reflection) */