package com.ericfeminella.utils
{
import flash.utils.describeType;
/**
*
* All static utility class which provides a mechanism for determining
* if a static constant has been defined by a specific Class object as
* well as operations for inspecting constants which have been defined
* by a class object
*
* @see http://livedocs.adobe.com/labs/flex/3/langref/statements.html#const
* @see http://livedocs.adobe.com/labs/flex/3/langref/statements.html#static
*
*
*/
public final class StaticConstantDefinitionUtil
{
/**
*
* Determines if a constant has been defined by the specified class. This method
* is useful if you need to enforce that the specified class defines the static
* constants from which a value has been set.
*
* @example
* <listing version="3.0">
*
* // The following class defines three static constants which represents typical
* // version scheme, the definitions are verified via StaticConstantDefinitionUtil
*
* package
* {
* public class Version
* {
* public static const MAJOR:int = 2;
* public static const MINOR:int = 1;
* public static const BUILD:int = 12;
* }
* }
*
* import com.ericfeminella.utils.StaticConstantDefinitionUtil;
*
* trace( StaticConstantDefinitionUtil.isDefinedByClass("MAJOR", Version) ); //true
* trace( StaticConstantDefinitionUtil.isDefinedByClass("MINOR", Version) ); //true
* trace( StaticConstantDefinitionUtil.isDefinedByClass("BUILD", Version) ); //true
* trace( StaticConstantDefinitionUtil.isDefinedByClass("REVISION", Version) ); //false
*
* </listing>
*
* @param static constant which definition is to be determined
* @param Class object from which to determine definition
* @return true if the constant is defined by the specified class, otherwise false
*
*/
public static function isDefinedByClass(memberName:String, Type:Class) : Boolean
{
var constants:XMLList = describeType( Type ).constant.@name;
return constants.contains( memberName );
}
/**
*
* Retrieves all static constants which have been defined by the specified class
*
* @example
* <listing version="3.0">
*
* // The following class defines three static constants which represent alpha
* // charachters, the definitions are verified via StaticConstantDefinitionUtil
*
* package
* {
* public class Version
* {
* public static const MAJOR:int = 2;
* public static const MINOR:int = 1;
* public static const BUILD:int = 12;
* }
* }
*
* import com.ericfeminella.utils.StaticConstantDefinitionUtil;
* trace(StaticConstantDefinitionUtil.getDefinedConstantsByClass(Version));//MAJOR,MINOR,BUILD
*
* </listing>
*
* @param Class object from which to determine constant definitions
* @return String Array containing the names of all static constants
*
*/
public static function getDefinedConstantsByClass(Type:Class) : Array
{
var constants:XMLList = describeType( Type ).constant.@name;
var names:Array = new Array();
var n:int = constants.length();
for (var i:int = 0; i < n; i++)
{
names.push( constants[i] );
}
return names;
}
}
}