/interpreter/tags/at2dist110511/src/edu/vub/at/objects/symbiosis/JavaClosure.java

http://ambienttalk.googlecode.com/ · Java · 131 lines · 63 code · 15 blank · 53 comment · 14 complexity · 311e1ff571ee1898da0c1928747fe47c MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * JavaClosure.java created on 9-dec-2006 at 21:16:12
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.symbiosis;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XSymbiosisFailure;
  31. import edu.vub.at.objects.ATClosure;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.natives.NATClosure;
  35. import edu.vub.at.objects.natives.NATContext;
  36. import edu.vub.at.objects.natives.NATNumber;
  37. import edu.vub.at.objects.natives.NATTable;
  38. import edu.vub.at.objects.natives.NATText;
  39. import java.lang.reflect.Array;
  40. import java.lang.reflect.Method;
  41. import java.util.Vector;
  42. /**
  43. * A JavaClosure pairs a JavaMethod (a bundle of native overloaded java.lang.reflect.Method objects)
  44. * together with a Java object receiver.
  45. *
  46. * It also provides the possibility of casting the JavaMethod instances to fit a
  47. * narrower static type signature.
  48. *
  49. * @author tvcutsem
  50. */
  51. public final class JavaClosure extends NATClosure implements ATJavaClosure {
  52. private final ATObject scope_;
  53. public JavaClosure(ATObject scope, JavaMethod meth) {
  54. super(meth, null);
  55. scope_ = scope;
  56. }
  57. /**
  58. * Overridden to allow for lazy instantiation of the context.
  59. *
  60. * A 'default' context is lazily constructed and returned.
  61. */
  62. public ATContext base_context() throws InterpreterException {
  63. if (context_ == null)
  64. context_ = new NATContext(scope_, scope_);
  65. return context_;
  66. }
  67. public NATText meta_print() throws InterpreterException {
  68. return NATText.atValue("<java closure:"+base_method().base_name().base_text().asNativeText().javaValue+">");
  69. }
  70. /**
  71. * For each Method in the wrapped JavaMethod's choices_, check whether it is compatible with
  72. * the given types. If so, add it to the choices_ array of the new JavaMethod.
  73. */
  74. public ATClosure base_cast(ATObject[] types) throws InterpreterException {
  75. Method[] choices = ((JavaMethod) method_).choices_;
  76. // unwrap the JavaClass wrappers
  77. Class[] actualTypes = new Class[types.length];
  78. for (int i = 0; i < actualTypes.length; i++) {
  79. // Array types may be represented as one-arg tables of a type: [Type]
  80. // TODO: properly refactor the instanceof test
  81. // problem: cannot do base_isTable because JavaObject/JavaClass objects will say yes!
  82. if (types[i] instanceof NATTable) {
  83. // Array.newInstance([Type][1],0).getClass()
  84. actualTypes[i] = Array.newInstance(types[i].asTable().
  85. base_at(NATNumber.ONE).asJavaClassUnderSymbiosis().getWrappedClass(), 0).getClass();
  86. } else {
  87. actualTypes[i] = types[i].asJavaClassUnderSymbiosis().getWrappedClass();
  88. }
  89. }
  90. Vector matchingMethods = new Vector();
  91. for (int i = 0; i < choices.length; i++) {
  92. if(matches(choices[i].getParameterTypes(), actualTypes)) {
  93. matchingMethods.add(choices[i]);
  94. }
  95. }
  96. Method[] matches = (Method[]) matchingMethods.toArray(new Method[matchingMethods.size()]);
  97. if (matches.length > 0) {
  98. return new JavaClosure(scope_, new JavaMethod(matches));
  99. } else {
  100. throw new XSymbiosisFailure(scope_, choices[0], types);
  101. }
  102. }
  103. /**
  104. * Compares two Class arrays and returns true iff both arrays have equal size and all members are the same.
  105. */
  106. private static final boolean matches(Class[] formals, Class[] actuals) {
  107. if (formals.length != actuals.length)
  108. return false;
  109. for (int i = 0; i < formals.length; i++) {
  110. if (!(formals[i] == actuals[i])) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. }