How to create a unique hash code for an instance of a method?

I am profiling with Aspectj.

I need to uniquely identify instances of the method in which the field was available For example:

public class Class{ int a; int b; public void method1(){ setA(5); setB(6); 

In this case, with AspectJ, I can get that access to a and access to b were made using the setA and SetB methods. And with

 Thread.currentThread().getStackTrace(); 

I can know that setA and setB were called by method 1 ().

The method name is not enough. I also need to uniquely identify the instance of the method.

For example, if method1 is called many times, I should notice that access to a and access to b were made by different instances of method1.

Any suggestion on how to get the hash code of an instance of a method extraction method?

+4
source share
1 answer

A simple (untested, use at your own risk) solution that could work was to maintain a counter for each method in the stream:

 private static final ConcurrentHashMap<String, ConcurrentHashMap<Long, AtomicInteger>> COUNTERS = new ConcurrentHashMap<>(); public static int getInvocationId(String methodName, long threadId) { return counter(methodName, threadId).getAndIncrement(); } private static AtomicInteger counter(String methodName, long threadId) { ConcurrentHashMap<Long, AtomicInteger> map = countersForMethodName(methodName); AtomicInteger counter = map.get(threadId); if (counter == null) { AtomicInteger newCounter = new AtomicInteger(); counter = map.putIfAbsent(threadId, newCounter); if (counter == null) { return newCounter; } } return counter; } private static ConcurrentHashMap<Long, AtomicInteger> countersForMethodName( String methodName) { ConcurrentHashMap<Long, AtomicInteger> map = COUNTERS.get(methodName); if (map == null) { ConcurrentHashMap<Long, AtomicInteger> newMap = new ConcurrentHashMap<>(); map = COUNTERS.putIfAbsent(methodName, newMap); if (map == null) { return newMap; } } return map; } 

Then in your advice something like:

 int invocationId = getInvocationId(thisJoinPoint.getSignature().getName(), Thread.currentThread().getId()); // do what you want with invocationId 

Note that this depends on the recommendations being executed in the same thread as the target method. Unfortunately, I am not sufficiently aware that AspectJ knows whether this assumption will always remain true.

CAVEAT: If your environment creates and expires new threads all the time, then the above tree will continue to grow (essentially a memory leak). If this is a problem, then you will need to add another code to periodically list all active threads and trim expired entries from the tree. In this case, you can use the map identifier for each stream and then the name for each method to make cropping more efficient.

+1
source

All Articles