package com.ericfeminella.introspection
{
import com.ericfeminella.utils.AbstractStaticType;
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, and determining if a specific value has been defined
* by a Class constant.
*
* @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 StaticDefinitionUtils extends AbstractStaticType
{
/**
*
* All static class which determines if a static member has been defined
* by the specified class.
*
* @example The following class defines four static members, that is, two
* static variables, one static constant and one static method the definitions
* are verified via StaticDefinitionUtils
*
* <listing version="3.0">
*
* package
* {
* public class Version
* {
* public static const MAJOR:int = 2;
* public static var MINOR:int = 1;
* public static var BUILD:int = 12;
*
* public static function getVersion () : String
* {
* // code for formating build...
* }
* }
* }
*
* trace( StaticDefinitionUtils.isClassMember( "MAJOR", Version ) ); //true
* trace( StaticDefinitionUtils.isClassMember( "MINOR", Version ) ); //true
* trace( StaticDefinitionUtils.isClassMember( "BUILD", Version ) ); //true
* trace( StaticDefinitionUtils.isClassMember( "getVersion", Version ) ); //true
*
* </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 isMember(memberName:String, Type:Class) : Boolean
{
return Object( Type ).hasOwnProperty( memberName );
}
/**
*
* Determine if a specific value has been assigned to a constant defined with the
* specified Class object
*
* @example
* <listing version="3.0">
*
* // The following class defines three static constants which represent build
* // constants, the values are verified via StaticDefinitionUtils
*
* package
* {
* public class Version
* {
* public static const MAJOR:int = 2;
* public static const MINOR:int = 1;
* public static const BUILD:int = 12;
* }
* }
*
* trace( StaticDefinitionUtils.isValueAssignedToMember( 1, Version ) ); // true
* trace( StaticDefinitionUtils.isValueAssignedToMember( 2, Version ) ); // true
* trace( StaticDefinitionUtils.isValueAssignedToMember( 12, Version ) ); // true
*
* </listing>
*
* @param value in which to determine if a class constant is assigned
* @param Type from which to determine class constant values
* @return true if the value is defined, otherwise false
*
*/
public static function isValueAssignedToConstant(value:*, Type:Class) : Boolean
{
var constants:XMLList = describeType( Type ).constant.@name;
var isAssigned:Boolean = false;
var n:int = constants.length();
for (var i:int = 0; i < n; i++)
{
if ( Type[ constants[i] ] == value )
{
isAssigned = true;
break;
}
}
return isAssigned;
}
/**
*
* Determine if a specific value has been assigned to a constant defined with the
* specified Class object
*
* @example
* <listing version="3.0">
*
* // The following class defines three static constants which represent build
* // constants, the values are verified via StaticDefinitionUtils
*
* package
* {
* public class Version
* {
* public static const MAJOR:int = 2;
* public static const MINOR:int = 1;
* public static var BUILD:int = 12;
* }
* }
*
* trace( StaticDefinitionUtils.isValueAssignedToMember( 1, Version ) ); // true
* trace( StaticDefinitionUtils.isValueAssignedToMember( 2, Version ) ); // true
* trace( StaticDefinitionUtils.isValueAssignedToMember( 12, Version ) ); // true
*
* </listing>
*
* @param value in which to determine if a class constant is assigned
* @param Type from which to determine class constant values
* @return true if the value is defined, otherwise false
*
*/
public static function isValueAssignedToVariable(value:*, Type:Class) : Boolean
{
var variables:XMLList = describeType( Type ).variable.@name;
var isAssigned:Boolean = false;
var n:int = variables.length();
for (var i:int = 0; i < n; i++)
{
if ( Type[ variables[i] ] == value )
{
isAssigned = true;
break;
}
}
return isAssigned;
}
}
}