PageRenderTime 29ms CodeModel.GetById 23ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist041108/src/edu/vub/at/objects/mirrors/NativeField.java

http://ambienttalk.googlecode.com/
Java | 142 lines | 68 code | 19 blank | 55 comment | 6 complexity | 60cee5dfc3ff053cab315a5a3ef711af 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 */
 28package edu.vub.at.objects.mirrors;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XIllegalOperation;
 33import edu.vub.at.objects.ATField;
 34import edu.vub.at.objects.ATMethod;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.coercion.NativeTypeTags;
 38import edu.vub.at.objects.grammar.ATSymbol;
 39import edu.vub.at.objects.natives.NATByRef;
 40import edu.vub.at.objects.natives.NATField;
 41import edu.vub.at.objects.natives.NATNumber;
 42import edu.vub.at.objects.natives.NATTable;
 43import edu.vub.at.objects.natives.NATText;
 44
 45import java.lang.reflect.Method;
 46
 47/**
 48 * Native Fields are represented in our reflective implementation as a pair
 49 * of accessor and mutator methods in the class of the native AmbientTalk object.
 50 * For instance, a native AmbientTalk object of class C has a field 'f' if the
 51 * class C implements a method 'getF()'. If the class also implements 'setF(v)'
 52 * the field is assignable.
 53 * 
 54 * @author tvcutsem
 55 * @author smostinc
 56 */
 57public class NativeField extends NATByRef implements ATField {
 58	
 59	/**
 60	 * The AmbientTalk name of the field
 61	 */
 62	private final ATSymbol name_;
 63	
 64	/**
 65	 * The AmbientTalk native object to which this field belongs
 66	 */
 67	private final ATObject host_;
 68	
 69	/**
 70	 * The native Java accessor method to be called when accessing the field
 71	 */
 72	private final Method accessor_;
 73	
 74	/**
 75	 * The native Java mutator method to be called when assigning to the field.
 76	 * This field may be null which indicates a read-only field
 77	 */
 78	private final Method mutator_;
 79
 80	public NativeField(ATObject host, ATSymbol name, Method accessor, Method mutator) {
 81		host_ = host;
 82		name_ = name;
 83		accessor_ = accessor;
 84		mutator_ = mutator;
 85	}
 86
 87	public ATSymbol base_name() {
 88		return name_;
 89	}
 90
 91	public ATObject base_readField() throws InterpreterException {
 92		return JavaInterfaceAdaptor.invokeNativeATMethod(accessor_, host_, NATTable.EMPTY.elements_);
 93	}
 94
 95	public ATObject base_writeField(ATObject newValue) throws InterpreterException {
 96		// certain fields may not have setters
 97		if (mutator_ != null) {
 98			JavaInterfaceAdaptor.invokeNativeATMethod(accessor_, host_, new ATObject[] { newValue });
 99			return Evaluator.getNil();
100		} else {
101			throw new XIllegalOperation("Field " + name_ + " cannot be set.");
102		}
103	}
104	
105	/**
106	 * Fields can be re-initialized when installed in an object that is being cloned.
107	 * They expect the new owner of the field as the sole instance to their 'new' method
108	 */
109	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
110		if (initargs.base_length() != NATNumber.ONE) {
111			return super.meta_newInstance(initargs);
112		} else {
113		    ATObject newhost = initargs.base_at(NATNumber.ONE);
114		    return new NativeField(newhost, name_, accessor_, mutator_);
115		}
116	}
117
118	public NATText meta_print() throws InterpreterException {
119		return NATText.atValue("<native field:"+name_+" of "+ host_.meta_print().javaValue +">");
120	}
121	
122    public ATTable meta_typeTags() throws InterpreterException {
123    	return NATTable.of(NativeTypeTags._FIELD_);
124    }
125	
126    public boolean isNativeField() {
127        return true;
128    }
129	
130	public ATField asField() {
131		return this;
132	}
133
134    public ATMethod base_accessor() throws InterpreterException {
135    	return NATField.accessorForField(this);
136    }
137    
138    public ATMethod base_mutator() throws InterpreterException {
139    	return NATField.mutatorForField(this);
140    }
141	
142}