jsint
Class Invoke

java.lang.Object
  extended byjsint.Invoke

public class Invoke
extends java.lang.Object

Provides dynamic Java method invocation through Java's Reflection interface. For a good discussion of a Scheme implementation, and the issues involved with dynamic method invocation in Java see:

Michael Travers, Java Q & A, Dr. Dobb's Journal, Jan., 2000, p. 103-112.

Primitive types are not widened because it would make method selection more ambiguous. By memoizing constructorTable() and methodTable() dynamic method lookup can be done without consing.

You'll notice that Java doesn't make this very easy. For example it would be nice if Method and Constructor shared an Invokable interface.

Privileged methods can be invoked if the JVM allows it.

The name of a method to be invoked can be any nonnull Object with a .toString() that names a method. It should probably be changed to String.


Field Summary
static int BUCKET_SIZE
          Each bucket in an method table contains a Class[] of parameterTypes and the corresponding method or constructor.
static java.util.Hashtable constructorCache
           
static java.util.Hashtable constructorCachePriv
           
static java.util.Hashtable instanceCache
          Instance method name -> Class -> parameter[]/method array.
static java.util.Hashtable staticCache
          Static method name -> Class -> parameter[]/method array.
 
Constructor Summary
Invoke()
           
 
Method Summary
static java.lang.Object[] constructorTable(java.lang.String c, boolean isPrivileged)
          Return the constructor table for the named class.
static java.lang.Object[] constructorTable0(java.lang.String c)
           
static java.lang.Object[] constructorTable0Priv(java.lang.String c)
           
static java.lang.reflect.Constructor findConstructor(java.lang.Object target, Pair types)
          Look up a particular constructor given its name, and the name of its declaring class, and a list of argument type names.
static java.lang.Object findMethod(java.lang.Object[] methods, java.lang.Object[] args)
          Find the most applicable method.
static java.lang.reflect.Method findMethod(java.lang.String name, java.lang.Object target, Pair types)
          Look up a particular method given its name, and the name of its declaring class, and a list of argument type names.
static java.lang.Object[] getCachedMethodTable(java.lang.Class c, java.lang.String name, boolean isStatic)
           
static java.util.Hashtable getClassTable(java.lang.String name, boolean isStatic)
          Returns a Class -> prameter[]/method array for the method named name.
static java.lang.reflect.Method[] getMethods(java.lang.Class c, boolean isPrivileged)
          Return all the methods for this class.
static java.lang.Object invokeConstructor(java.lang.String c, java.lang.Object[] args)
           
static java.lang.Object invokeInstance(java.lang.Object target, java.lang.String name, java.lang.Object[] args, boolean isPrivileged)
           
static java.lang.Object invokeMethod(java.lang.Class c, java.lang.Object target, java.lang.String name, java.lang.Object[] args, boolean isStatic, boolean isPrivileged)
           
static java.lang.Object invokeRawConstructor(java.lang.reflect.Constructor m, java.lang.Object[] args)
           
static java.lang.Object invokeRawMethod(java.lang.reflect.Method m, java.lang.Object target, java.lang.Object[] args)
           
static java.lang.Object invokeStatic(java.lang.Class c, java.lang.String name, java.lang.Object[] args)
           
static boolean isApplicable(java.lang.Class[] types, java.lang.Object[] args)
           
static java.lang.Object[] methodTable(java.lang.Class c, java.lang.String name, boolean isStatic, boolean isPrivileged)
           
static java.lang.Object[] methodTable0(java.lang.Class c, java.lang.String name, boolean isStatic, boolean isPrivileged)
           
static java.lang.Object[] methodTableLookup(java.lang.Class c, java.lang.String name, boolean isStatic, boolean isPrivileged)
           
static java.lang.Object[] methodTableLookupInstance(java.lang.Class c, java.lang.String name)
           
static java.lang.Object[] methodTableLookupInstance(java.lang.Class c, java.lang.String name, boolean isPrivileged)
           
static java.lang.Object[] methodTableLookupStatic(java.lang.Class c, java.lang.String name, boolean isPrivileged)
           
static java.util.Vector methodVector(java.lang.Class c, java.lang.String name)
           
static java.util.Vector methodVector(java.lang.Class c, java.lang.String name, boolean isPrivileged)
           
static java.util.Vector methodVectorMerge(java.lang.Class c, java.lang.String name, java.util.Vector result)
          Add new methods to your superclasses table.
static java.util.Vector methodVectorMerge(java.lang.Class c, java.lang.String name, java.util.Vector result, boolean isPrivileged)
           
static boolean parameterTypesMatch(java.lang.Class[] p1, java.lang.Class[] p2)
          Do the paramter types of an instance method match?
static java.lang.Object peek(java.lang.Object target, java.lang.String name)
           
static java.lang.Object peekStatic(java.lang.Class c, java.lang.String name)
           
static java.lang.Object poke(java.lang.Object target, java.lang.String name, java.lang.Object value)
           
static java.lang.Object pokeStatic(java.lang.Class c, java.lang.String name, java.lang.Object value)
           
static void putCachedMethodTable(java.lang.Class c, java.lang.String name, boolean isStatic, java.lang.Object value)
           
static java.lang.Class[] toClassArray(Pair types, int n)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUCKET_SIZE

public static final int BUCKET_SIZE
Each bucket in an method table contains a Class[] of parameterTypes and the corresponding method or constructor.

See Also:
Constant Field Values

constructorCache

public static final java.util.Hashtable constructorCache

constructorCachePriv

public static final java.util.Hashtable constructorCachePriv

staticCache

