All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dmgcodevil.jmspy.MethodInvocationRecorder Maven / Gradle / Ivy

package com.github.dmgcodevil.jmspy;

import com.github.dmgcodevil.jmspy.context.ContextExplorer;
import com.github.dmgcodevil.jmspy.context.InvocationContext;
import com.github.dmgcodevil.jmspy.graph.InvocationGraph;
import com.github.dmgcodevil.jmspy.proxy.ProxyFactory;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import static com.github.dmgcodevil.jmspy.proxy.CommonUtils.isNotPrimitiveOrWrapper;

/**
 * Class provides several methods to start invocation recording for some objects.
 * 

* Basically overall process comprises several steps, namely: * 1. Create proxy for the target object. * 2. Create invocation record *

* There is ability to register {@link ContextExplorer} to get more information about execution context. * Basically this information will be common for all invocation records * thus it doesn't make sense to register multiple explorers. *

* Also there is ability to save invocation snapshot. * * @author dmgcodevil */ public class MethodInvocationRecorder { private List invocationRecords = new ArrayList<>(); private ContextExplorer contextExplorer; private ProxyFactory proxyFactory; public MethodInvocationRecorder() { proxyFactory = ProxyFactory.getInstance(); } public MethodInvocationRecorder(ProxyFactory proxyFactory) { this.proxyFactory = proxyFactory; } public MethodInvocationRecorder(ContextExplorer contextExplorer) { this(); this.contextExplorer = contextExplorer; } public MethodInvocationRecorder(ContextExplorer contextExplorer, ProxyFactory proxyFactory) { this.contextExplorer = contextExplorer; this.proxyFactory = proxyFactory; } public List getInvocationRecords() { return invocationRecords; } public T record(T target) { return record(null, target); } public T record(Method method, T target) { if (isNotPrimitiveOrWrapper(target)) { InvocationRecord invocationRecord = new InvocationRecord(createInvocationContext(method), InvocationGraph.create(target)); T proxy = (T) proxyFactory.create(target, invocationRecord); invocationRecords.add(invocationRecord); return proxy; } else { return target; } } public Snapshot makeSnapshot() { return Snapshot.save(new Snapshot(invocationRecords)); } private InvocationContext createInvocationContext(Method method) { return InvocationContext.builder() .root(method) .contextExplorer(contextExplorer) .stackTrace(Thread.currentThread().getStackTrace()) .build(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy