Reflect.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import haxe.lang.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. @:core_api 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 {
  112. Array<String> ret = new Array<String>();
  113. if (o instanceof java.lang.Class)
  114. {
  115. Class<?> cl = (java.lang.Class) o;
  116. for(java.lang.reflect.Field f : cl.getFields())
  117. {
  118. if (java.lang.reflect.Modifier.isStatic(f.getModifiers()))
  119. ret.push(f.getName());
  120. }
  121. for(java.lang.reflect.Method m : cl.getMethods())
  122. {
  123. if (java.lang.reflect.Modifier.isStatic(m.getModifiers()))
  124. ret.push(m.getName());
  125. }
  126. }
  127. return ret;
  128. }
  129. ')
  130. public static function fields( o : Dynamic ) : Array<String>
  131. {
  132. return null;
  133. }
  134. /**
  135. Tells if a value is a function or not.
  136. **/
  137. @:functionBody('
  138. return f instanceof haxe.lang.Function;
  139. ')
  140. public static function isFunction( f : Dynamic ) : Bool
  141. {
  142. return false;
  143. }
  144. /**
  145. Generic comparison function, does not work for methods, see [compareMethods]
  146. **/
  147. @:functionBody('
  148. return haxe.lang.Runtime.compare(a, b);
  149. ')
  150. public static function compare<T>( a : T, b : T ) : Int
  151. {
  152. return 0;
  153. }
  154. /**
  155. Compare two methods closures. Returns true if it's the same method of the same instance.
  156. **/
  157. @:functionBody('
  158. if (f1 == f2)
  159. return true;
  160. if (f1 instanceof haxe.lang.Closure && f2 instanceof haxe.lang.Closure)
  161. {
  162. haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
  163. haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
  164. return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.equals(f2c.field);
  165. }
  166. return false;
  167. ')
  168. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  169. {
  170. return false;
  171. }
  172. /**
  173. Tells if a value is an object or not.
  174. **/
  175. @:functionBody('
  176. return v instanceof haxe.lang.DynamicObject;
  177. ')
  178. public static function isObject( v : Dynamic ) : Bool
  179. {
  180. return false;
  181. }
  182. /**
  183. Delete an object field.
  184. **/
  185. @:functionBody('
  186. return (o instanceof haxe.lang.DynamicObject && ((haxe.lang.DynamicObject) o).__hx_deleteField(f));
  187. ')
  188. public static function deleteField( o : Dynamic, f : String ) : Bool
  189. {
  190. return false;
  191. }
  192. /**
  193. Make a copy of the fields of an object.
  194. **/
  195. public static function copy<T>( o : T ) : T
  196. {
  197. var o2 : Dynamic = {};
  198. for( f in Reflect.fields(o) )
  199. Reflect.setField(o2,f,Reflect.field(o,f));
  200. return cast o2;
  201. }
  202. /**
  203. Transform a function taking an array of arguments into a function that can
  204. be called with any number of arguments.
  205. **/
  206. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  207. {
  208. return new VarArgsFunction(f);
  209. }
  210. }