/interpreter/tags/at2-build060407/src/edu/vub/at/objects/symbiosis/JavaMethod.java

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