views:

35

answers:

1

Hi

I want to simulate user behavior in an java application and I want to write method calls and parameters to a log file that i will read and make the same calls with.

I want to do this using reflection (java.lang.reflect.Proxy) to both write the file to a log and then read the log and make the calls. Is there a tool or way to

1.Write out to a log file a call to a method like this for example: com.example.Order.doStuff(String a, int b)

2.Write out to a log file the contents of the return type with fields if it exists like this: com.example.ReturnType [private fildname = contents]

3.Read this information and make the call above using reflection?

Thanks.

+1  A: 

Look up AOP (Aspect Oriented Programming) This will allow you to declare interceptors around methods and then you can simply use the interceptors to write to a log file.

Below is a sample executing code by reflection.

import java.lang.reflect.Method;

public class RunMthdRef {
  public int add(int a, int b) {
    return a+b;
  }

  public int sub(int a, int b) {
    return a-b;
  }

  public int mul(int a, int b) {
    return a*b;
  }

  public int div(int a, int b) {
    return a/b;
  }

  public static void main(String[] args) {
    try {
      Integer[] input={new Integer(2),new Integer(6)};
      Class cl=Class.forName("RunMthdRef");
      Class[] par=new Class[2];
      par[0]=Integer.TYPE;
      par[1]=Integer.TYPE;
      Method mthd=cl.getMethod("add",par);
      Integer output=(Integer)mthd.invoke(new RunMthdRef(),input);
      System.out.println(output.intValue());
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
}
Romain Hippeau