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

http://ambienttalk.googlecode.com/ · Java · 146 lines · 83 code · 18 blank · 45 comment · 7 complexity · 50153c76f0a0f8a11b304d8781085312 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * JavaMethod.java created on 5-nov-2006 at 20:08:39
  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.Method;
  30. import edu.vub.at.eval.Evaluator;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.exceptions.XTypeMismatch;
  33. import edu.vub.at.objects.ATBoolean;
  34. import edu.vub.at.objects.ATClosure;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATMethod;
  37. import edu.vub.at.objects.ATObject;
  38. import edu.vub.at.objects.ATTable;
  39. import edu.vub.at.objects.coercion.NativeTypeTags;
  40. import edu.vub.at.objects.grammar.ATBegin;
  41. import edu.vub.at.objects.grammar.ATSymbol;
  42. import edu.vub.at.objects.mirrors.Reflection;
  43. import edu.vub.at.objects.natives.NATBoolean;
  44. import edu.vub.at.objects.natives.NATByRef;
  45. import edu.vub.at.objects.natives.NATContext;
  46. import edu.vub.at.objects.natives.NATTable;
  47. import edu.vub.at.objects.natives.NATText;
  48. import edu.vub.at.objects.natives.grammar.AGBegin;
  49. /**
  50. * JavaMethod is a wrapper class encapsulating one or more java.lang.reflect.Method objects.
  51. * The receiver is to be supplied during method application.
  52. *
  53. * All methods in the choices array should be overloaded versions of the same method
  54. * (i.e. they should have the same selector). The choices array should never be empty!
  55. *
  56. * JavaMethod objects must be constant, they are globally cached for all actors to use.
  57. *
  58. * @author tvcutsem
  59. */
  60. public final class JavaMethod extends NATByRef implements ATMethod {
  61. protected final Method[] choices_;
  62. public JavaMethod(Method[] choices) {
  63. // assertion
  64. if (choices.length == 0) { throw new RuntimeException("assertion failed"); }
  65. choices_ = choices;
  66. }
  67. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  68. ATObject wrapper = ctx.base_lexicalScope();
  69. Object receiver;
  70. if (wrapper.isJavaObjectUnderSymbiosis()) {
  71. receiver = wrapper.asJavaObjectUnderSymbiosis().getWrappedObject();
  72. } else {
  73. // static invocations do not require a receiver
  74. receiver = null;
  75. }
  76. return Symbiosis.symbioticInvocation(wrapper, receiver, choices_[0].getName(), this, arguments.asNativeTable().elements_);
  77. }
  78. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  79. return base_apply(arguments, ctx);
  80. }
  81. public ATBegin base_bodyExpression() throws InterpreterException {
  82. // list all of the method signatures of the (possibly overloaded) Java method
  83. StringBuffer buff = new StringBuffer("Java implementation of: ");
  84. for (int i = 0; i < choices_.length; i++) {
  85. buff.append("\n");
  86. buff.append(choices_[i].toString());
  87. }
  88. buff.append("\n");
  89. return new AGBegin(NATTable.atValue(new ATObject[] { NATText.atValue(buff.toString()) }));
  90. }
  91. public ATSymbol base_name() throws InterpreterException {
  92. return Reflection.downSelector(choices_[0].getName());
  93. }
  94. public ATTable base_parameters() throws InterpreterException {
  95. return Evaluator._ANON_MTH_ARGS_;
  96. }
  97. public ATTable base_annotations() throws InterpreterException {
  98. return NATTable.EMPTY;
  99. }
  100. public NATText meta_print() throws InterpreterException {
  101. return NATText.atValue("<java method:"+choices_[0].getName()+">");
  102. }
  103. public ATTable meta_typeTags() throws InterpreterException {
  104. return NATTable.of(NativeTypeTags._METHOD_);
  105. }
  106. public ATMethod asMethod() throws XTypeMismatch {
  107. return this;
  108. }
  109. public boolean isJavaMethodUnderSymbiosis() { return true; }
  110. public JavaMethod asJavaMethodUnderSymbiosis() { return this; }
  111. /**
  112. * Two JavaMethod instances are equal if they both represent a set of methods
  113. * from the same declaring class with the same selector.
  114. */
  115. public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException {
  116. if (comparand.isJavaMethodUnderSymbiosis()) {
  117. JavaMethod mth = comparand.asJavaMethodUnderSymbiosis();
  118. return NATBoolean.atValue(
  119. (mth.choices_[0].getDeclaringClass().equals(choices_[0].getDeclaringClass())) &&
  120. mth.choices_[0].getName().equals(choices_[0].getName()));
  121. } else {
  122. return NATBoolean._FALSE_;
  123. }
  124. }
  125. public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) {
  126. return new JavaClosure(this, new NATContext(lexicalScope,dynamicReceiver));
  127. }
  128. }