PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/mirrors/NativeMethod.java

http://ambienttalk.googlecode.com/
Java | 172 lines | 87 code | 22 blank | 63 comment | 4 complexity | 680d44667115d53e29175cf67719b7bb 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 java.lang.reflect.Method;
 31import java.util.Vector;
 32
 33import edu.vub.at.eval.Evaluator;
 34import edu.vub.at.exceptions.InterpreterException;
 35import edu.vub.at.exceptions.XTypeMismatch;
 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.natives.NATByRef;
 45import edu.vub.at.objects.natives.NATClosure;
 46import edu.vub.at.objects.natives.NATContext;
 47import edu.vub.at.objects.natives.NATTable;
 48import edu.vub.at.objects.natives.NATText;
 49import edu.vub.at.objects.natives.grammar.AGBegin;
 50import edu.vub.at.objects.natives.grammar.AGSymbol;
 51import edu.vub.at.signals.natives.NATLiftedBehavior;
 52
 53/**
 54 * A NativeMethod is a wrapper around a Java method allowing it to be selected 
 55 * from native base-level objects and passed around as an ordinary object.
 56 * 
 57 * @author tvcutsem
 58 * @author smostinc
 59 */
 60public final class NativeMethod extends NATByRef implements ATMethod {
 61
 62	/**
 63	 * Some methods do not need to be lifted implicitly when they are being applied to
 64	 * a behavior.  When defining one's own methods, this can be achieved by supplying
 65	 * an annotation to indicate that the method is already lifted. 
 66	 * 
 67	 * For methods which are implemented natively using base_methods,   this is not an
 68	 * option, as these methods have no real annotations.  However, methods to reflect
 69	 * on a behavior do not need to be lifted explicitly, for instance.
 70	 * 
 71	 * Such natively implemented methods which are to be considered Lifted are present
 72	 * in the Vector that is maintained below.
 73	 */
 74	private static final Vector _LIFTED_ = new Vector();
 75	
 76	static {
 77		_LIFTED_.add(AGSymbol.jAlloc("init"));
 78		_LIFTED_.add(AGSymbol.jAlloc("reflect:"));
 79		_LIFTED_.add(AGSymbol.jAlloc("createMirror"));
 80		_LIFTED_.add(AGSymbol.jAlloc("tagsOf:"));
 81		_LIFTED_.add(AGSymbol.jAlloc("value:"));
 82	}
 83	
 84	
 85	private final Method javaMethod_;
 86	private final ATSymbol name_;
 87	// native object from which the java method was selected
 88	private final ATObject nativeReceiver_;
 89	
 90	/**
 91	 * Construct a new wrapper object from a Java method.
 92	 * @param javaMethod the java method to be wrapped.
 93	 * @param name the original name of the method as an AmbientTalk symbol
 94	 */
 95	public NativeMethod(Method javaMethod, ATSymbol name, ATObject jReceiver) {
 96		javaMethod_ = javaMethod;
 97		name_ = name;
 98		nativeReceiver_ = jReceiver;
 99	}
100
101	public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
102		return new NativeClosure(this, new NATContext(lexicalScope, dynamicReceiver));
103	}
104	
105	/**
106	 * The name of a wrapped Java method is the name of the Java method, converted to an
107	 * AmbientTalk selector name.
108	 */
109	public ATSymbol base_name() throws InterpreterException {
110		return name_;
111	}
112	
113	/**
114	 * The parameters of a wrapped method are represented as symbols
115	 * representing the class name of the parameter type.
116	 */
117	public ATTable base_parameters() throws InterpreterException {
118		Class[] paramTypes = javaMethod_.getParameterTypes();
119		AGSymbol[] paramNames = new AGSymbol[paramTypes.length];
120		for (int i = 0; i < paramTypes.length; i++) {
121			paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i]));
122		}
123		return NATTable.atValue(paramNames);
124	}
125
126	public ATBegin base_bodyExpression() {
127		return new AGBegin(NATTable.atValue(new ATObject[] {
128				NATText.atValue("Native implementation of " + javaMethod_.toString())}));
129	}
130	
131	public ATTable base_annotations() throws InterpreterException {
132		return NATTable.EMPTY;
133	}
134	
135	public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
136		// If the method is not Lifted (and can hence be applied directly even if the arguments
137		// might contain one or more behaviors)
138		if(! _LIFTED_.contains(name_)) {
139			// Test whether the method shoulod be lifted or not
140			boolean		shouldBeLifted	= false;
141			ATObject[] 	argumentsArray 	= arguments.asNativeTable().elements_;
142			
143			for (int argIndex = 0; argIndex < argumentsArray.length; argIndex++) {
144				ATObject currentArgument = argumentsArray[argIndex];
145			
146				if (currentArgument.isBehavior()) {
147					return NATLiftedBehavior.fromClosure(new NATClosure(this, ctx), argumentsArray);
148				}
149			}
150		} // else (if the method is a lifted method or none of the arguments is a behavior)
151		
152		return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, nativeReceiver_,
153						                                 arguments.asNativeTable().elements_);
154	}
155	
156	public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
157		return base_apply(arguments, ctx);
158	}
159
160	public ATMethod asMethod() throws XTypeMismatch {
161		return this;
162	}
163	
164	public NATText meta_print() throws InterpreterException {
165		return NATText.atValue("<native method:"+name_+">");
166	}
167	
168    public ATTable meta_typeTags() throws InterpreterException {
169    	return NATTable.of(NativeTypeTags._METHOD_);
170    }
171	
172}