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. It provides much the same information MTASC can put into a trace statement WITHOUT MTASC (except for the linenumbers).
Anyway, the reflection package is perfectly capable of being used standalone without a logger of some kind. 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 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 I was called by main
Now, for the small print : you HAVE to call ClassFinder.registerAll() once somewhere at the start of your program.
Download the sample here: GettingCalleeName (639 downloads)