/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/symbiosis/JavaClosure.java

http://ambienttalk.googlecode.com/ · Java · 116 lines · 55 code · 13 blank · 48 comment · 12 complexity · 2b437488dc627eaf5725a9fbff3f4a03 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 java.lang.reflect.Array;
  30. import java.lang.reflect.Method;
  31. import java.util.Vector;
  32. import edu.vub.at.exceptions.InterpreterException;
  33. import edu.vub.at.exceptions.XSymbiosisFailure;
  34. import edu.vub.at.objects.ATClosure;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATObject;
  37. import edu.vub.at.objects.natives.NATClosure;
  38. import edu.vub.at.objects.natives.NATNumber;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. /**
  42. * A JavaClosure pairs a JavaMethod (a bundle of native overloaded java.lang.reflect.Method objects)
  43. * together with a Java object receiver.
  44. *
  45. * It also provides the possibility of casting the JavaMethod instances to fit a
  46. * narrower static type signature.
  47. *
  48. * @author tvcutsem
  49. */
  50. public final class JavaClosure extends NATClosure implements ATJavaClosure {
  51. public JavaClosure(JavaMethod meth, ATContext ctx) {
  52. super(meth, ctx);
  53. }
  54. public NATText meta_print() throws InterpreterException {
  55. return NATText.atValue("<java closure:"+base_method().base_name().base_text().asNativeText().javaValue+">");
  56. }
  57. /**
  58. * For each Method in the wrapped JavaMethod's choices_, check whether it is compatible with
  59. * the given types. If so, add it to the choices_ array of the new JavaMethod.
  60. */
  61. public ATClosure base_cast(ATObject[] types) throws InterpreterException {
  62. Method[] choices = ((JavaMethod) method_).choices_;
  63. // unwrap the JavaClass wrappers
  64. Class[] actualTypes = new Class[types.length];
  65. for (int i = 0; i < actualTypes.length; i++) {
  66. // Array types may be represented as one-arg tables of a type: [Type]
  67. // TODO: properly refactor the instanceof test
  68. // problem: cannot do base_isTable because JavaObject/JavaClass objects will say yes!
  69. if (types[i] instanceof NATTable) {
  70. // Array.newInstance([Type][1],0).getClass()
  71. actualTypes[i] = Array.newInstance(types[i].asTable().
  72. base_at(NATNumber.ONE).asJavaClassUnderSymbiosis().getWrappedClass(), 0).getClass();
  73. } else {
  74. actualTypes[i] = types[i].asJavaClassUnderSymbiosis().getWrappedClass();
  75. }
  76. }
  77. Vector matchingMethods = new Vector();
  78. for (int i = 0; i < choices.length; i++) {
  79. if(matches(choices[i].getParameterTypes(), actualTypes)) {
  80. matchingMethods.add(choices[i]);
  81. }
  82. }
  83. Method[] matches = (Method[]) matchingMethods.toArray(new Method[matchingMethods.size()]);
  84. if (matches.length > 0) {
  85. return new JavaClosure(new JavaMethod(matches), context_);
  86. } else {
  87. throw new XSymbiosisFailure(context_.base_lexicalScope(), choices[0], types);
  88. }
  89. }
  90. /**
  91. * Compares two Class arrays and returns true iff both arrays have equal size and all members are the same.
  92. */
  93. private static final boolean matches(Class[] formals, Class[] actuals) {
  94. if (formals.length != actuals.length)
  95. return false;
  96. for (int i = 0; i < formals.length; i++) {
  97. if (!(formals[i] == actuals[i])) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. }