/interpreter/tags/at2-build190607/src/edu/vub/at/objects/mirrors/NativeMethod.java

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