Class Invoker

java.lang.Object
  extended byInvoker

public class Invoker
extends Object

Invoker lets you invoke protected and private methods in any object. Examples:

public class TestInvoker {

  public TestInvoker() {
    Invoker.addPath("C:/AADev/examples/");
    VeryPrivate vp = new VeryPrivate();
    try {
      Invoker.invoke(VeryPrivate.class, vp, "test1", new Object[]{});
      Invoker.invoke(VeryPrivate.class, vp, "test2", new Object[]{"Hello!"});
      Invoker.invoke(VeryPrivate.class, vp, "test3", new Class[]{Integer.TYPE}, new Object[]{new Integer(555)});
      Invoker.invoke(VeryPrivate.class, vp, "test4", new Class[]{String.class, Integer.TYPE}, new Object[]{"Hello again!", new Integer(555)});
    }
    catch (Exception e) {
      System.out.println("Problem: " + e);
    }    
  }

  public static void main(String[] args) {
    new TestInvoker();
  }
}


class VeryPrivate {

  private void test1() {
    System.out.println("Test1: no parameters");
  }

  private void test2(String s) {
    System.out.println("Test2: a String: " + s);
  }  

  private void test3(int i) {
    System.out.println("Test3: an integer: " + i);
  }

  private void test4(String s, int i) {
    System.out.println("Test4: a String: " + s + ", and an Integer: " + i);
  }
}

Author:
Michel Deriaz

Method Summary
static void addPath(String path)
          Invokes the protected addURL method from the URLClassLoader class, in order to bypass the fact that it is not possible to modify the classpath during runtime.
static void invoke(Class className, Object obj, String method, Class[] paramClass, Object[] param)
          Invokes a method containing primitive types.
static void invoke(Class className, Object obj, String method, Object[] param)
          Invokes a method containing no primitive types.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

invoke

public static void invoke(Class className,
                          Object obj,
                          String method,
                          Object[] param)
                   throws Exception
Invokes a method containing no primitive types. If the method has primitive types, see the invoke(Class, Object, String, Class[], Object[]) method.

Parameters:
className - the class of the object, for example URLClassLoader.class
obj - the object containing the method to invoke
method - the name of the method to invoke, for example "addURL"
param - the parameters, for example new Object[]{url}
Throws:
Exception - the following exceptions can be thrown from this method: NoSuchMethodException, NullPointerException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ExceptionInInitializerError.
See Also:
invoke(Class, Object, String, Class[], Object[])

invoke

public static void invoke(Class className,
                          Object obj,
                          String method,
                          Class[] paramClass,
                          Object[] param)
                   throws Exception
Invokes a method containing primitive types. If the method has no primitive types, see the invoke(Class, Object, String, Object[]) method.

Parameters:
className - the class of the object, for example MyProgram.class
obj - the object containing the method to invoke
method - the name of the method to invoke, for example "testMethod"
paramClass - the classes of the parameters, for exemple new Class[]{Integer.TYPE}
param - the parameters, for example new Object[]{new Integer(555)}
Throws:
Exception - the following exceptions can be thrown from this method: NoSuchMethodException, NullPointerException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ExceptionInInitializerError.
See Also:
invoke(Class, Object, String, Object[])

addPath

public static void addPath(String path)
Invokes the protected addURL method from the URLClassLoader class, in order to bypass the fact that it is not possible to modify the classpath during runtime.

Parameters:
path - the path to add