PageRenderTime 12ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cocktail/classInstance/as3/ClassInstance.hx

http://github.com/silexlabs/Cocktail
Haxe | 70 lines | 25 code | 8 blank | 37 comment | 3 complexity | 291944232bad4440a458a3b6d5b8e1c9 MD5 | raw file
 1/*
 2This file is part of Silex - see http://projects.silexlabs.org/?/silex
 3
 4Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
 5
 6This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 
 7
 8This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 9
10To read the license please visit http://www.gnu.org/copyleft/gpl.html
11*/
12package cocktail.classInstance.as3;
13
14import haxe.Log;
15import cocktail.classInstance.abstract.AbstractClassInstance;
16
17/**
18 * This is the implementation of the native instance proxy for Flash. It implemenents
19 * a class instantiation method specific to the Flash runtime
20 * @author Yannick DOMINGUEZ
21 */
22class ClassInstance extends AbstractClassInstance
23{
24	/**
25	 * class constructor
26	 */
27	public function new(nativeInstanceClassName:String) 
28	{
29		super(nativeInstanceClassName);
30		
31		//instantiate the native Flash class and store a reference to it
32		var classReference:Class<Dynamic> = Type.resolveClass(nativeInstanceClassName);
33		_nativeInstance = Type.createInstance(classReference, new Array());
34	}
35	
36	//////////////////////////////////////////////////////////////////////////////////////////
37	// Overriden proxy method to add Flash specific behaviour. Used to prevent 
38	// runtime error from being thrown in Flash
39	//////////////////////////////////////////////////////////////////////////////////////////
40	
41	/**
42	 * check if a function exists on the class instance. Must rely on Type.getInstanceField to check if
43	 * the method exists, as Reflect.hasField only works for dynamic attributes and only return false for 
44	 * class attributes/methods.
45	 * 
46	 * @param functionName the name of the searched method
47	 * @return
48	 */
49	override public function isFunction(functionName:String):Bool
50	{
51		//retrieve all the static native class fields
52		var instanceFields:Array<String> = Type.getInstanceFields(Type.getClass(_nativeInstance));
53		var numInstanceFields:Int = instanceFields.length;
54		
55		//loop in the fields to find a match for the method name
56		for (i in 0...numInstanceFields)
57		{
58			if (instanceFields[i] == functionName)
59			{
60				//a field with the right name exists,
61				//test if it is a function
62				return super.isFunction(functionName);
63			}
64		}
65
66		//the method does not exist
67		return false;
68	}
69	
70}