PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/symbiosis/JavaField.java

http://ambienttalk.googlecode.com/
Java | 113 lines | 62 code | 15 blank | 36 comment | 5 complexity | fa2ad619ce3981d2afa0d680a96c29a0 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * JavaField.java created on 5-nov-2006 at 20:08:18
  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.symbiosis;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XIllegalArgument;
 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.mirrors.Reflection;
 40import edu.vub.at.objects.natives.NATByRef;
 41import edu.vub.at.objects.natives.NATField;
 42import edu.vub.at.objects.natives.NATNumber;
 43import edu.vub.at.objects.natives.NATTable;
 44import edu.vub.at.objects.natives.NATText;
 45
 46import java.lang.reflect.Field;
 47
 48/**
 49 * A JavaField is a simple wrapper around a native java.lang.reflect.Field
 50 * 
 51 * @author tvcutsem
 52 */
 53public final class JavaField extends NATByRef implements ATField {
 54
 55	private final Object host_;
 56	private final Field field_;
 57
 58	public JavaField(Object host, Field f) {
 59		host_ = host;
 60		field_ = f;
 61	}
 62
 63	public ATSymbol base_name() {
 64		return Reflection.downSelector(field_.getName());
 65	}
 66
 67	public ATObject base_readField() throws InterpreterException {
 68		return Symbiosis.readField(host_, field_);
 69	}
 70
 71	public ATObject base_writeField(ATObject newValue) throws InterpreterException {
 72		Symbiosis.writeField(host_, field_, newValue);
 73		return Evaluator.getNil();
 74	}
 75
 76	public NATText meta_print() throws InterpreterException {
 77		return NATText.atValue("<java field:"+field_+">");
 78	}
 79	
 80	public ATField asField() {
 81		return this;
 82	}
 83	
 84	/**
 85	 * Fields can be re-initialized when installed in an object that is being cloned.
 86	 * They expect the new owner of the field as the sole instance to their 'new' method
 87	 */
 88	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
 89		if (initargs.base_length() != NATNumber.ONE) {
 90			return super.meta_newInstance(initargs);
 91		} else {
 92			ATObject newhost = initargs.base_at(NATNumber.ONE);
 93			if (newhost.isJavaObjectUnderSymbiosis()) {
 94				return new JavaField(newhost.asJavaObjectUnderSymbiosis().getWrappedObject(), field_);
 95			} else {
 96				throw new XIllegalArgument("Java Field re-initialization requires a symbiotic Java object, given " + newhost);
 97			}
 98		}
 99	}
100	
101    public ATTable meta_typeTags() throws InterpreterException {
102    	return NATTable.of(NativeTypeTags._FIELD_);
103    }
104
105    public ATMethod base_accessor() throws InterpreterException {
106    	return NATField.accessorForField(this);
107    }
108    
109    public ATMethod base_mutator() throws InterpreterException {
110    	return NATField.mutatorForField(this);
111    }
112    	
113}