A question on the flashcoders list about an hour ago triggered me to put together a very simple example that I have been wanting to put together for ages now.
How do you get the name of a function?
With function we could refer to:
any function
a calling function
a function being called
My reflection package is at the core of my xflas2 logger, but I don’t think a lot of people have been using it. Even though it provides much the same information MTASC can put into a trace statement WITHOUT MTASC (except for the linenumbers). Not that I don’t like MTASC, I absolutely love MTASC and never leave home without it ;-).
Anyway, the reflection package is perfectly capable of being used standalone without my logger or Xray around (yeah that’s right: osflash.org/xray yeah baby!). It provides two simple classes with an even simpler API: ClassFinder and FunctionFinder.
Imagine a function:
private static function runExample3() {
_print (
"I am :"+
FunctionFinder.getFunctionName(arguments.callee)+
" and I am defined in "+
ClassFinder.getClassName(
FunctionFinder.getFunctionClass(arguments.callee)
)+
" and OH YEAH I was called by "+
FunctionFinder.getFunctionName(arguments.caller)
);
}
Note that it’s static, but it doesn’t have to be, I’m just being a lazy git.
This prints:
I am :runExample3 and I am defined in SampleClass and OH YEAH I was called by main
Now, for the small print : you HAVE to call ClassFinder.registerAll() once somewhere at the start of your program.
For your convenience I’ve put an example together for download, have fun and let me know what you think of it!
Note:
I am working on an (xuse da tude but I’ve been very happy with it so far) awesome new version of a reflection package, which doesn’t have the registerAll requirement, but I’m not quite there yet so for now this will have to do :).