Find out whether ActionScript Function has varargs / optional arguments using reflection?
Given an ActionScript Function object, is there any way to determine whether that function has one or more optional parameters, or vararg parameters? The length
property seems to return the minimum number of arguments accepted:
function vararg(a:*, b:*, ...rest):void {}
function optional(a:*, b:* = null, c:* = null):void {}
trace(vararg.length); // 2
trace(optional.length); // 1
I've开发者_JAVA技巧 tried reflecting over the function properties:
for (var name:String in optional) {
trace(name + ": " + optional[name];
}
However this did not output anything at all.
Does anyone know how to discover this information through reflection?
Well, I can get you a little bit closer, but not all the way.
If you call describeType
on the object that has the function AND those functions are public, you will get more information about the functions:
var description:XML = describeType(this);
var testFunction:* = description.method.(@name == "optional")[0];
trace(testFunction);
This will give you useful output:
<method name="optional" declaredBy="MyClass" returnType="void">
<parameter index="1" type="*" optional="false"/>
<parameter index="2" type="*" optional="true"/>
<parameter index="3" type="*" optional="true"/>
<metadata name="__go_to_definition_help">
<arg key="file" value="/path/to/MyClass.mxml"/>
<arg key="pos" value="222"/>
</metadata>
</method>
It also won't tell you about the ...rest
varargs. So, there are two caveats: they must be public AND you don't get varargs... but you do get a lot more information...
I am not sure you will be able to get any more information than this.
I've always thought describeType
needs to be able to reflect on private stuff as well... but alas.
http://bugs.adobe.com/jira/browse/FP-1472 is the bug to add varargs to describeType. It's got a priority of "none", which doesn't give much hope that this will be fixed. Maybe voting it up will help though.
精彩评论