PageRenderTime 35ms CodeModel.GetById 6ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

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

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