/interpreter/tags/at2dist220411/src/edu/vub/at/objects/mirrors/NativeMethod.java

http://ambienttalk.googlecode.com/ · Java · 129 lines · 65 code · 16 blank · 48 comment · 1 complexity · ac2504aae2952f9f75a304883c5aa44b 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 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.ATClosure;
  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.NativeTypeTags;
  38. import edu.vub.at.objects.grammar.ATBegin;
  39. import edu.vub.at.objects.grammar.ATSymbol;
  40. import edu.vub.at.objects.natives.NATByRef;
  41. import edu.vub.at.objects.natives.NATTable;
  42. import edu.vub.at.objects.natives.NATText;
  43. import edu.vub.at.objects.natives.grammar.AGBegin;
  44. import edu.vub.at.objects.natives.grammar.AGSymbol;
  45. import java.lang.reflect.Method;
  46. /**
  47. * A NativeMethod is a wrapper around a Java method allowing it to be selected
  48. * from native base-level objects and passed around as an ordinary object.
  49. *
  50. * @author tvcutsem
  51. * @author smostinc
  52. */
  53. public final class NativeMethod extends NATByRef implements ATMethod {
  54. private final Method javaMethod_;
  55. private final ATSymbol name_;
  56. // native object from which the java method was selected
  57. private final ATObject nativeReceiver_;
  58. /**
  59. * Construct a new wrapper object from a Java method.
  60. * @param javaMethod the java method to be wrapped.
  61. * @param name the original name of the method as an AmbientTalk symbol
  62. */
  63. public NativeMethod(Method javaMethod, ATSymbol name, ATObject jReceiver) {
  64. javaMethod_ = javaMethod;
  65. name_ = name;
  66. nativeReceiver_ = jReceiver;
  67. }
  68. public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
  69. return new NativeClosure(lexicalScope, this);
  70. }
  71. /**
  72. * The name of a wrapped Java method is the name of the Java method, converted to an
  73. * AmbientTalk selector name.
  74. */
  75. public ATSymbol base_name() throws InterpreterException {
  76. return name_;
  77. }
  78. /**
  79. * The parameters of a wrapped method are represented as symbols
  80. * representing the class name of the parameter type.
  81. */
  82. public ATTable base_parameters() throws InterpreterException {
  83. Class[] paramTypes = javaMethod_.getParameterTypes();
  84. AGSymbol[] paramNames = new AGSymbol[paramTypes.length];
  85. for (int i = 0; i < paramTypes.length; i++) {
  86. paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i]));
  87. }
  88. return NATTable.atValue(paramNames);
  89. }
  90. public ATBegin base_bodyExpression() {
  91. return new AGBegin(NATTable.atValue(new ATObject[] {
  92. NATText.atValue("Native implementation of " + javaMethod_.toString())}));
  93. }
  94. public ATTable base_annotations() throws InterpreterException {
  95. return NATTable.EMPTY;
  96. }
  97. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  98. return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, nativeReceiver_,
  99. arguments.asNativeTable().elements_);
  100. }
  101. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  102. return base_apply(arguments, ctx);
  103. }
  104. public ATMethod asMethod() throws XTypeMismatch {
  105. return this;
  106. }
  107. public NATText meta_print() throws InterpreterException {
  108. return NATText.atValue("<native method:"+name_+">");
  109. }
  110. public ATTable meta_typeTags() throws InterpreterException {
  111. return NATTable.of(NativeTypeTags._METHOD_);
  112. }
  113. }