describeType メソッド

describeType() メソッド (livedocs@lab) は引数に渡されたオブジェクトの情報を XML 形式で返します。引数には ActionScript の任意のオブジェクトを(クラスや int なども)渡すことができます。

var mc:MovieClip = new MovieClip();
var typeInfo:XML = describeType(mc);

返される XML オブジェクトに含まれる情報は引数として渡されたオブジェクトの属性やメソッド等です。Java のリフレクションと同じような機能です。

XML オブジェクトだと扱いが面倒に思われるかもしれませんが、E4X のおかげでむしろ Object よりも便利です。例えば、上の例の続きで、戻り値が void のメソッドのリストを取得するには typeInfo.method.(@returnType == "void") のように記述できます。

出力例

以下、MovieClip のインスタンスを describeType() に渡した結果を例として具体的な出力を説明します。

まず、ルートノードは type というタグです。type タグの属性を見ると、パッケージ名とクラス名、親クラス名、その他のクラス宣言時の修飾子が分かります。

<type name="flash.display::MovieClip" base="flash.display::Sprite" isDynamic="true" isFinal="false" isStatic="false">  

残りのタグは、type の子ノードです。一つ目は extendsClass タグで、継承関係にある全ての上位クラスの名前とパッケージ名が type 属性から分かります。また implementsInterface タグで継承しているインターフェースも分かります。

<extendsClass type="flash.display::Sprite"/>
<extendsClass type="flash.display::DisplayObjectContainer"/>
<extendsClass type="flash.display::InteractiveObject"/>
<extendsClass type="flash.display::DisplayObject"/>
<extendsClass type="flash.events::EventDispatcher"/>
<extendsClass type="Object"/>
<implementsInterface type="flash.display::IBitmapDrawable"/>
<implementsInterface type="flash.events::IEventDispatcher"/>

属性情報は accessor タグから分かります。名前と、アクセス、型、宣言されたクラスが分かります。

<accessor name="totalFrames" access="readonly" type="int" declaredBy="flash.display::MovieClip"/>
<accessor name="currentFrame" access="readonly" type="int" declaredBy="flash.display::MovieClip"/>

メソッド情報は method タグです。メソッド名、宣言したクラス、戻り値の型が属性に記述されます。パラメータを持つメソッドは子ノードを持ち、そこに、順番、型、オプションかどうかの情報があります。

<method name="play" declaredBy="flash.display::MovieClip" returnType="void"/>
<method name="gotoAndPlay" declaredBy="flash.display::MovieClip" returnType="void">
  <parameter index="1" type="*" optional="false"/>
  <parameter index="2" type="String" optional="true"/>
</method>

一通り MovieClip の定義情報が終わると、上位クラスで宣言されている属性とメソッドが続きます。MovieClip の場合は Sprite から EventDispatcher まで長々と情報が表示されます。

<method name="startDrag" declaredBy="flash.display::Sprite" returnType="void">
<parameter index="1" type="Boolean" optional="true"/>
<parameter index="2" type="flash.geom::Rectangle" optional="true"/>
</method>
...
...
<method name="addEventListener" declaredBy="flash.events::EventDispatcher" returnType="void">
<parameter index="1" type="String" optional="false"/>
<parameter index="2" type="Function" optional="false"/>
<parameter index="3" type="Boolean" optional="true"/>
<parameter index="4" type="int" optional="true"/>
<parameter index="5" type="Boolean" optional="true"/>
</method>

静的情報の取得

上の例ではインスタンスの情報のみで、クラスメソッド等の情報は出てきませんでした。static 宣言されている情報を取得するには describeType() にクラスオブジェクトを渡します。

下の例では Font クラスオブジェクトを渡しています。

trace(describeType(Font));

この例での出力は以下のようになります。type タグの base 属性が Class になっています。その他の属性も、クラスオブジェクトに対するもので Font クラスの属性ではありません。

type タグの直接の子ノードになっている method や accessor タグが static 宣言されたものです。インスタンス変数やメソッドは factory タグの子ノードになっています。

<type name="flash.text::Font" base="Class" isDynamic="true" isFinal="true" isStatic="true">
  <extendsClass type="Class"/>
  <extendsClass type="Object"/>
  <method name="enumerate" declaredBy="flash.display::Font" returnType="Array"/>
  <method name="register" declaredBy="flash.display::Font" returnType="void">
    <parameter index="1" type="Class" optional="false"/>
  </method>
  <accessor name="prototype" access="readonly" type="*" declaredBy="Class"/>
  <factory type="flash.text::Font">
    <extendsClass type="Object"/>
    <accessor name="fontStyle" access="readonly" type="String" declaredBy="flash.text::Font"/>
    <accessor name="fontName" access="readonly" type="String" declaredBy="flash.text::Font"/>
    <method name="hasGlyphs" declaredBy="flash.text::Font" returnType="Boolean">
      <parameter index="1" type="String" optional="false"/>
    </method>
  </factory>
</type>

beta3 ではメタタグ情報が出力されないようで、使用変更というよりはバグっぽい感じです。

コメントする

2014年1月

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
レンタルサーバー

月別 アーカイブ

Powered by Movable Type 4.261