/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/mirrors/NativeMethod.java

http://ambienttalk.googlecode.com/ · Java · 172 lines · 87 code · 22 blank · 63 comment · 4 complexity · 680d44667115d53e29175cf67719b7bb MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NativeMethod.java created on Jul 27, 2006 at 1:35:19 AM
  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.mirrors;
  29. import java.lang.reflect.Method;
  30. import java.util.Vector;
  31. import edu.vub.at.eval.Evaluator;
  32. import edu.vub.at.exceptions.InterpreterException;
  33. import edu.vub.at.exceptions.XTypeMismatch;
  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.natives.NATByRef;
  43. import edu.vub.at.objects.natives.NATClosure;
  44. import edu.vub.at.objects.natives.NATContext;
  45. import edu.vub.at.objects.natives.NATTable;
  46. import edu.vub.at.objects.natives.NATText;
  47. import edu.vub.at.objects.natives.grammar.AGBegin;
  48. import edu.vub.at.objects.natives.grammar.AGSymbol;
  49. import edu.vub.at.signals.natives.NATLiftedBehavior;
  50. /**
  51. * A NativeMethod is a wrapper around a Java method allowing it to be selected
  52. * from native base-level objects and passed around as an ordinary object.
  53. *
  54. * @author tvcutsem
  55. * @author smostinc
  56. */
  57. public final class NativeMethod extends NATByRef implements ATMethod {
  58. /**
  59. * Some methods do not need to be lifted implicitly when they are being applied to
  60. * a behavior. When defining one's own methods, this can be achieved by supplying
  61. * an annotation to indicate that the method is already lifted.
  62. *
  63. * For methods which are implemented natively using base_methods, this is not an
  64. * option, as these methods have no real annotations. However, methods to reflect
  65. * on a behavior do not need to be lifted explicitly, for instance.
  66. *
  67. * Such natively implemented methods which are to be considered Lifted are present
  68. * in the Vector that is maintained below.
  69. */
  70. private static final Vector _LIFTED_ = new Vector();
  71. static {
  72. _LIFTED_.add(AGSymbol.jAlloc("init"));
  73. _LIFTED_.add(AGSymbol.jAlloc("reflect:"));
  74. _LIFTED_.add(AGSymbol.jAlloc("createMirror"));
  75. _LIFTED_.add(AGSymbol.jAlloc("tagsOf:"));
  76. _LIFTED_.add(AGSymbol.jAlloc("value:"));
  77. }
  78. private final Method javaMethod_;
  79. private final ATSymbol name_;
  80. // native object from which the java method was selected
  81. private final ATObject nativeReceiver_;
  82. /**
  83. * Construct a new wrapper object from a Java method.
  84. * @param javaMethod the java method to be wrapped.
  85. * @param name the original name of the method as an AmbientTalk symbol
  86. */
  87. public NativeMethod(Method javaMethod, ATSymbol name, ATObject jReceiver) {
  88. javaMethod_ = javaMethod;
  89. name_ = name;
  90. nativeReceiver_ = jReceiver;
  91. }
  92. public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
  93. return new NativeClosure(this, new NATContext(lexicalScope, dynamicReceiver));
  94. }
  95. /**
  96. * The name of a wrapped Java method is the name of the Java method, converted to an
  97. * AmbientTalk selector name.
  98. */
  99. public ATSymbol base_name() throws InterpreterException {
  100. return name_;
  101. }
  102. /**
  103. * The parameters of a wrapped method are represented as symbols
  104. * representing the class name of the parameter type.
  105. */
  106. public ATTable base_parameters() throws InterpreterException {
  107. Class[] paramTypes = javaMethod_.getParameterTypes();
  108. AGSymbol[] paramNames = new AGSymbol[paramTypes.length];
  109. for (int i = 0; i < paramTypes.length; i++) {
  110. paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i]));
  111. }
  112. return NATTable.atValue(paramNames);
  113. }
  114. public ATBegin base_bodyExpression() {
  115. return new AGBegin(NATTable.atValue(new ATObject[] {
  116. NATText.atValue("Native implementation of " + javaMethod_.toString())}));
  117. }
  118. public ATTable base_annotations() throws InterpreterException {
  119. return NATTable.EMPTY;
  120. }
  121. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  122. // If the method is not Lifted (and can hence be applied directly even if the arguments
  123. // might contain one or more behaviors)
  124. if(! _LIFTED_.contains(name_)) {
  125. // Test whether the method shoulod be lifted or not
  126. boolean shouldBeLifted = false;
  127. ATObject[] argumentsArray = arguments.asNativeTable().elements_;
  128. for (int argIndex = 0; argIndex < argumentsArray.length; argIndex++) {
  129. ATObject currentArgument = argumentsArray[argIndex];
  130. if (currentArgument.isBehavior()) {
  131. return NATLiftedBehavior.fromClosure(new NATClosure(this, ctx), argumentsArray);
  132. }
  133. }
  134. } // else (if the method is a lifted method or none of the arguments is a behavior)
  135. return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, nativeReceiver_,
  136. arguments.asNativeTable().elements_);
  137. }
  138. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  139. return base_apply(arguments, ctx);
  140. }
  141. public ATMethod asMethod() throws XTypeMismatch {
  142. return this;
  143. }
  144. public NATText meta_print() throws InterpreterException {
  145. return NATText.atValue("<native method:"+name_+">");
  146. }
  147. public ATTable meta_typeTags() throws InterpreterException {
  148. return NATTable.of(NativeTypeTags._METHOD_);
  149. }
  150. }