PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/java/fan/sys/FanObj.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 249 lines | 189 code | 33 blank | 27 comment | 40 complexity | 22934cc479be92fa0550ff79fde7852e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 2 Dec 05 Brian Frank Creation
  7. //
  8. package fan.sys;
  9. import java.math.*;
  10. import fanx.util.*;
  11. /**
  12. * FanObj is the root class of all classes in Fantom - it is the class
  13. * representation of Obj.
  14. */
  15. public class FanObj
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. // Java
  19. //////////////////////////////////////////////////////////////////////////
  20. public int hashCode()
  21. {
  22. long hash = hash();
  23. return (int)(hash ^ (hash >>> 32));
  24. }
  25. public final String toString()
  26. {
  27. return toStr();
  28. }
  29. //////////////////////////////////////////////////////////////////////////
  30. // Identity
  31. //////////////////////////////////////////////////////////////////////////
  32. public static boolean equals(Object self, Object x)
  33. {
  34. return self.equals(x);
  35. }
  36. public static long compare(Object self, Object x)
  37. {
  38. if (self instanceof FanObj)
  39. return ((FanObj)self).compare(x);
  40. else if (self instanceof Comparable)
  41. return ((Comparable)self).compareTo(x);
  42. else
  43. return FanStr.compare(toStr(self), toStr(x));
  44. }
  45. public long compare(Object obj)
  46. {
  47. return FanStr.compare(toStr(), toStr(obj));
  48. }
  49. public static long hash(Object self)
  50. {
  51. if (self instanceof FanObj)
  52. return ((FanObj)self).hash();
  53. else
  54. return self.hashCode();
  55. }
  56. public long hash()
  57. {
  58. return super.hashCode();
  59. }
  60. public static String toStr(Object self)
  61. {
  62. if (self instanceof FanObj)
  63. return ((FanObj)self).toStr();
  64. else if (self instanceof Err)
  65. return ((Err)self).toStr();
  66. else if (self.getClass() == java.lang.Double.class)
  67. return FanFloat.toStr(((java.lang.Double)self).doubleValue());
  68. else
  69. return self.toString();
  70. }
  71. public String toStr()
  72. {
  73. return super.toString();
  74. }
  75. public static boolean isImmutable(Object self)
  76. {
  77. if (self instanceof FanObj)
  78. return ((FanObj)self).isImmutable();
  79. else if (self == null)
  80. return true;
  81. else if (self instanceof Err)
  82. return true;
  83. else
  84. return FanUtil.isJavaImmutable(self.getClass());
  85. }
  86. public boolean isImmutable()
  87. {
  88. try
  89. {
  90. return typeof().isConst();
  91. }
  92. catch (NullPointerException e)
  93. {
  94. // there are cases where accessing the type in a static initializer
  95. // can happen before the type is configured; since static init problems
  96. // are tricky to debug just make sure we dump some diagnostics
  97. Err err = Err.make("Calling Obj.isImmutable in static initializers before type are available");
  98. err.trace();
  99. throw err;
  100. }
  101. }
  102. public static Object toImmutable(Object self)
  103. {
  104. if (self == null) return null;
  105. if (self instanceof FanObj)
  106. return ((FanObj)self).toImmutable();
  107. else if (self instanceof Err)
  108. return self;
  109. else if (FanUtil.isJavaImmutable(self.getClass()))
  110. return self;
  111. throw NotImmutableErr.make(self.getClass().getName());
  112. }
  113. public Object toImmutable()
  114. {
  115. if (typeof().isConst()) return this;
  116. throw NotImmutableErr.make(typeof().toString());
  117. }
  118. public static Type typeof(Object self)
  119. {
  120. if (self instanceof FanObj)
  121. return ((FanObj)self).typeof();
  122. else if (self instanceof Err)
  123. return ((Err)self).typeof();
  124. else
  125. return FanUtil.toFanType(self.getClass(), true);
  126. }
  127. public Type typeof()
  128. {
  129. return Sys.ObjType;
  130. }
  131. public static Object with(Object self, Func f)
  132. {
  133. if (self instanceof FanObj)
  134. {
  135. return ((FanObj)self).with(f);
  136. }
  137. else
  138. {
  139. f.call(self);
  140. return self;
  141. }
  142. }
  143. public Object with(Func f) { f.call(this); return this; }
  144. public static Object trap(Object self, String name)
  145. {
  146. if (self instanceof FanObj)
  147. return ((FanObj)self).trap(name, (List)null);
  148. else
  149. return doTrap(self, name, null, typeof(self));
  150. }
  151. public static Object trap(Object self, String name, List args)
  152. {
  153. if (self instanceof FanObj)
  154. return ((FanObj)self).trap(name, args);
  155. else
  156. return doTrap(self, name, args, typeof(self));
  157. }
  158. public Object trap(String name) { return doTrap(this, name, null, typeof()); }
  159. public Object trap(String name, List args) { return doTrap(this, name, args, typeof()); }
  160. private static Object doTrap(Object self, String name, List args, Type type)
  161. {
  162. Slot slot = type.slot(name, true);
  163. if (slot instanceof Method)
  164. {
  165. Method m = (Method)slot;
  166. return m.func.callOn(self, args);
  167. }
  168. else
  169. {
  170. // handle FFI field overloaded with a method
  171. Field f = (Field)slot;
  172. if (f.overload != null)
  173. return f.overload.func.callOn(self, args);
  174. // zero args -> getter
  175. int argSize = (args == null) ? 0 : args.sz();
  176. if (argSize == 0)
  177. {
  178. return f.get(self);
  179. }
  180. // one arg -> setter
  181. if (argSize == 1)
  182. {
  183. Object val = args.get(0);
  184. f.set(self, val);
  185. return val;
  186. }
  187. throw ArgErr.make("Invalid number of args to get or set field '" + name + "'");
  188. }
  189. }
  190. // Remap all java.lang.Objects as statics since we emit to FanObj
  191. public static String toString(Object o) { return o.toString(); }
  192. public static Class getClass(Object o) { return o.getClass(); }
  193. public static int hashCode(Object o) { return o.hashCode(); }
  194. public static void notify(Object o) { o.notify(); }
  195. public static void notifyAll(Object o) { o.notifyAll(); }
  196. public static void wait(Object o) throws InterruptedException { o.wait(); }
  197. public static void wait(Object o, long t) throws InterruptedException { o.wait(t); }
  198. public static void wait(Object o, long t, int n) throws InterruptedException { o.wait(t, n); }
  199. //////////////////////////////////////////////////////////////////////////
  200. // Utils
  201. //////////////////////////////////////////////////////////////////////////
  202. public static void echo() { echo(""); }
  203. public static void echo(Object obj)
  204. {
  205. if (obj == null) obj = "null";
  206. String str = toStr(obj);
  207. try
  208. {
  209. Env.cur().out().printLine(str);
  210. }
  211. catch (Throwable e)
  212. {
  213. System.out.println(str);
  214. }
  215. }
  216. }