views:

167

answers:

7

Is there any way to know how many times a instance of a class has invoked its member method.

I think(not sure), one way is to have a dedicated a member variable for a method, But that will not be feasible if we have so many methods.

For example:

class A{
        public void someMethod(){   
        }
}

and i have a instance say

A a = new A();

So I want to know the Number of times a has invoked someMethod in a program. We can have any number of methods.

+2  A: 

You may consider using profilers, e.g. one from NetBeans or YourKit, etc.

Dmitry
Can you elaborate. I didnt understand
GK
@gurukulki: http://java-source.net/open-source/profilers With a profiler you can analyze under each class/method/variable/memory usage and get statistics.
BalusC
+5  A: 

If you need this information inside the program, this is exactly what aspect oriented programming is meant for. Using AspectJ, it would be quite easy. Spring AOP will probably also work.

Michael Borgwardt
+4  A: 

There are a few approaches you could take, depending on how easily you can modify the code:

  • just add the counter variable as you suggest; clumsy if you have to add it in a lot of places, but easy to code
  • put your counter code in some other utility class, with a single method that you call from all relevant places to "increment counter for this method"; the utility method in question then examines the call stack-- e.g. via (new Exception()).getStackTrace()-- to see who was calling and increment the relevant counter
  • use a profiler that provides this facility
  • use the ASM library to add a counter.
  • use the Java instrumentation framework to modify the class definitions of relevant methods on the fly as the classes are loaded; this is potentially the most flexible-- you can even instrument code that you haven't actually written yourself and can't modify, and it means you don't have to alter the code of the actual classes you want to perform counting on-- but it is by far the most complex to code.
Neil Coffey
+1  A: 

There a lot of Open Source Java Profilers.

A profiler does dynamic program analysis (as opposed to static code analysis), and shows a program's behavior, gathering information as the program executes. Some of these profilers shows method invocation statistics.

JuanZe
+1  A: 

If you have access to JProbe it will tell you the number of times a method was invoked by a particular instance.

They say MAT is also good and its free, I haven't tried it yet.

Ravi Gupta
+3  A: 

Indeed, AOP would be the right tool here and I would write a little aspect to use JAMon (which can precisely gather statistics such as hits, time statistics (avg,total,min,max), concurrency statistics and more). See this previous answer for an example (or goolge a bit). If you are using Spring, then Spring has a ready to use JamonPerformanceMonitorInterceptor.

Pascal Thivent
+1  A: 

It's possible to do with java.lang.reflect.Proxy class. In Horstmann's book 'Core Java. Volume I' this technique is described in details.

Roman