Reflect.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C)2005-2017 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. @: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. @:keep
  35. public static function field( o : Dynamic, field : String ) : Dynamic
  36. {
  37. if (Std.is(o, IHxObject)) {
  38. return untyped (o : IHxObject).__hx_getField(field, false, false, false);
  39. }
  40. return Runtime.slowGetField(o, field, false);
  41. }
  42. @:keep
  43. public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void
  44. {
  45. if (Std.is(o, IHxObject)) {
  46. untyped (o : IHxObject).__hx_setField(field, value, false);
  47. } else {
  48. Runtime.slowSetField(o, field, value);
  49. }
  50. }
  51. public static function getProperty( o : Dynamic, field : String ) : Dynamic
  52. {
  53. if (Std.is(o, IHxObject)) {
  54. return untyped (o : IHxObject).__hx_getField(field, false, false, true);
  55. }
  56. if (Runtime.slowHasField(o, "get_" + field)) {
  57. return Runtime.slowCallField(o, "get_" + field, null);
  58. }
  59. return Runtime.slowGetField(o, field, false);
  60. }
  61. public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void
  62. {
  63. if (Std.is(o, IHxObject)) {
  64. untyped (o : IHxObject).__hx_setField(field, value, true);
  65. } else if (Runtime.slowHasField(o, "set_" + field)) {
  66. Runtime.slowCallField(o, "set_" + field, [value]);
  67. } else {
  68. Runtime.slowSetField(o, field, value);
  69. }
  70. }
  71. public static function callMethod( o : Dynamic, func : haxe.Constraints.Function, args : Array<Dynamic> ) : Dynamic
  72. {
  73. return untyped (func : Function).__hx_invokeDynamic(args);
  74. }
  75. @:keep
  76. public static function fields( o : Dynamic ) : Array<String>
  77. {
  78. if (Std.is(o, IHxObject)) {
  79. var ret:Array<String> = [];
  80. untyped (o : IHxObject).__hx_getFields(ret);
  81. return ret;
  82. } else if (Std.is(o, java.lang.Class)) {
  83. return Type.getClassFields(cast o);
  84. } else {
  85. return [];
  86. }
  87. }
  88. public static function isFunction( f : Dynamic ) : Bool
  89. {
  90. return Std.is(f, Function);
  91. }
  92. public static function compare<T>( a : T, b : T ) : Int
  93. {
  94. return Runtime.compare(a, b);
  95. }
  96. @:access(java.internal.Closure)
  97. public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool
  98. {
  99. if (f1 == f2) {
  100. return true;
  101. }
  102. if (Std.is(f1, Closure) && Std.is(f2, Closure)) {
  103. var f1c:Closure = cast f1;
  104. var f2c:Closure = cast f2;
  105. return Runtime.refEq(f1c.obj, f2c.obj) && f1c.field == f2c.field;
  106. }
  107. return false;
  108. }
  109. public static function isObject( v : Dynamic ) : Bool
  110. {
  111. 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));
  112. }
  113. public static function isEnumValue( v : Dynamic ) : Bool {
  114. return v != null && (Std.is(v, HxEnum) || Std.is(v, java.lang.Enum));
  115. }
  116. public static function deleteField( o : Dynamic, field : String ) : Bool
  117. {
  118. return (Std.is(o, DynamicObject) && (o : DynamicObject).__hx_deleteField(field));
  119. }
  120. public static function copy<T>( o : T ) : T
  121. {
  122. var o2 : Dynamic = {};
  123. for( f in Reflect.fields(o) )
  124. Reflect.setField(o2,f,Reflect.field(o,f));
  125. return cast o2;
  126. }
  127. @:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
  128. public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic
  129. {
  130. return new VarArgsFunction(f);
  131. }
  132. }