Utility: getClass()

Here is a little utility I built to easily retrieve the Class for a given String definition or instantiated object:

—-

package
{
  import flash.utils.getDefinitionByName;
  import flash.utils.getQualifiedClassName;

  /**
   * Wraps getDefinitionByName and simply returns null if the class cannot be found.
   *
   * If you pass in a non-String object, will return the class associated with that object.
   *
   * @param definitionNameOrObject A definition name (e.g. "com.myDomain.MyClass") or an actual object,
   * like 'this'.
   */
  public function getClass(definitionNameOrObject : *) : Class
  {
    var clazz : Class;

    if (definitionNameOrObject is String)
    {
      try
      {
        clazz = getDefinitionByName(definitionNameOrObject as String) as Class;

        return clazz;
      }
      catch (error : Error)
      {
        return null;
      }
    }
    else if (definitionNameOrObject is Object)
    {
      var className : String = getQualifiedClassName(definitionNameOrObject);

      clazz = getDefinitionByName(className) as Class;

      return clazz;
    }

    return null;
  }
}

Note: Make sure the file is exactly named “getClass.as” or else the global function will not work.

- Josh

Advertisement
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.