PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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

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