{"id":244,"date":"2008-02-21T15:08:16","date_gmt":"2008-02-21T23:08:16","guid":{"rendered":"http:\/\/dougmccune.com\/blog\/2008\/02\/21\/sneaky-flex-trick-to-get-the-name-of-any-calling-functions\/"},"modified":"2008-02-21T15:16:59","modified_gmt":"2008-02-21T23:16:59","slug":"sneaky-flex-trick-to-get-the-name-of-any-calling-functions","status":"publish","type":"post","link":"https:\/\/dougmccune.com\/blog\/2008\/02\/21\/sneaky-flex-trick-to-get-the-name-of-any-calling-functions\/","title":{"rendered":"Sneaky Flex trick to get the name of any calling functions"},"content":{"rendered":"<p>Well I&#8217;ll be damned, in my <a href=\"https:\/\/dougmccune.com\/blog\/2008\/02\/21\/monkey-patching-flexsprite-to-list-all-event-listeners-on-any-flex-component\/\">last post<\/a> I wrote about how I was trying to figure out which class was calling addEventListener on a FlexSprite. I couldn&#8217;t figure out a way to get the name of the calling class, or the name of the method that called addEventListener(). Well, Almog Kurtser commented that there&#8217;s a trick you can do by using the Error class and the getStackTraceMethod() to parse out these strings.<\/p>\n<p>If you call: <code style=\"font-size:12px;\">trace(new Error().getStackTrace())<\/code> you&#8217;ll get something that looks like:<\/p>\n<div style=\"width:350px; height:200px; overflow : auto; border:1px solid silver; background-color:#ffffff \">\n<pre>\r\n<code style=\"font-size:12px;\">\r\nError\r\nat MonkeyPatchTest\/___MonkeyPatchTest_Application1_creationComplete()[path\/to\/source\/src\/MonkeyPatchTest.mxml:4]\r\nat flash.events::EventDispatcher\/dispatchEventFunction()\r\nat flash.events::EventDispatcher\/dispatchEvent()\r\nat mx.core::UIComponent\/dispatchEvent()[E:\\dev\\flex_3_beta3\\sdk\\frameworks\\projects\\framework\\src\\mx\\core\\UIComponent.as:9041]\r\nat mx.core::UIComponent\/set initialized()[E:\\dev\\flex_3_beta3\\sdk\\frameworks\\projects\\framework\\src\\mx\\core\\UIComponent.as:1165]\r\nat mx.managers::LayoutManager\/doPhasedInstantiation()[E:\\dev\\flex_3_beta3\\sdk\\frameworks\\projects\\framework\\src\\mx\\managers\\LayoutManager.as:696]\r\nat Function\/http:\/\/adobe.com\/AS3\/2006\/builtin::apply()\r\nat mx.core::UIComponent\/callLaterDispatcher2()[E:\\dev\\flex_3_beta3\\sdk\\frameworks\\projects\\framework\\src\\mx\\core\\UIComponent.as:8450]\r\nat mx.core::UIComponent\/callLaterDispatcher()[E:\\dev\\flex_3_beta3\\sdk\\frameworks\\projects\\framework\\src\\mx\\core\\UIComponent.as:8393]\r\n<\/code><\/pre>\n<\/div>\n<p>Within that long string you have all sorts of information about every calling function all the way up the stack. You can parse that all out and figure out where in the application you were. You can do this from anywhere in your app.<\/p>\n<p>So I updated the <a href=\"https:\/\/dougmccune.com\/blog\/2008\/02\/21\/monkey-patching-flexsprite-to-list-all-event-listeners-on-any-flex-component\/\">monkey patched event dispatcher example<\/a> in my last post to use this method and lo and behold we can get the class name of the class that called addEventListener(). We even get the method name and the line number where addEventListener was called.<\/p>\n<p>Now in the <a href=\"http:\/\/dougmccune.com\/flex\/monkeypatchevents\/flexspy\/\">FlexSpy example<\/a> you get much more information and can trace back exactly where the event listener was added. Sweet! A big thanks goes to Almog (and to Alex Harui who Almog references as his source of this little gem).<\/p>\n<p><strong>IMPORTANT NOTE:<\/strong> The Error.getStackTrace() method only works with the <strong>DEBUG<\/strong> version of your swf. If you export a release build then a call to Error.getStackTrace() won&#8217;t work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well I&#8217;ll be damned, in my last post I wrote about how I was trying to figure out which class was calling addEventListener on a FlexSprite. I couldn&#8217;t figure out a way to get the name of the calling class, or the name of the method that called addEventListener(). Well, Almog Kurtser commented that there&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-flex"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":0,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"wp:attachment":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}