Reflect.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import java.internal.Function;
  2. import java.Boot;
  3. /*
  4. * Copyright (c) 2005, The haXe Project Contributors
  5. * All rights reserved.
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  25. * DAMAGE.
  26. */
  27. /**
  28. The Reflect API is a way to manipulate values dynamicly through an
  29. abstract interface in an untyped manner. Use with care.
  30. **/
  31. @:keep @:coreApi class Reflect {
  32. /**
  33. Tells if an object has a field set. This doesn't take into account the object prototype (class methods).
  34. **/
  35. @:functionBody('
  36. if (o instanceof haxe.lang.IHxObject)
  37. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, true, false) != haxe.lang.Runtime.undefined;
  38. return haxe.lang.Runtime.slowHasField(o, field);
  39. ')
  40. public static function hasField( o : Dynamic, field : String ) : Bool
  41. {
  42. return false;
  43. }
  44. /**
  45. Returns the field of an object, or null if [o] is not an object or doesn't have this field.
  46. **/
  47. @:functionBody('
  48. if (o instanceof haxe.lang.IHxObject)
  49. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, false, false);
  50. return haxe.lang.Runtime.slowGetField(o, field, false);
  51. ')
  52. public static function field( o : Dynamic, field : String ) : Dynamic
  53. {
  54. return null;
  55. }
  56. /**
  57. Set an object field value.
  58. **/
  59. @:functionBody('
  60. if (o instanceof haxe.lang.IHxObject)
  61. ((haxe.lang.IHxObject) o).__hx_setField(field, value, false);
  62. else
  63. haxe.lang.Runtime.slowSetField(o, field, value);
  64. ')
  65. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  66. {
  67. }
  68. /**
  69. Similar to field but also supports property (might be slower).
  70. **/
  71. @:functionBody('
  72. if (o instanceof haxe.lang.IHxObject)
  73. return ((haxe.lang.IHxObject) o).__hx_getField(field, false, false, true);
  74. return haxe.lang.Runtime.slowGetField(o, field, false);
  75. ')
  76. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  77. {
  78. return null;
  79. }
  80. /**
  81. Similar to setField but also supports property (might be slower).
  82. **/
  83. @:functionBody('
  84. if (o instanceof haxe.lang.IHxObject)
  85. ((haxe.lang.IHxObject) o).__hx_setField(field, value, true);
  86. else
  87. haxe.lang.Runtime.slowSetField(o, field, value);
  88. ')
  89. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  90. {
  91. }
  92. /**
  93. Call a method with the given object and arguments.
  94. **/
  95. @:functionBody('
  96. return ((haxe.lang.Function) func).__hx_invokeDynamic(args);
  97. ')
  98. public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic
  99. {
  100. return null;
  101. }
  102. /**
  103. Returns the list of fields of an object, excluding its prototype (class methods).
  104. **/
  105. @:functionBody('
  106. if (o instanceof haxe.lang.IHxObject)
  107. {
  108. Array<String> ret = new Array<String>();
  109. ((haxe.lang.IHxObject) o).__hx_getFields(ret);
  110. return ret;
  111. } else if (o instanceof java.lang.Class) {
  112. return Type.getClassFields( (java.lang.Class) o);
  113. } else {
  114. return new Array<String>();
  115. }
  116. ')
  117. public static function fields( o : Dynamic ) : Array<String>
  118. {
  119. return null;
  120. }
  121. /**
  122. Tells if a value is a function or not.
  123. **/
  124. @:functionBody('
  125. return f instanceof haxe.lang.Function;
  126. ')
  127. public static function isFunction( f : Dynamic ) : Bool
  128. {
  129. return false;
  130. }
  131. /**
  132. Generic comparison function, does not work for methods, see [compareMethods]
  133. **/
  134. @:functionBody('
  135. return haxe.lang.Runtime.compare(a, b);
  136. ')
  137. public static function compare<T>( a : T, b : T ) : Int
  138. {
  139. return 0;
  140. }
  141. /**
  142. Compare two methods closures. Returns true if it's the same method of the same instance.
  143. **/
  144. @:functionBody('
  145. if (f1 == f2)
  146. return true;
  147. if (f1 instanceof haxe.lang.Closure && f2 instanceof haxe.lang.Closure)
  148. {
  149. haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
  150. haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
  151. return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.equals(f2c.field);
  152. }
  153. return false;
  154. ')
  155. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  156. {
  157. return false;
  158. }
  159. /**
  160. Tells if a value is an object or not.
  161. **/
  162. @:functionBody('
  163. return v instanceof haxe.lang.DynamicObject;
  164. ')
  165. public static function isObject( v : Dynamic ) : Bool
  166. {
  167. return false;
  168. }
  169. /**
  170. Delete an object field.
  171. **/
  172. @:functionBody('
  173. return (o instanceof haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(f));
  174. ')
  175. public static function deleteField( o : Dynamic, f : String ) : Bool
  176. {
  177. return false;
  178. }
  179. /**
  180. Make a copy of the fields of an object.
  181. **/
  182. public static function copy<T>( o : T ) : T
  183. {
  184. var o2 : Dynamic = {};
  185. for( f in Reflect.fields(o) )
  186. Reflect.setField(o2,f,Reflect.field(o,f));
  187. return cast o2;
  188. }
  189. /**
  190. Transform a function taking an array of arguments into a function that can
  191. be called with any number of arguments.
  192. **/
  193. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  194. {
  195. return new VarArgsFunction(f);
  196. }
  197. }