0

How do you get the full classpath of a static function in Actionscript, when given only the reference to the function? See the below code sample:

public class Tests {
     static function myFunc():void {}
}

var func:Function = Tests.myFunc;

trace(func.?) // should trace "Tests.myFunc"

Is this possible? Even the open source editor and debugger FlashDevelop can't do this. Try rolling over an object with a function ref, it shows the raw pointer (like 12341234) instead of the full name of the function. Of course with describeType you can get info about a type, ie. a class reference, but not when you only have a function ref. Given only a function reference, how do you get its name?

Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • 3
    duplicate, http://stackoverflow.com/questions/4731935/actionscript-obtain-the-name-of-the-current-function and http://stackoverflow.com/questions/711404/can-an-actionscript-function-find-out-its-own-name – fsbmain May 13 '14 at 15:17
  • Incorrect. Those questions ask about getting the name of the CURRENT FUNCTION, whereas my question is about getting the name of a FUNCTION BY ITS REFERENCE (pointer) – Robin Rodricks May 13 '14 at 15:25
  • Most importantly, you CAN use `getStackTrace` to get the name of the CURRENT FUNCTION, but this method would not work if you have a function pointer and need its name. – Robin Rodricks May 13 '14 at 15:26
  • take a look on the first question with the _describeType_ approach, it utilize only reference to the function rather than the current one. Yes, you have to use _this_ or have reference to it somehow. In general it's impossible to get the function name – fsbmain May 13 '14 at 15:28
  • 1
    @Geotarget - I also think it's duplicate, because the first one describes the situation very well and explains when and only you can get a function name and how. Stack trace is not a proper way of doing it, and it's something like a small cheat for me :) – Andrey Popov May 13 '14 at 15:43
  • ok, I added answer with the example for the static function – fsbmain May 13 '14 at 15:56

1 Answers1

1

It's not possible in general, having only reference to the function (as mentioned in this accepted answer Actionscript - Obtain the name of the current function). There is only two ways to get the name:

  1. using describeType, but in this case you have to provide the function host object. You can find example in the first answer Actionscript - Obtain the name of the current function, I modified it for the static functions as well and utilizing getSavedThis that works in the debug player only:

    var name:String = getFunctionName( staticTest );
    trace("1", name);
    name = getFunctionName( staticTest, Astest ); //Astest the name of the class that hosts staticTest function
    trace("2", name);
    
    public static function staticTest():void
    {
    
    }
    
    public static function getFunctionName( func:Function, parent:* = null ):String
    {
        if(!parent)
        {
            //works only in debug flash player
            parent = getSavedThis(staticTest);
        }
    
        var methods:XMLList = describeType(parent)..method;
        for each ( var m:XML in methods)
        {
            if (parent[m.@name] == func)
                return m.@name;
        }
        return null;
    }
    

output:

[trace] 1 staticTest
[trace] 2 staticTest
  1. using getStackTrace (example in the same answer) but it works only in debug player and you have to be in the function closure.
Community
  • 1
  • 1
fsbmain
  • 5,267
  • 2
  • 16
  • 23