public static final java.util.Hashtable staticCache
Static method name -> Class -> parameter[]/method array.


instanceCache

public static final java.util.Hashtable instanceCache
Instance method name -> Class -> parameter[]/method array.

Constructor Detail

Invoke

public Invoke()
Method Detail

peek

public static java.lang.Object peek(java.lang.Object target,
                                    java.lang.String name)

peekStatic

public static java.lang.Object peekStatic(java.lang.Class c,
                                          java.lang.String name)

poke

public static java.lang.Object poke(java.lang.Object target,
                                    java.lang.String name,
                                    java.lang.Object value)

pokeStatic

public static java.lang.Object pokeStatic(java.lang.Class c,
                                          java.lang.String name,
                                          java.lang.Object value)

invokeConstructor

public static java.lang.Object invokeConstructor(java.lang.String c,
                                                 java.lang.Object[] args)

invokeRawConstructor

public static java.lang.Object invokeRawConstructor(java.lang.reflect.Constructor m,
                                                    java.lang.Object[] args)

invokeStatic

public static java.lang.Object invokeStatic(java.lang.Class c,
                                            java.lang.String name,
                                            java.lang.Object[] args)

invokeInstance

public static java.lang.Object invokeInstance(java.lang.Object target,
                                              java.lang.String name,
                                              java.lang.Object[] args,
                                              boolean isPrivileged)

invokeMethod

public static java.lang.Object invokeMethod(java.lang.Class c,
                                            java.lang.Object target,
                                            java.lang.String name,
                                            java.lang.Object[] args,
                                            boolean isStatic,
                                            boolean isPrivileged)

invokeRawMethod

public static java.lang.Object invokeRawMethod(java.lang.reflect.Method m,
                                               java.lang.Object target,
                                               java.lang.Object[] args)

constructorTable

public static java.lang.Object[] constructorTable(java.lang.String c,
                                                  boolean isPrivileged)
Return the constructor table for the named class.


constructorTable0Priv

public static java.lang.Object[] constructorTable0Priv(java.lang.String c)

constructorTable0

public static java.lang.Object[] constructorTable0(java.lang.String c)

getClassTable

public static java.util.Hashtable getClassTable(java.lang.String name,
                                                boolean isStatic)
Returns a Class -> prameter[]/method array for the method named name.


getCachedMethodTable

public static java.lang.Object[] getCachedMethodTable(java.lang.Class c,
                                                      java.lang.String name,
                                                      boolean isStatic)

putCachedMethodTable

public static void putCachedMethodTable(java.lang.Class c,
                                        java.lang.String name,
                                        boolean isStatic,
                                        java.lang.Object value)

methodTable0

public static java.lang.Object[] methodTable0(java.lang.Class c,
                                              java.lang.String name,
                                              boolean isStatic,
                                              boolean isPrivileged)

methodTable

public static java.lang.Object[] methodTable(java.lang.Class c,
                                             java.lang.String name,
                                             boolean isStatic,
                                             boolean isPrivileged)

methodTableLookup

public static java.lang.Object[] methodTableLookup(java.lang.Class c,
                                                   java.lang.String name,
                                                   boolean isStatic,
                                                   boolean isPrivileged)

methodTableLookupStatic

public static java.lang.Object[] methodTableLookupStatic(java.lang.Class c,
                                                         java.lang.String name,
                                                         boolean isPrivileged)

methodTableLookupInstance

public static java.lang.Object[] methodTableLookupInstance(java.lang.Class c,
                                                           java.lang.String name)

methodTableLookupInstance

public static java.lang.Object[] methodTableLookupInstance(java.lang.Class c,
                                                           java.lang.String name,
                                                           boolean isPrivileged)

methodVector

public static java.util.Vector methodVector(java.lang.Class c,
                                            java.lang.String name)

methodVector

public static java.util.Vector methodVector(java.lang.Class c,
                                            java.lang.String name,
                                            boolean isPrivileged)

methodVectorMerge

public static java.util.Vector methodVectorMerge(java.lang.Class c,
                                                 java.lang.String name,
                                                 java.util.Vector result)
Add new methods to your superclasses table.


methodVectorMerge

public static java.util.Vector methodVectorMerge(java.lang.Class c,
                                                 java.lang.String name,
                                                 java.util.Vector result,
                                                 boolean isPrivileged)

parameterTypesMatch

public static boolean parameterTypesMatch(java.lang.Class[] p1,
                                          java.lang.Class[] p2)
Do the paramter types of an instance method match?


findMethod

public static java.lang.Object findMethod(java.lang.Object[] methods,
                                          java.lang.Object[] args)
Find the most applicable method. For instance methods getMethods() has already handled the "this" argument, so instance and static methods are matched the same way.


isApplicable

public static boolean isApplicable(java.lang.Class[] types,
                                   java.lang.Object[] args)

findMethod

public static java.lang.reflect.Method findMethod(java.lang.String name,
                                                  java.lang.Object target,
                                                  Pair types)
Look up a particular method given its name, and the name of its declaring class, and a list of argument type names.

This is only used by (method).


findConstructor

public static java.lang.reflect.Constructor findConstructor(java.lang.Object target,
                                                            Pair types)
Look up a particular constructor given its name, and the name of its declaring class, and a list of argument type names.

This is only used by (constructor).


toClassArray

public static java.lang.Class[] toClassArray(Pair types,
                                             int n)

getMethods

public static java.lang.reflect.Method[] getMethods(java.lang.Class c,
                                                    boolean isPrivileged)
Return all the methods for this class. If you can't get all, for some reason,, just return the public ones.

Memoizable.