/interpreter/tags/at2-build190607/src/edu/vub/at/objects/mirrors/NativeMethod.java
Java | 117 lines | 56 code | 14 blank | 47 comment | 1 complexity | 1d60e0864a51ca0d8604c21c42f36790 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.ATContext; 34import edu.vub.at.objects.ATMethod; 35import edu.vub.at.objects.ATObject; 36import edu.vub.at.objects.ATTable; 37import edu.vub.at.objects.coercion.NativeTypeTags; 38import edu.vub.at.objects.grammar.ATBegin; 39import edu.vub.at.objects.grammar.ATSymbol; 40import edu.vub.at.objects.natives.NATByRef; 41import edu.vub.at.objects.natives.NATTable; 42import edu.vub.at.objects.natives.NATText; 43import edu.vub.at.objects.natives.grammar.AGBegin; 44import edu.vub.at.objects.natives.grammar.AGSymbol; 45 46import java.lang.reflect.Method; 47 48/** 49 * A NativeMethod is a wrapper around a Java method allowing it to be selected 50 * from native base-level objects and passed around as an ordinary object. 51 * 52 * @author tvcutsem 53 * @author smostinc 54 */ 55public final class NativeMethod extends NATByRef implements ATMethod { 56 57 private final Method javaMethod_; 58 private final ATSymbol name_; 59 60 /** 61 * Construct a new wrapper object from a Java method. 62 * @param javaMethod the java method to be wrapped. 63 * @param isMeta signifies whether or not the wrapped method signifies an AmbientTalk meta-level method 64 */ 65 public NativeMethod(Method javaMethod, ATSymbol name) { 66 javaMethod_ = javaMethod; 67 name_ = name; 68 } 69 70 /** 71 * The name of a wrapped Java method is the name of the Java method, converted to an 72 * AmbientTalk selector name. 73 */ 74 public ATSymbol base_getName() throws InterpreterException { 75 return name_; 76 } 77 78 /** 79 * The parameters of a wrapped method are represented as symbols 80 * representing the class name of the parameter type. 81 */ 82 public ATTable base_getParameters() throws InterpreterException { 83 Class[] paramTypes = javaMethod_.getParameterTypes(); 84 AGSymbol[] paramNames = new AGSymbol[paramTypes.length]; 85 for (int i = 0; i < paramTypes.length; i++) { 86 paramNames[i] = AGSymbol.jAlloc(Evaluator.valueNameOf(paramTypes[i])); 87 } 88 return NATTable.atValue(paramNames); 89 } 90 91 public ATBegin base_getBodyExpression() { 92 return new AGBegin(NATTable.atValue(new ATObject[] { 93 NATText.atValue("Native implementation of " + javaMethod_.toString())})); 94 } 95 96 public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException { 97 return JavaInterfaceAdaptor.invokeNativeATMethod(javaMethod_, ctx.base_getLexicalScope(), 98 arguments.asNativeTable().elements_); 99 } 100 101 public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException { 102 return base_apply(arguments, ctx); 103 } 104 105 public ATMethod asMethod() throws XTypeMismatch { 106 return this; 107 } 108 109 public NATText meta_print() throws InterpreterException { 110 return NATText.atValue("<native method:"+name_+">"); 111 } 112 113 public ATTable meta_getTypeTags() throws InterpreterException { 114 return NATTable.of(NativeTypeTags._METHOD_); 115 } 116 117}