edu.vub.at.objects.mirrors
Class JavaInterfaceAdaptor

java.lang.Object
  extended byedu.vub.at.objects.mirrors.JavaInterfaceAdaptor

public class JavaInterfaceAdaptor
extends java.lang.Object

JavaInterfaceAdaptor is a class providing several static methods which allow accessing and invoking Java methods which represent native AmbientTalk methods. It is used by the Reflection class to up ambienttalk invocations and field accesses and translate them using java reflection.

Author:
tvcutsem, smostinc

Constructor Summary
JavaInterfaceAdaptor()
           
 
Method Summary
static java.lang.reflect.Method[] allMethodsPrefixed(java.lang.Class fromClass, java.lang.String prefix, boolean isStatic)
          Returns all public methods from the given class parameter whose name starts with the given prefix.
static java.lang.Object atObjectToPrimitiveJava(ATObject atObj, java.lang.Class type)
           
private static java.lang.Object[] coerceArguments(ATObject[] args, java.lang.Class[] types)
           
static ATObject createNativeATObject(java.lang.Class jClass, ATObject[] jInitArgs)
          Try to create a new instance of a Java class given an array of initialization arguments.
private static java.lang.reflect.Method[] getMethodsForSelector(java.lang.Class jClass, java.lang.String selector)
          Since Java uses strict matching when asked for a method, given an array of classes, this often means that the types are overspecified and therefore no matches can be found.
static java.lang.reflect.Method getNativeATMethod(java.lang.Class baseInterface, ATObject receiver, java.lang.String methodName)
           
static boolean hasApplicableJavaMethod(java.lang.Class jClass, java.lang.String jSelector)
          Tests given a class, whether the class either declares or inherits a method for a given selector.
static ATObject invokeNativeATMethod(java.lang.Class jClass, ATObject natReceiver, java.lang.String jSelector, ATObject[] jArguments)
          Invokes a method on a Java object identified by a selector.
static ATObject invokeNativeATMethod(java.lang.reflect.Method javaMethod, ATObject jReceiver, ATObject[] jArguments)
          Invokes a method on a native AmbientTalk object identified by a java.lang.reflect.Method object.
static boolean isPrimitiveType(java.lang.Class c)
           
static ATObject primitiveJavaToATObject(java.lang.Object jObj)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaInterfaceAdaptor

public JavaInterfaceAdaptor()
Method Detail

hasApplicableJavaMethod

public static boolean hasApplicableJavaMethod(java.lang.Class jClass,
                                              java.lang.String jSelector)
Tests given a class, whether the class either declares or inherits a method for a given selector.

Parameters:
jClass - - a Java class, representing an AT object.
jSelector - - a selector, describing the method to be searched for.
Returns:
whether a methods with a matching selector can be found

invokeNativeATMethod

public static ATObject invokeNativeATMethod(java.lang.Class jClass,
                                            ATObject natReceiver,
                                            java.lang.String jSelector,
                                            ATObject[] jArguments)
                                     throws InterpreterException
Invokes a method on a Java object identified by a selector.

Parameters:
jClass - the class of the receiver object
natReceiver - the receiver (a native AmbientTalk object)
jSelector - the java-level selector identifying the method to invoke
jArguments - parameters, normally AT objects
Returns:
the return value of the reflectively invoked method
Throws:
InterpreterException

invokeNativeATMethod

public static ATObject invokeNativeATMethod(java.lang.reflect.Method javaMethod,
                                            ATObject jReceiver,
                                            ATObject[] jArguments)
                                     throws InterpreterException
Invokes a method on a native AmbientTalk object identified by a java.lang.reflect.Method object. Note that if the method to invoke reflectively has a formal parameter list consisting of one argument of type ATObject[], then the arguments are wrapped in an array such that the function actually takes a variable number of arguments. A native AmbientTalk method is an ordinary Java method with the following constraints: - its name usually starts with base_ or meta_, identifying whether the method is accessible at the AmbientTalk base or meta level - its formal parameters MUST all be subtypes of ATObject (or be a single array of ATObject[] for varargs) - its return type must be a subtype of ATObject or a native Java type (native types are subject to default conversion to the appropriate AmbientTalk natives) - it may only throw InterpreterException exceptions

Parameters:
javaMethod - the Java method to invoke
jReceiver - the Java object representing the receiver (normally an AT object)
jArguments - the AT arguments to pass
Returns:
the return value of the reflectively invoked method TODO: code duplication w.r.t. invokeSymbioticMethod => replace this method by calls to invokeSymbioticMethod?
Throws:
InterpreterException

createNativeATObject

public static ATObject createNativeATObject(java.lang.Class jClass,
                                            ATObject[] jInitArgs)
                                     throws InterpreterException
Try to create a new instance of a Java class given an array of initialization arguments. Because we do not have exact typing information, all of the public constructors of the class are traversed until one is found that can create new instances given the current initargs.

Throws:
InterpreterException

getNativeATMethod

public static java.lang.reflect.Method getNativeATMethod(java.lang.Class baseInterface,
                                                         ATObject receiver,
                                                         java.lang.String methodName)
                                                  throws InterpreterException
Throws:
InterpreterException

allMethodsPrefixed

public static java.lang.reflect.Method[] allMethodsPrefixed(java.lang.Class fromClass,
                                                            java.lang.String prefix,
                                                            boolean isStatic)
Returns all public methods from the given class parameter whose name starts with the given prefix. Moreover, the boolean parameter isStatic determines whether to consider only static or only non-static methods.


getMethodsForSelector

private static java.lang.reflect.Method[] getMethodsForSelector(java.lang.Class jClass,
                                                                java.lang.String selector)
Since Java uses strict matching when asked for a method, given an array of classes, this often means that the types are overspecified and therefore no matches can be found. As a consequence we have our own mechanism to select which set of methods is applicable given a selector. Further dispatch needs only to be performed when more than a single match exists.

Parameters:
jClass - - the class from which the methods will be selected.
selector - - the name of the requested method.
Returns:
an array of applicable methods

coerceArguments

private static java.lang.Object[] coerceArguments(ATObject[] args,
                                                  java.lang.Class[] types)
                                           throws XTypeMismatch
Throws:
XTypeMismatch

isPrimitiveType

public static final boolean isPrimitiveType(java.lang.Class c)

primitiveJavaToATObject

public static final ATObject primitiveJavaToATObject(java.lang.Object jObj)
                                              throws XReflectionFailure
Throws:
XReflectionFailure

atObjectToPrimitiveJava

public static final java.lang.Object atObjectToPrimitiveJava(ATObject atObj,
                                                             java.lang.Class type)
                                                      throws InterpreterException
Throws:
InterpreterException