Products
Technologies

Developer resources

Custom metadata in three simple steps.

Avg. Rating 4.8

Problem

How to add custom metadata tags to your classes or properties to use on runtime?

Solution

1. Configure compiler. 2. Write metadata 3. Use describeType

Detailed explanation

Custom metadata in three simple steps.

1. Configure compiler

To new Flex project add : -keep-as3-metadata+=Meta1,Meta2 to 'additional compiler arguments'. Write your metadata tag names separated by comma.

Hint: Build SWC library with keep-as3-metadata, load it into project and use your tags in this project. You don't need to add compiler parameter again.

2. Write metadata

Example :

package
{
    [Meta2(param1 = "param 1 value")]
    public class TestClass
    {

        [Meta1(param1 = "param 1 value", param2 = "param 2 value")]
        public var test1:String;
       
       
        [Meta2(paramA = "param 1 value", paramB = "param 2 value")]
        public function get test2():String
        {
            return null;
        }
       
        public function set test2(val:String):void
        {
        }

        [Meta1(param1 = "param 1 value")]
        public function someMethod():void
        {
        };

    }
}

3. Use your metadata during runtime

Just : describeType(TestClass)

and lookup your data (I mean E4X) :

<type name="TestClass" base="Class" isDynamic="true" isFinal="true" isStatic="true">
  <extendsClass type="Class"/>
  <extendsClass type="Object"/>
  <accessor name="prototype" access="readonly" type="*" declaredBy="Class"/>
  <factory type="TestClass">


    <metadata name="Meta2">
      <arg key="param1" value="param 1 value"/>
    </metadata>


    <extendsClass type="Object"/>


    <method name="someMethod" declaredBy="TestClass" returnType="void">
      <metadata name="Meta1">
        <arg key="param1" value="param 1 value"/>
      </metadata>
    </method>


    <variable name="test1" type="String">
      <metadata name="Meta1">
        <arg key="param1" value="param 1 value"/>
        <arg key="param2" value="param 2 value"/>
      </metadata>
    </variable>


    <accessor name="test2" access="readwrite" type="String" declaredBy="TestClass">
      <metadata name="Meta2">
        <arg key="paramA" value="param 1 value"/>
        <arg key="paramB" value="param 2 value"/>
      </metadata>
    </accessor>


  </factory>
</type>

Good luck.

Report abuse

Related recipes