org.pfsw.reflect
Class Dynamic

java.lang.Object
  extended by org.pfsw.reflect.Dynamic

public abstract class Dynamic
extends java.lang.Object

This class supports more dynamic programming than usually possible in Java. With the class method perform in different variants it provides a more convenient dynamic method invocation service, than the pure reflection api. Especially Smalltalk programmers will find some similarities to their language.
To be able using this class, when a security manager is installed, you'll have to grant
permission java.lang.reflect.ReflectPermission "supressAccessChecks" ;
in the policy file. Otherwise any dynamic method invocation will cause a AccessControlException.

See Also:
ReflectPermission

Constructor Summary
Dynamic()
           
 
Method Summary
protected static java.lang.reflect.Method findMethod(java.lang.Class aClass, java.lang.String methodName, java.lang.Class[] paramTypes)
           
protected static java.lang.Class getTypeOf(java.lang.Object object)
           
static java.lang.Object invoke(java.lang.Object receiver, java.lang.reflect.Method method, java.lang.Object[] args)
          Executes the given method on the given object with the specified arguments.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName)
          Executes the named method on the given object and without any arguments.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName, boolean arg)
          Executes the named method on the given object with one boolean argument.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName, int arg)
          Executes the named method on the given object with one integer argument.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName, java.lang.Object arg)
          Executes the named method on the given object with one none primitive argument.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName, java.lang.Object[] args)
          Executes the named method on the given object with several arguments.
static java.lang.Object invoke(java.lang.Object receiver, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] argTypes)
          Executes the named method on the given object with several arguments.
static java.lang.Object perform(java.lang.Object receiver, java.lang.reflect.Method method, java.lang.Object[] args)
          Executes the given method on the given object with the specified arguments.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName)
          Executes the named method on the given object and without any arguments.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, boolean arg)
          Executes the named method on the given object with one boolean argument.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, int arg)
          Executes the named method on the given object with one integer argument.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, java.lang.Object arg)
          Executes the named method on the given object with one none primitive argument.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, java.lang.Object[] args)
          Executes the named method on the given object with several arguments.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] argTypes)
          Executes the named method on the given object with several arguments.
static java.lang.Object perform(java.lang.Object receiver, java.lang.String methodName, java.lang.Object[] args, java.lang.String[] signature)
          This method allows direct dynamic execution of JMX MBean method invocations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Dynamic

public Dynamic()
Method Detail

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName)
Executes the named method on the given object and without any arguments. If an exception occurs it will be returned as the result. Otherwise the result of the invoked method will be returned.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName,
                                      java.lang.Object arg)
Executes the named method on the given object with one none primitive argument.

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - An object, which becomes the only argument for the called method

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName,
                                      int arg)
Executes the named method on the given object with one integer argument.

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - An integer value, which becomes the only argument for the called method

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName,
                                      boolean arg)
Executes the named method on the given object with one boolean argument.

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - A boolean value, which becomes the only argument for the called method

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName,
                                      java.lang.Object[] args)
Executes the named method on the given object with several arguments.
Arguments that are instances of classes that correspond to primitive types will be automatically converted into the primitive types.
Integer -> int
Boolean -> boolean
Long -> long
Double -> double
Float -> float
Byte - byte
Character -> char
Short -> short

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
args - The arguments for the method invocation

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.String methodName,
                                      java.lang.Object[] args,
                                      java.lang.Class[] argTypes)
Executes the named method on the given object with several arguments.
Here the types of the arguments the method expects can be defined explicitly to detect the correct method signature.

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
args - The arguments for the method invocation
argTypes - The types of the arguments as defined in the method signature

invoke

public static java.lang.Object invoke(java.lang.Object receiver,
                                      java.lang.reflect.Method method,
                                      java.lang.Object[] args)
Executes the given method on the given object with the specified arguments.

If an exception occurs during this method invocation the result will be the exception rather than the expected value of the method call!

Parameters:
receiver - The object the method should be invoked on.
method - The method to invoke.
args - The arguments for the method invocation

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName)
                                throws java.lang.Exception
Executes the named method on the given object and without any arguments.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       java.lang.Object arg)
                                throws java.lang.Exception
Executes the named method on the given object with one none primitive argument.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - An object, which becomes the only argument for the called method
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       int arg)
                                throws java.lang.Exception
Executes the named method on the given object with one integer argument.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - An integer value, which becomes the only argument for the called method
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       boolean arg)
                                throws java.lang.Exception
Executes the named method on the given object with one boolean argument.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
arg - A boolean value, which becomes the only argument for the called method
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       java.lang.Object[] args)
                                throws java.lang.Exception
Executes the named method on the given object with several arguments.
Arguments that are instances of classes that correspond to primitive types will be automatically converted into the primitive types.
Integer -> int
Boolean -> boolean
Long -> long
Double -> double
Float -> float
Byte - byte
Character -> char
Short -> short

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
args - The arguments for the method call
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       java.lang.Object[] args,
                                       java.lang.String[] signature)
                                throws java.lang.Exception
This method allows direct dynamic execution of JMX MBean method invocations.

Parameters:
receiver - The object on which the method must be invoked
methodName - The name of the method to invoke
args - The arguments to be passed to the method
signature - The qualified class names of the argument types
Returns:
The result of the method execution.
Throws:
java.lang.Exception

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       java.lang.Object[] args,
                                       java.lang.Class[] argTypes)
                                throws java.lang.Exception
Executes the named method on the given object with several arguments.
Here the types of the arguments can be defined explicitly rather than being determined automatically.

Parameters:
receiver - The object the method should be performed on.
methodName - The name of the method to perform.
args - The arguments for the method call
argTypes - The types of the arguments in the args parameter
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

perform

public static java.lang.Object perform(java.lang.Object receiver,
                                       java.lang.reflect.Method method,
                                       java.lang.Object[] args)
                                throws java.lang.Exception
Executes the given method on the given object with the specified arguments.

Parameters:
receiver - The object the method should be performed on.
method - The method to perform.
args - The arguments for the method call
Throws:
java.lang.Exception - All kinds of exceptions are passed to the caller

getTypeOf

protected static java.lang.Class getTypeOf(java.lang.Object object)

findMethod

protected static java.lang.reflect.Method findMethod(java.lang.Class aClass,
                                                     java.lang.String methodName,
                                                     java.lang.Class[] paramTypes)