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

http://ambienttalk.googlecode.com/ · Java · 132 lines · 60 code · 17 blank · 55 comment · 6 complexity · 6feeec5ee9b460e11b0a520969703c86 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATPrimitiveField.java created on Aug 2, 2006 at 12:47:02 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.XIllegalOperation;
  32. import edu.vub.at.objects.ATField;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.coercion.NativeTypeTags;
  36. import edu.vub.at.objects.grammar.ATSymbol;
  37. import edu.vub.at.objects.natives.NATByRef;
  38. import edu.vub.at.objects.natives.NATNumber;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. import java.lang.reflect.Method;
  42. /**
  43. * Native Fields are represented in our reflective implementation as a pair
  44. * of accessor and mutator methods in the class of the native AmbientTalk object.
  45. * For instance, a native AmbientTalk object of class C has a field 'f' if the
  46. * class C implements a method 'getF()'. If the class also implements 'setF(v)'
  47. * the field is assignable.
  48. *
  49. * @author tvcutsem
  50. * @author smostinc
  51. */
  52. public class NativeField extends NATByRef implements ATField {
  53. /**
  54. * The AmbientTalk name of the field
  55. */
  56. private final ATSymbol name_;
  57. /**
  58. * The AmbientTalk native object to which this field belongs
  59. */
  60. private final ATObject host_;
  61. /**
  62. * The native Java accessor method to be called when accessing the field
  63. */
  64. private final Method accessor_;
  65. /**
  66. * The native Java mutator method to be called when assigning to the field.
  67. * This field may be null which indicates a read-only field
  68. */
  69. private final Method mutator_;
  70. public NativeField(ATObject host, ATSymbol name, Method accessor, Method mutator) {
  71. host_ = host;
  72. name_ = name;
  73. accessor_ = accessor;
  74. mutator_ = mutator;
  75. }
  76. public ATSymbol base_name() {
  77. return name_;
  78. }
  79. public ATObject base_readField() throws InterpreterException {
  80. return JavaInterfaceAdaptor.invokeNativeATMethod(accessor_, host_, NATTable.EMPTY.elements_);
  81. }
  82. public ATObject base_writeField(ATObject newValue) throws InterpreterException {
  83. // certain fields may not have setters
  84. if (mutator_ != null) {
  85. JavaInterfaceAdaptor.invokeNativeATMethod(accessor_, host_, new ATObject[] { newValue });
  86. return Evaluator.getNil();
  87. } else {
  88. throw new XIllegalOperation("Field " + name_ + " cannot be set.");
  89. }
  90. }
  91. /**
  92. * Fields can be re-initialized when installed in an object that is being cloned.
  93. * They expect the new owner of the field as the sole instance to their 'new' method
  94. */
  95. public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
  96. if (initargs.base_length() != NATNumber.ONE) {
  97. return super.meta_newInstance(initargs);
  98. } else {
  99. ATObject newhost = initargs.base_at(NATNumber.ONE);
  100. return new NativeField(newhost, name_, accessor_, mutator_);
  101. }
  102. }
  103. public NATText meta_print() throws InterpreterException {
  104. return NATText.atValue("<native field:"+name_+" of "+ host_.meta_print().javaValue +">");
  105. }
  106. public ATTable meta_typeTags() throws InterpreterException {
  107. return NATTable.of(NativeTypeTags._FIELD_);
  108. }
  109. public boolean isNativeField() {
  110. return true;
  111. }
  112. public ATField asField() {
  113. return this;
  114. }
  115. }