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

http://ambienttalk.googlecode.com/ · Java · 109 lines · 48 code · 14 blank · 47 comment · 0 complexity · e4c782c5e4abeb93e8b583673cd5bdf8 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NativeAnonymousMethod.java created on 10-aug-2006 at 10:03:55
  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.XIllegalApplication;
  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.NATClosure;
  42. import edu.vub.at.objects.natives.NATTable;
  43. import edu.vub.at.objects.natives.NATText;
  44. import edu.vub.at.objects.natives.grammar.AGBegin;
  45. import edu.vub.at.objects.natives.grammar.AGSymbol;
  46. /**
  47. * A NativeAnonymousMethod represents the meta_apply method of an anonymous NativeClosure subclass.
  48. *
  49. * An anonymous native method has the following properties:
  50. * - name = "nativelambda" (to distinguish it from 'ordinary' lambdas)
  51. * - arguments = [ \@args ] (it takes an arbitrary number of arguments)
  52. * - body = "Native implementation in {class}" (a string telling an inspector that
  53. * this closure is natively implemented in the given Java class)
  54. * - applying a nativelambda directly (without going through this NativeClosure)
  55. * results in an error
  56. *
  57. * @author tvcutsem
  58. */
  59. public class NativeAnonymousMethod extends NATByRef implements ATMethod {
  60. private final Class creatorClass_;
  61. /**
  62. * @param creatorClass class of the object that has created the NativeClosure that will wrap <tt>this</tt>
  63. */
  64. public NativeAnonymousMethod(Class creatorClass) {
  65. creatorClass_ = creatorClass;
  66. }
  67. /**
  68. * It is an error to directly apply an anonymous method. The closure must be applied instead.
  69. * @throws XIllegalApplication because an anonymous method must be applied through its wrapping closure.
  70. */
  71. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  72. throw new XIllegalApplication("Cannot apply an anonymous native method. Apply the closure instead.", creatorClass_);
  73. }
  74. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  75. return base_apply(arguments, ctx);
  76. }
  77. public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
  78. return new NATClosure(this, lexicalScope, dynamicReceiver);
  79. }
  80. public ATSymbol base_name() { return Evaluator._ANON_MTH_NAM_; }
  81. public ATTable base_parameters() { return Evaluator._ANON_MTH_ARGS_; }
  82. public ATBegin base_bodyExpression() {
  83. return new AGBegin(NATTable.atValue(new ATObject[] {
  84. AGSymbol.jAlloc("Native anonymous implementation in " + creatorClass_.getName())}));
  85. }
  86. public ATTable base_annotations() throws InterpreterException {
  87. return NATTable.EMPTY;
  88. }
  89. public NATText meta_print() throws InterpreterException {
  90. return NATText.atValue("<anonymous java method in "+creatorClass_.getName()+">");
  91. }
  92. public ATTable meta_typeTags() throws InterpreterException {
  93. return NATTable.of(NativeTypeTags._METHOD_);
  94. }
  95. }