PageRenderTime 35ms CodeModel.GetById 16ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/symbiosis/JavaMethod.java

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