/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
- /**
- * AmbientTalk/2 Project
- * NativeMethod.java created on Jul 27, 2006 at 1:35:19 AM
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.objects.mirrors;
- import java.lang.reflect.Method;
- import java.util.Vector;
- import edu.vub.at.eval.Evaluator;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.exceptions.XTypeMismatch;
- import edu.vub.at.objects.ATClosure;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATMethod;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.coercion.NativeTypeTags;
- import edu.vub.at.objects.grammar.ATBegin;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.natives.NATByRef;
- import edu.vub.at.objects.natives.NATClosure;
- import edu.vub.at.objects.natives.NATContext;
- import edu.vub.at.objects.natives.NATTable;
- import edu.vub.at.objects.natives.NATText;
- import edu.vub.at.objects.natives.grammar.AGBegin;
- import edu.vub.at.objects.natives.grammar.AGSymbol;
- import edu.vub.at.signals.natives.NATLiftedBehavior;
- /**
- * A NativeMethod is a wrapper around a Java method allowing it to be selected
- * from native base-level objects and passed around as an ordinary object.
- *
- * @author tvcutsem
- * @author smostinc
- */
- public final class NativeMethod extends NATByRef implements ATMethod {
- /**
- * Some methods do not need to be lifted implicitly when they are being applied to
- * a behavior. When defining one's own methods, this can be achieved by supplying
- * an annotation to indicate that the method is already lifted.
- *
- * For methods which are implemented natively using base_methods, this is not an
- * option, as these methods have no real annotations. However, methods to reflect
- * on a behavior do not need to be lifted explicitly, for instance.
- *
- * Such natively implemented methods which are to be considered Lifted are present
- * in the Vector that is maintained below.
- */
- private static final Vector _LIFTED_ = new Vector();
-
- static {
- _LIFTED_.add(AGSymbol.jAlloc("init"));
- _LIFTED_.add(AGSymbol.jAlloc("reflect:"));
- _LIFTED_.add(AGSymbol.jAlloc("createMirror"));
- _LIFTED_.add(AGSymbol.jAlloc("tagsOf:"));
- _LIFTED_.add(AGSymbol.jAlloc("value:"));
- }
-
-
- private final Method javaMethod_;
- private final ATSymbol name_;
- // native object from which the java method was selected
- private final ATObject nativeReceiver_;
-
- /**
- * Construct a new wrapper object from a Java method.
- * @param javaMethod the java method to be wrapped.
- * @param name the original name of the method as an AmbientTalk symbol
- */
- public NativeMethod(Method javaMethod, ATSymbol name, ATObject jReceiver) {
- javaMethod_ = javaMethod;
- name_ = name;
- nativeReceiver_ = jReceiver;
- }
- public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
- return new NativeClosure(this, new NATContext(lexicalScope, dynamicReceiver));
- }
-
- /**
- * The name of a wrapped Java method is the name of the Java method, converted to an
- * AmbientTalk selector name.
- */
- public ATSymbol base_name() throws InterpreterException {
- return name_;
- }
-
- /**
- * The parameters of a wrapped method are represented as symbols
- * representing the class name of the parameter type.
- */
- public ATTable base_parameters() throws InterpreterException {
- Class[] paramTypes = javaMethod_.getParameterTypes();
- AGSymbol[] paramNames = new AGSymbol[paramTypes.length];
- for (int i = 0; i < paramTypes.length; i++) {
- paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i]));
- }
- return NATTable.atValue(paramNames);
- }
- public ATBegin base_bodyExpression() {
- return new AGBegin(NATTable.atValue(new ATObject[] {
- NATText.atValue("Native implementation of " + javaMethod_.toString())}));
- }
-
- public ATTable base_annotations() throws InterpreterException {
- return NATTable.EMPTY;
- }
-
- public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
- // If the method is not Lifted (and can hence be applied directly even if the arguments
- // might contain one or more behaviors)
- if(! _LIFTED_.contains(name_)) {
- // Test whether the method shoulod be lifted or not
- boolean shouldBeLifted = false;
- ATObject[] argumentsArray = arguments.asNativeTable().elements_;
-
- for (int argIndex = 0; argIndex < argumentsArray.length; argIndex++) {
- ATObject currentArgument = argumentsArray[argIndex];
-
- if (currentArgument.isBehavior()) {
- return NATLiftedBehavior.fromClosure(new NATClosure(this, ctx), argumentsArray);
- }
- }
- } // else (if the method is a lifted method or none of the arguments is a behavior)
-
- return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, nativeReceiver_,
- arguments.asNativeTable().elements_);
- }
-
- public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
- return base_apply(arguments, ctx);
- }
- public ATMethod asMethod() throws XTypeMismatch {
- return this;
- }
-
- public NATText meta_print() throws InterpreterException {
- return NATText.atValue("<native method:"+name_+">");
- }
-
- public ATTable meta_typeTags() throws InterpreterException {
- return NATTable.of(NativeTypeTags._METHOD_);
- }
-
- }