PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 116 lines | 55 code | 13 blank | 48 comment | 12 complexity | 2b437488dc627eaf5725a9fbff3f4a03 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * JavaClosure.java created on 9-dec-2006 at 21:16:12
  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.Array;
 31import java.lang.reflect.Method;
 32import java.util.Vector;
 33
 34import edu.vub.at.exceptions.InterpreterException;
 35import edu.vub.at.exceptions.XSymbiosisFailure;
 36import edu.vub.at.objects.ATClosure;
 37import edu.vub.at.objects.ATContext;
 38import edu.vub.at.objects.ATObject;
 39import edu.vub.at.objects.natives.NATClosure;
 40import edu.vub.at.objects.natives.NATNumber;
 41import edu.vub.at.objects.natives.NATTable;
 42import edu.vub.at.objects.natives.NATText;
 43
 44/**
 45 * A JavaClosure pairs a JavaMethod (a bundle of native overloaded java.lang.reflect.Method objects)
 46 * together with a Java object receiver.
 47 * 
 48 * It also provides the possibility of casting the JavaMethod instances to fit a
 49 * narrower static type signature.
 50 * 
 51 * @author tvcutsem
 52 */
 53public final class JavaClosure extends NATClosure implements ATJavaClosure {
 54
 55	public JavaClosure(JavaMethod meth, ATContext ctx) {
 56		super(meth, ctx);
 57	}
 58	
 59	public NATText meta_print() throws InterpreterException {
 60		return NATText.atValue("<java closure:"+base_method().base_name().base_text().asNativeText().javaValue+">");
 61	}
 62
 63	/**
 64	 * For each Method in the wrapped JavaMethod's choices_, check whether it is compatible with
 65	 * the given types. If so, add it to the choices_ array of the new JavaMethod.
 66	 */
 67	public ATClosure base_cast(ATObject[] types) throws InterpreterException {
 68		Method[] choices = ((JavaMethod) method_).choices_;
 69		
 70		// unwrap the JavaClass wrappers
 71		Class[] actualTypes = new Class[types.length];
 72		for (int i = 0; i < actualTypes.length; i++) {
 73			// Array types may be represented as one-arg tables of a type: [Type]
 74			// TODO: properly refactor the instanceof test
 75			// problem: cannot do base_isTable because JavaObject/JavaClass objects will say yes!
 76			if (types[i] instanceof NATTable) {
 77				// Array.newInstance([Type][1],0).getClass()
 78				actualTypes[i] = Array.newInstance(types[i].asTable().
 79				    base_at(NATNumber.ONE).asJavaClassUnderSymbiosis().getWrappedClass(), 0).getClass();
 80			} else {
 81				actualTypes[i] = types[i].asJavaClassUnderSymbiosis().getWrappedClass();
 82			}
 83		}
 84		Vector matchingMethods = new Vector();
 85		
 86		for (int i = 0; i < choices.length; i++) {
 87			if(matches(choices[i].getParameterTypes(), actualTypes)) {
 88				matchingMethods.add(choices[i]);
 89			}
 90		}
 91		
 92		Method[] matches = (Method[]) matchingMethods.toArray(new Method[matchingMethods.size()]);
 93		if (matches.length > 0) {
 94			return new JavaClosure(new JavaMethod(matches), context_);
 95		} else {
 96			throw new XSymbiosisFailure(context_.base_lexicalScope(), choices[0], types);
 97		}
 98	}
 99	
100	/**
101	 * Compares two Class arrays and returns true iff both arrays have equal size and all members are the same.
102	 */
103	private static final boolean matches(Class[] formals, Class[] actuals) {
104		if (formals.length != actuals.length)
105			return false;
106		
107		for (int i = 0; i < formals.length; i++) {
108			if (!(formals[i] == actuals[i])) {
109				return false;
110			}
111		}
112		
113		return true;
114	}
115
116}