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

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/mirrors/NativeMethod.java

http://ambienttalk.googlecode.com/
Java | 129 lines | 65 code | 16 blank | 48 comment | 1 complexity | ac2504aae2952f9f75a304883c5aa44b MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NativeMethod.java created on Jul 27, 2006 at 1:35:19 AM
  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.mirrors;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATMethod;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.coercion.NativeTypeTags;
 39import edu.vub.at.objects.grammar.ATBegin;
 40import edu.vub.at.objects.grammar.ATSymbol;
 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;
 45import edu.vub.at.objects.natives.grammar.AGSymbol;
 46
 47import java.lang.reflect.Method;
 48
 49/**
 50 * A NativeMethod is a wrapper around a Java method allowing it to be selected 
 51 * from native base-level objects and passed around as an ordinary object.
 52 * 
 53 * @author tvcutsem
 54 * @author smostinc
 55 */
 56public final class NativeMethod extends NATByRef implements ATMethod {
 57
 58	private final Method javaMethod_;
 59	private final ATSymbol name_;
 60	// native object from which the java method was selected
 61	private final ATObject nativeReceiver_;
 62	
 63	/**
 64	 * Construct a new wrapper object from a Java method.
 65	 * @param javaMethod the java method to be wrapped.
 66	 * @param name the original name of the method as an AmbientTalk symbol
 67	 */
 68	public NativeMethod(Method javaMethod, ATSymbol name, ATObject jReceiver) {
 69		javaMethod_ = javaMethod;
 70		name_ = name;
 71		nativeReceiver_ = jReceiver;
 72	}
 73
 74	public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
 75		return new NativeClosure(lexicalScope, this);
 76	}
 77	
 78	/**
 79	 * The name of a wrapped Java method is the name of the Java method, converted to an
 80	 * AmbientTalk selector name.
 81	 */
 82	public ATSymbol base_name() throws InterpreterException {
 83		return name_;
 84	}
 85	
 86	/**
 87	 * The parameters of a wrapped method are represented as symbols
 88	 * representing the class name of the parameter type.
 89	 */
 90	public ATTable base_parameters() throws InterpreterException {
 91		Class[] paramTypes = javaMethod_.getParameterTypes();
 92		AGSymbol[] paramNames = new AGSymbol[paramTypes.length];
 93		for (int i = 0; i < paramTypes.length; i++) {
 94			paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i]));
 95		}
 96		return NATTable.atValue(paramNames);
 97	}
 98
 99	public ATBegin base_bodyExpression() {
100		return new AGBegin(NATTable.atValue(new ATObject[] {
101				NATText.atValue("Native implementation of " + javaMethod_.toString())}));
102	}
103	
104	public ATTable base_annotations() throws InterpreterException {
105		return NATTable.EMPTY;
106	}
107	
108	public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
109		return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, nativeReceiver_,
110						                                 arguments.asNativeTable().elements_);
111	}
112	
113	public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
114		return base_apply(arguments, ctx);
115	}
116
117	public ATMethod asMethod() throws XTypeMismatch {
118		return this;
119	}
120	
121	public NATText meta_print() throws InterpreterException {
122		return NATText.atValue("<native method:"+name_+">");
123	}
124	
125    public ATTable meta_typeTags() throws InterpreterException {
126    	return NATTable.of(NativeTypeTags._METHOD_);
127    }
128	
129}