jsint
Class Invoke

java.lang.Object
  |
  +--jsint.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 instanceCache
           
static java.util.Hashtable staticCache
           
 
Constructor Summary
Invoke()
           
 
Method Summary
static void collectDeclaredMethods(java.lang.Class c, java.util.Hashtable h)
           
static java.lang.Object[] constructorTable(java.lang.String c)
          Return the constructor table for the named class.
static java.lang.Object[] constructorTable(java.lang.String c, boolean canAccessPrivateData)
           
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.Object name, boolean isStatic)
           
static java.lang.reflect.Method[] getMethods(java.lang.Class c)
          Return all the methods for this class.
static java.lang.reflect.Method[] getMethods(java.lang.Class c, boolean canAccessPrivateData)
           
static java.lang.Object invokeConstructor(java.lang.String c, java.lang.Object[] args)
           
static java.lang.Object invokeInstance(java.lang.Object target, java.lang.Object name, java.lang.Object[] args)
           
static java.lang.Object invokeInstance(java.lang.Object target, java.lang.Object name, java.lang.Object[] args, boolean canAccessPrivateData)
           
static java.lang.Object invokeMethod(java.lang.Class c, java.lang.Object target, java.lang.Object name, java.lang.Object[] args, boolean isStatic)
           
static java.lang.Object invokeMethod(java.lang.Class c, java.lang.Object target, java.lang.Object name, java.lang.Object[] args, boolean isStatic, boolean canAccessPrivateData)
           
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.Object 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.Object name, boolean isStatic)
           
static java.lang.Object[] methodTable(java.lang.Class c, java.lang.Object name, boolean isStatic, boolean canAccessPrivateData)
           
static java.lang.Object[] methodTable0(java.lang.Class c, java.lang.Object name, boolean isStatic)
           
static java.lang.Object[] methodTable0(java.lang.Class c, java.lang.Object name, boolean isStatic, boolean canAccessPrivateData)
           
static java.lang.Object[] methodTableLookup(java.lang.Class c, java.lang.Object name, boolean isStatic)
           
static java.lang.Object[] methodTableLookup(java.lang.Class c, java.lang.Object name, boolean isStatic, boolean canAccessPrivateData)
           
static java.lang.Object[] methodTableLookupInstance(java.lang.Class c, java.lang.Object name)
           
static java.lang.Object[] methodTableLookupInstance(java.lang.Class c, java.lang.Object name, boolean canAccessPrivateData)
           
static java.lang.Object[] methodTableLookupStatic(java.lang.Class c, java.lang.Object name)
           
static java.lang.Object[] methodTableLookupStatic(java.lang.Class c, java.lang.Object name, boolean canAccessPrivateData)
           
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 canAccessPrivateData)
           
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 canAccessPrivateData)
           
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.Object 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.

constructorCache

public static final java.util.Hashtable constructorCache

staticCache

public static final java.util.Hashtable staticCache

instanceCache

public static final java.util.Hashtable instanceCache
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.Object name,
                                            java.lang.Object[] args)

invokeInstance

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

invokeInstance

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

invokeMethod

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

invokeMethod

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

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)
Return the constructor table for the named class.

constructorTable

public static java.lang.Object[] constructorTable(java.lang.String c,
                                                  boolean canAccessPrivateData)

getCachedMethodTable

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

putCachedMethodTable

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

methodTable0

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

methodTable0

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

methodTable

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

methodTable

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

methodTableLookup

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

methodTableLookup

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

methodTableLookupStatic

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

methodTableLookupStatic

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

methodTableLookupInstance

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

methodTableLookupInstance

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

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 canAccessPrivateData)

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 canAccessPrivateData)

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)
Return all the methods for this class. If you can't get all, for some reason,, just return the public ones.

Memoizable.


getMethods

public static java.lang.reflect.Method[] getMethods(java.lang.Class c,
                                                    boolean canAccessPrivateData)

collectDeclaredMethods

public static void collectDeclaredMethods(java.lang.Class c,
                                          java.util.Hashtable h)