knowledge-database (beta)

Current group: comp.lang.objective-c

Reflection Support

Reflection Support  
bazad
 Re: Reflection Support  
Chris Hanson
 Re: Reflection Support  
bazad
 Re: Reflection Support  
Tilo_Prütz
 Re: Reflection Support  
Sherm Pendley
 Re: Reflection Support  
bazad
 Re: Reflection Support  
Sherm Pendley
 Re: Reflection Support  
bazad
 Re: Reflection Support  
Sherm Pendley
 Re: Reflection Support  
Tilo_Prütz
 Re: Reflection Support  
Philippe Mougin
From:bazad
Subject:Reflection Support
Date:05 Nov 2004 12:23:50 -0800
Does Objective-C support reflection?
From:Chris Hanson
Subject:Re: Reflection Support
Date:Sun, 07 Nov 2004 23:25:16 GMT
On 2004-11-05 12:23:50 -0800, bazad said:
> Does Objective-C support reflection?

Do you mean introspection? Objective-C implementations typically have
a low-level API for accessing the language runtime, and frameworks such
as Cocoa typically layer a higher-level API on top of that.

For example, in Cocoa, you can ask an object if it responds to a
particular selector, for the function pointer (IMP) corresponding to a
particular method, etc. You can construct method invocations at
runtime and invoke them. And so on.

So yes, it supports what Java calls "reflection" very well.

-- Chris
From:bazad
Subject:Re: Reflection Support
Date:18 Nov 2004 09:53:59 -0800
Chris Hanson writes:

> On 2004-11-05 12:23:50 -0800, bazad said:
> > Does Objective-C support reflection?
>
> Do you mean introspection? Objective-C implementations typically have
> a low-level API for accessing the language runtime, and frameworks
> such as Cocoa typically layer a higher-level API on top of that.
>
> For example, in Cocoa, you can ask an object if it responds to a
> particular selector, for the function pointer (IMP) corresponding to a
> particular method, etc. You can construct method invocations at
> runtime and invoke them. And so on.
>
> So yes, it supports what Java calls "reflection" very well.
>
> -- Chris

Yes, I found that introspection is supported for the real life cases.

I had an idea of writing a small console tool to print out the list of
instance methods with their signatures. It is more of the exercise,
than a practical application. The application is sort of an instance
browser.

Even if I succeed in reading all the available methods, the problem
is, that each selector is a number and the original method name is not
preserved.

I guess header files are what I am supposed to look at when learning
about instance capabilities.
From:Tilo_Prütz
Subject:Re: Reflection Support
Date:Thu, 18 Nov 2004 20:39:04 +0100
bazad wrote:

> Chris Hanson writes:
>
>> On 2004-11-05 12:23:50 -0800, bazad said:
>> > Does Objective-C support reflection?
>>
>> Do you mean introspection? Objective-C implementations typically have
>> a low-level API for accessing the language runtime, and frameworks
>> such as Cocoa typically layer a higher-level API on top of that.
>>
>> For example, in Cocoa, you can ask an object if it responds to a
>> particular selector, for the function pointer (IMP) corresponding to a
>> particular method, etc. You can construct method invocations at
>> runtime and invoke them. And so on.
>>
>> So yes, it supports what Java calls "reflection" very well.
>>
>> -- Chris
>
> Yes, I found that introspection is supported for the real life cases.
>
> I had an idea of writing a small console tool to print out the list of
> instance methods with their signatures. It is more of the exercise,
> than a practical application. The application is sort of an instance
> browser.
>
> Even if I succeed in reading all the available methods, the problem
> is, that each selector is a number and the original method name is not
> preserved.
>
> I guess header files are what I am supposed to look at when learning
> about instance capabilities.

In GNU-runtime (without any foundation):

sel_get_name(sel)

gives you the selector name as const char *.
When using libFoundation you can use NSStringFromSelector(sel) as in the
NeXT-runtime. I think GNUStep has this, too.

For iterating over all methods there are API functions, too.

I don't know about Apple but in GNU-runtime SEL is not a number.


>tilo
From:Sherm Pendley
Subject:Re: Reflection Support
Date:Thu, 18 Nov 2004 13:14:52 -0500
bazad wrote:

> Even if I succeed in reading all the available methods, the problem
> is, that each selector is a number and the original method name is not
> preserved.

Nonsense, of course it's preserved. Use NSStringFromSelector() to
convert it to a string object. There are similar low-level functions you
can use if you want a C string.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
From:bazad
Subject:Re: Reflection Support
Date:21 Nov 2004 12:15:18 -0800
Sherm Pendley writes:

> bazad wrote:
>
> > Even if I succeed in reading all the available methods, the problem
> > is, that each selector is a number and the original method name is not
> > preserved.
>
> Nonsense, of course it's preserved. Use NSStringFromSelector() to
> convert it to a string object. There are similar low-level functions
> you can use if you want a C string.
>
> sherm--
>
> --
> Cocoa programming in Perl: http://camelbones.sourceforge.net
> Hire me! My resume: http://www.dot-app.org

It is very nice :)

Is it possible to iterate over all classes in the objc library?

#import
#import

int main(int argc, char* argv[])
{
Object* obj = [Object new];
MetaClass meta = [obj metaClass];
SEL selector = @selector(name);
int i;
struct objc_method method;
struct objc_method_list* methods;

printf("Class Name: %s\n", [obj name]);

methods = meta->methods;
printf("%i methods:\n", methods->method_count);
for(i = 0; i < methods->method_count; i++)
{
method = methods->method_list[i];
printf("%i:%s\n", i, sel_get_name(method.method_name));
}

return 0;
}
From:Sherm Pendley
Subject:Re: Reflection Support
Date:Sun, 21 Nov 2004 17:19:22 -0500
bazad wrote:

