Emulating client activity by calling a dumping method to register and simulate usage in java

I want to simulate user behavior in a Java application, and I want to write calls and method parameters to a log file, which I will read and make with the same calls.

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

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

2. Write the contents of the returned type with fields to the log file if it exists as follows: com.example.ReturnType [private fildname = contents]

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

Thanks.

+4
source share
1 answer

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

The following is an example of code execution 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 ab; } 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(); } } } 
+1
source

Source: https://habr.com/ru/post/1307792/


All Articles