From 2df37435d0126ae9cc627fa13d99517ce2951a5b Mon Sep 17 00:00:00 2001 From: CaS Date: Sun, 2 Mar 2003 10:17:24 +0000 Subject: [PATCH] Trivial functions added. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@16106 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 1 + Headers/gnustep/base/GSObjCRuntime.h | 2 + Source/Additions/GSObjCRuntime.m | 122 ++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 9e2a52f5c..7112af852 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,7 @@ and consistency. Moved implementation from NSObject.m to GSObjCRuntime.m so this is available for use withing the Additions library when built standalone. + Add a couple of trivial functions to list ivars and methods. 2003-02-19 Mirko Viviani diff --git a/Headers/gnustep/base/GSObjCRuntime.h b/Headers/gnustep/base/GSObjCRuntime.h index 7f65210f4..e2d8b8eb2 100644 --- a/Headers/gnustep/base/GSObjCRuntime.h +++ b/Headers/gnustep/base/GSObjCRuntime.h @@ -79,6 +79,8 @@ GS_EXPORT void GSObjCGetVariable(id obj, int offset, unsigned int size, void *data); GS_EXPORT void GSObjCSetVariable(id obj, int offset, unsigned int size, const void *data); +GS_EXPORT NSArray* GSObjCMethodNames(id obj); +GS_EXPORT NSArray* GSObjCVariableNames(id obj); GS_EXPORT void GSObjCAddClassBehavior(Class receiver, Class behavior); diff --git a/Source/Additions/GSObjCRuntime.m b/Source/Additions/GSObjCRuntime.m index dfc6ba86d..83b604270 100644 --- a/Source/Additions/GSObjCRuntime.m +++ b/Source/Additions/GSObjCRuntime.m @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #else @@ -69,7 +70,8 @@ GSObjCFindVariable(id obj, const char *name, struct objc_ivar_list *ivars; struct objc_ivar *ivar = 0; - class = [obj class]; + if (obj == nil) return NO; + class = GSObjCClass(obj); while (class != nil && ivar == 0) { ivars = class->ivars; @@ -102,6 +104,124 @@ GSObjCFindVariable(id obj, const char *name, return YES; } +/** + * This method returns an array listing the names of all the + * instance methods available to obj, whether they + * belong to the class of obj or one of its superclasses.
+ * If obj is a class, this returns the class methods.
+ * Returns nil if obj is nil. + */ +NSArray* +GSObjCMethodNames(id obj) +{ + NSMutableSet *set; + NSArray *array; + Class class; + struct objc_method_list *methods; + + if (obj == nil) + { + return nil; + } + /* + * Add names to a set so methods declared in superclasses + * and then overridden do not appear more than once. + */ + set = [[NSMutableSet alloc] initWithCapacity: 32]; + + class = GSObjCClass(obj); + + while (class != nil) + { +#ifdef NeXT_RUNTIME + void *iterator = 0; + + while ((methods = class_nextMethodList(class, &iterator)) ) + { + int i; + + for (i = 0; i < methods->method_count; i++) + { + struct objc_method *method = &methods->method_list[i]; + + if (method->method_name != 0) + { + NSString *name; + + name = [[NSString alloc] initWithUTF8String: + method->method_name]; + [set addObject: name]; + RELEASE(name); + } + } + } +#else + methods = class->methods; + if (methods != 0) + { + int i; + + for (i = 0; i < methods->method_count; i++) + { + NSString *name; + + name = [[NSString alloc] initWithUTF8String: + sel_get_name(methods->method_list[i].method_name)]; + [set addObject: name]; + RELEASE(name); + } + methods = methods->method_next; + } +#endif + class = class->super_class; + } + + array = [set allObjects]; + RELEASE(set); + return array; +} + +/** + * This method returns an array listing the names of all the + * instance variables present in the instance obj, whether they + * belong to the class of obj or one of its superclasses.
+ * Returns nil if obj is nil. + */ +NSArray* +GSObjCVariableNames(id obj) +{ + NSMutableArray *array; + Class class; + struct objc_ivar_list *ivars; + + if (obj == nil) + { + return nil; + } + array = [NSMutableArray arrayWithCapacity: 16]; + class = GSObjCClass(obj); + while (class != nil) + { + ivars = class->ivars; + if (ivars != 0) + { + int i; + + for (i = 0; i < ivars->ivar_count; i++) + { + NSString *name; + + name = [[NSString alloc] initWithUTF8String: + ivars->ivar_list[i].ivar_name]; + [array addObject: name]; + RELEASE(name); + } + } + class = class->super_class; + } + return array; +} + /** Deprecated ... use GSObjCGetVariable() */ void GSGetVariable(id obj, int offset, unsigned int size, void *data)