(Links in this post will open in a new window/tab. I’m also aiming at being overly explicit
)
Problem and solution
This post will discuss a method I use to figure out which method called the method Im currently executing code within and also what the name of the currently executing method is. To start with, take a peek at the first pastebin snippet.
Simple enough, we execute the following function from an applicationclass called InspectionTest:
private function doImportantStuff():void {
var c:InterestingClass = new InterestingClass();
trace(c.method2("Foo"));
}
And this then traces out “You told me: Foo”, simple enough.
Now, I’ll apply some changes (pastebin snippet) to InterestingClass in order to allow it to be self inspected. Executing doImportantStuff() would after the changes yield:
I am in method2 of InterestingClass and was called from doImportantStuff of InspectionTest
You told me: Foo
Again, to be clear, InspectionTest is the application class that has the method doImportantStuff(). Lets try inheritance in AnotherInterestingClass! (pastebin snippet)
private function doImportantStuff():void {
var c:AnotherInterestingClass = new AnotherInterestingClass();
trace(c.method2("Foo"));
}
Executing doImportantStuff() again yields:
I am in method2 of InterestingClass and was called from doImportantStuff of InspectionTest
You told me: Foo
Which is perfectly alright! AnotherInterestingClass isn’t overriding method2().. what happens if we do that? (pastebin snippet)
doImportantStuff() now yields:
I am in method2 of AnotherInterestingClass and was called from doImportantStuff of InspectionTest
You told me: Foo
As the result shows, by overriding method2() in AnotherInterestingClass and making a call to processStacktrace() we get a result back which we can use to figure out which method was called and from what method it was called… and as you probably can see we also get which class name the originating method is from.
Discussion
Knowing which method has been called and what method called it can be important to know when doing stuff using the reflection API. AS3 supports metadata tag (docs entry on metadata tags) notation on methods and properties (perhaps also class declarations?) and we can see these tags by calling describeType (docs entry on describeType) with a class or instance of a class as the parameter. With the data given from describeType() and knowing which method is executing we can then see what kind of metadata is attached to that method… neat eh?
It should be noted that these tags can not be used via Adobe Flash CS3 IDE compilation (as far as I know, atleast), only the commandline compiler that comes with the Adobe Flex SDK. Further, if you declare a custom tag you must explicitly tell the compiler to include these by adding “-keep-as3-metadata+=[Tag name]”. For example, if I have a metatag called MyTag, I add “-keep-as3-metadata+=MyTag” to the compiler parameters and it will then visible when calling describeType().
Upcoming
In a future post to this blog, I will describe how I created a role/permission-based policy framework to be used with Adobe Flex, using the techniques described in this post. It would be nice to have a discussion on improvements that can be made to the processStacktrace() method or completly different approaches to the problem.
Nasties
It should be noted that the regular expression used in processStacktrace() currently ignore getter/setter functions. There might also be other issues when doing deep inheritance or composition.. . if you spot any bugs, please report them here 