PageRenderTime 37ms CodeModel.GetById 7ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2dist030708/src/edu/vub/at/objects/symbiosis/JavaMethod.java

http://ambienttalk.googlecode.com/
Java | 145 lines | 82 code | 18 blank | 45 comment | 7 complexity | ff6c0a177845165c9914e2bd057326fe MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * JavaMethod.java created on 5-nov-2006 at 20:08:39
  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.XTypeMismatch;
 33import edu.vub.at.objects.ATBoolean;
 34import edu.vub.at.objects.ATClosure;
 35import edu.vub.at.objects.ATContext;
 36import edu.vub.at.objects.ATMethod;
 37import edu.vub.at.objects.ATObject;
 38import edu.vub.at.objects.ATTable;
 39import edu.vub.at.objects.coercion.NativeTypeTags;
 40import edu.vub.at.objects.grammar.ATBegin;
 41import edu.vub.at.objects.grammar.ATSymbol;
 42import edu.vub.at.objects.mirrors.Reflection;
 43import edu.vub.at.objects.natives.NATBoolean;
 44import edu.vub.at.objects.natives.NATByRef;
 45import edu.vub.at.objects.natives.NATTable;
 46import edu.vub.at.objects.natives.NATText;
 47import edu.vub.at.objects.natives.grammar.AGBegin;
 48
 49import java.lang.reflect.Method;
 50
 51/**
 52 * JavaMethod is a wrapper class encapsulating one or more java.lang.reflect.Method objects.
 53 * The receiver is to be supplied during method application.
 54 * 
 55 * All methods in the choices array should be overloaded versions of the same method
 56 * (i.e. they should have the same selector). The choices array should never be empty!
 57 *
 58 * JavaMethod objects must be constant, they are globally cached for all actors to use.
 59 *
 60 * @author tvcutsem
 61 */
 62public final class JavaMethod extends NATByRef implements ATMethod {
 63	
 64	protected final Method[] choices_;
 65	
 66	public JavaMethod(Method[] choices) {
 67		// assertion
 68		if (choices.length == 0) { throw new RuntimeException("assertion failed"); }
 69		choices_ = choices;
 70	}
 71	
 72	public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
 73		ATObject wrapper = ctx.base_receiver();
 74		Object receiver;
 75		if (wrapper.isJavaObjectUnderSymbiosis()) {
 76			receiver = wrapper.asJavaObjectUnderSymbiosis().getWrappedObject();
 77		} else {
 78			// static invocations do not require a receiver
 79			receiver = null;
 80		}
 81		return Symbiosis.symbioticInvocation(wrapper, receiver, choices_[0].getName(), this, arguments.asNativeTable().elements_);
 82	}
 83	
 84	public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
 85		return base_apply(arguments, ctx);
 86	}
 87	
 88	public ATBegin base_bodyExpression() throws InterpreterException {
 89		// list all of the method signatures of the (possibly overloaded) Java method
 90		StringBuffer buff = new StringBuffer("Java implementation of: ");
 91		for (int i = 0; i < choices_.length; i++) {
 92			buff.append("\n");
 93			buff.append(choices_[i].toString());
 94		}
 95		buff.append("\n");
 96		return new AGBegin(NATTable.atValue(new ATObject[] { NATText.atValue(buff.toString()) }));
 97	}
 98
 99	public ATSymbol base_name() throws InterpreterException {
100		return Reflection.downSelector(choices_[0].getName());
101	}
102
103	public ATTable base_parameters() throws InterpreterException {
104		return Evaluator._ANON_MTH_ARGS_;
105	}
106
107	public ATTable base_annotations() throws InterpreterException {
108		return NATTable.EMPTY;
109	}
110	
111	public NATText meta_print() throws InterpreterException {
112		return NATText.atValue("<java method:"+choices_[0].getName()+">");
113	}
114	
115    public ATTable meta_typeTags() throws InterpreterException {
116    	return NATTable.of(NativeTypeTags._METHOD_);
117    }
118	
119	public ATMethod asMethod() throws XTypeMismatch {
120		return this;
121	}
122	
123	public boolean isJavaMethodUnderSymbiosis() { return true; }
124	public JavaMethod asJavaMethodUnderSymbiosis() { return this; }
125	
126	/**
127	 * Two JavaMethod instances are equal if they both represent a set of methods
128	 * from the same declaring class with the same selector.
129	 */
130    public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException {
131		if (comparand.isJavaMethodUnderSymbiosis()) {
132			JavaMethod mth = comparand.asJavaMethodUnderSymbiosis();
133			return NATBoolean.atValue(
134					(mth.choices_[0].getDeclaringClass().equals(choices_[0].getDeclaringClass())) &&
135			         mth.choices_[0].getName().equals(choices_[0].getName()));
136		} else {
137			return NATBoolean._FALSE_;
138		}
139    }
140
141	public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) {
142		return new JavaClosure(lexicalScope, this);
143	}
144	
145}