> It is very nice :)
>
> Is it possible to iterate over all classes in the objc library?

Yes - it's actually very easy. Here's the function from CamelBones that
does that, calling a function to create a corresponding Perl class for
each one. It includes code for both the GNU and Apple runtimes:

#ifdef GNUSTEP
#import
#else
#import
#import
#endif

// Create Perl wrappers for all registered ObjC classes
void CBWrapRegisteredClasses(void) {
#ifdef GNUSTEP
void *enum_state;
Class thisClass;

enum_state = NULL;
while ((thisClass = objc_next_class(&enum_state))) {
CBWrapObjectiveCClass(thisClass);
}
#else
int numClasses;
Class *classes;
int i;

classes = NULL;
numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0) {
classes = malloc(sizeof(Class) * numClasses);
objc_getClassList(classes, numClasses);
for(i=0; i < numClasses; i++) {
CBWrapObjectiveCClass(classes[i]);
}
free(classes);
}
#endif
}

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
From:bazad
Subject:Re: Reflection Support
Date:21 Nov 2004 19:03:28 -0800
bazad writes:

> Sherm Pendley writes:
>
> > bazad wrote:
> >
> > > Even if I succeed in reading all the available methods, the problem
> > > is, that each selector is a number and the original method name is not
> > > preserved.
> >
> > Nonsense, of course it's preserved. Use NSStringFromSelector() to
> > convert it to a string object. There are similar low-level functions
> > you can use if you want a C string.
> >
> > sherm--
> >
> > --
> > Cocoa programming in Perl: http://camelbones.sourceforge.net
> > Hire me! My resume: http://www.dot-app.org
>
> It is very nice :)
>
> Is it possible to iterate over all classes in the objc library?
>
> #import
> #import
>
> int main(int argc, char* argv[])
> {
> Object* obj = [Object new];
> MetaClass meta = [obj metaClass];
> SEL selector = @selector(name);
> int i;
> struct objc_method method;
> struct objc_method_list* methods;
>
> printf("Class Name: %s\n", [obj name]);
>
> methods = meta->methods;
> printf("%i methods:\n", methods->method_count);
> for(i = 0; i < methods->method_count; i++)
> {
> method = methods->method_list[i];
> printf("%i:%s\n", i, sel_get_name(method.method_name));
> }
>
> return 0;
> }

I tried decoding method signatues encoded in struct objc_method (field
method_types) and it is not that easy. Is there any fucntion to do
that?
From:Sherm Pendley
Subject:Re: Reflection Support
Date:Sun, 21 Nov 2004 22:33:37 -0500
bazad wrote:

> I tried decoding method signatues encoded in struct objc_method (field
> method_types) and it is not that easy. Is there any fucntion to do
> that?

Use class_getInstanceVariable(Class theClass, const char *varName) to
get a pointer to a objc_ivar struct, which looks like this:

struct objc_ivar {
char *ivar_name;
char *ivar_type;
int ivar_offset;
}

The meaning of name and type are obvious; the offset is the location of
the variable within the instance memory. isa is always at offset zero -
the instance variables you declare in your class definitions follow
after that.

You'll still need to decode the type, but that's a heck of a lot easier
when you only need to decode it for one variable at a time. Most of the
time you can ignore the size qualifier, and use a simple switch on the
first char:

switch (*objc_ivar_ptr->ivar_type) {
case 'c': // char
break;
case '@': // id
break;
// ... etc.
}

For C arrays and struct types you'll need to dig a little deeper.

(The above all applies to Apple's runtime - from your mention of
objc_method I'm assuming that's what you're using, and I don't have
headers for the GNU runtime handy.)

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
From:Tilo_Prütz
Subject:Re: Reflection Support
Date:Mon, 22 Nov 2004 08:38:26 +0100
Sherm Pendley wrote:
>
> switch (*objc_ivar_ptr->ivar_type) {
> case 'c': // char
> break;
> case '@': // id
> break;
> // ... etc.
> }
>

In GNU runtime there are macros like _C_ID defined for the type characters
in objc/objc-api.h and objc/encoding.h. If you have encoding.c, this
would be a good starting point to explore the objc_method_description.

> For C arrays and struct types you'll need to dig a little deeper.
>
> (The above all applies to Apple's runtime - from your mention of
> objc_method I'm assuming that's what you're using, and I don't have
> headers for the GNU runtime handy.)
>
> sherm--

objc_method is also defined in the GNU runtime :).


greetz

>tilo
From:Philippe Mougin
Subject:Re: Reflection Support
Date:19 Nov 2004 03:03:02 -0800
bazad wrote in message news:<1100800666.YSdgfmY0elAPLiUlz5uNgg@teranews>...
[...]
> Yes, I found that introspection is supported for the real life cases.
>
> I had an idea of writing a small console tool to print out the list of
> instance methods with their signatures. It is more of the exercise,
> than a practical application. The application is sort of an instance
> browser.
>
> Even if I succeed in reading all the available methods, the problem
> is, that each selector is a number and the original method name is not
> preserved.
>
> I guess header files are what I am supposed to look at when learning
> about instance capabilities.

The Objective-C run-time allows you to query for methods names and
signatures. If you are on Mac OS X, have a look at the documentation
for the Objective-C run-time, in particular see the document at
http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/ObjCRuntimeRef/function_group_5.html.
The doc for the class_nextMethodList() function contains an example of
code that will helps you to iterate over the methods of a particular
class. I use this kind of stuff extensively in F-Script
(http://www.fscript.org), which contains an object browser that shows
methods and let one to execute them. You might want to look at the
source code.

Philippe Mougin
   

Copyright © 2006 knowledge-database   -   All rights reserved