Reflect.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import java.internal.Function;
  23. import java.internal.HxObject;
  24. import java.internal.Runtime;
  25. import java.Boot;
  26. @:keep @:coreApi class Reflect {
  27. public static function hasField( o : Dynamic, field : String ) : Bool
  28. {
  29. if (Std.is(o, IHxObject)) {
  30. return untyped (o : IHxObject).__hx_getField(field, false, true, false) != Runtime.undefined;
  31. }
  32. return Runtime.slowHasField(o, field);
  33. }
  34. public static function field( o : Dynamic, field : String ) : Dynamic
  35. {
  36. if (Std.is(o, IHxObject)) {
  37. return untyped (o : IHxObject).__hx_getField(field, false, false, false);
  38. }
  39. return Runtime.slowGetField(o, field, false);
  40. }
  41. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  42. {
  43. if (Std.is(o, IHxObject)) {
  44. untyped (o : IHxObject).__hx_setField(field, value, false);
  45. } else {
  46. Runtime.slowSetField(o, field, value);
  47. }
  48. }
  49. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  50. {
  51. if (Std.is(o, IHxObject)) {
  52. return untyped (o : IHxObject).__hx_getField(field, false, false, true);
  53. }
  54. if (Runtime.slowHasField(o, "get_" + field)) {
  55. return Runtime.slowCallField(o, "get_" + field, null);
  56. }
  57. return Runtime.slowGetField(o, field, false);
  58. }
  59. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  60. {
  61. if (Std.is(o, IHxObject)) {
  62. untyped (o : IHxObject).__hx_setField(field, value, true);
  63. } else if (Runtime.slowHasField(o, "set_" + field)) {
  64. Runtime.slowCallField(o, "set_" + field, [value]);
  65. } else {
  66. Runtime.slowSetField(o, field, value);
  67. }
  68. }
  69. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic
  70. {
  71. return untyped (func : Function).__hx_invokeDynamic(args);
  72. }
  73. public static function fields( o : Dynamic ) : Array<String>
  74. {
  75. if (Std.is(o, IHxObject)) {
  76. var ret:Array<String> = [];
  77. untyped (o : IHxObject).__hx_getFields(ret);
  78. return ret;
  79. } else if (Std.is(o, java.lang.Class)) {
  80. return Type.getClassFields(cast o);
  81. } else {
  82. return [];
  83. }
  84. }
  85. public static function isFunction( f : Dynamic ) : Bool
  86. {
  87. return Std.is(f, Function);
  88. }
  89. public static function compare<T>( a : T, b : T ) : Int
  90. {
  91. return Runtime.compare(a, b);
  92. }
  93. @:access(java.internal.Closure)
  94. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  95. {
  96. if (f1 == f2) {
  97. return true;
  98. }
  99. if (Std.is(f1, Closure) && Std.is(f2, Closure)) {
  100. var f1c:Closure = cast f1;
  101. var f2c:Closure = cast f2;
  102. return Runtime.refEq(f1c.obj, f2c.obj) && f1c.field == f2c.field;
  103. }
  104. return false;
  105. }
  106. public static function isObject( v : Dynamic ) : Bool
  107. {
  108. return v != null && !(Std.is(v, HxEnum) || Std.is(v, Function) || Std.is(v, java.lang.Enum) || Std.is(v, java.lang.Number) || Std.is(v, java.lang.Boolean.BooleanClass));
  109. }
  110. public static function isEnumValue( v : Dynamic ) : Bool {
  111. return v != null && (Std.is(v, HxEnum) || Std.is(v, java.lang.Enum));
  112. }
  113. public static function deleteField( o : Dynamic, field : String ) : Bool
  114. {
  115. return (Std.is(o, DynamicObject) && (o : DynamicObject).__hx_deleteField(field));
  116. }
  117. public static function copy<T>( o : T ) : T
  118. {
  119. var o2 : Dynamic = {};
  120. for( f in Reflect.fields(o) )
  121. Reflect.setField(o2,f,Reflect.field(o,f));
  122. return cast o2;
  123. }
  124. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  125. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  126. {
  127. return new VarArgsFunction(f);
  128. }
  129. }