3

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.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • 3
    Hey gurukulki, everytime when you post a code snippet, the formatting is poor and almost none of them actually compiles. Please pay attention to that as well :) Intenting is to be done with 4 spaces and `Class` is to be written as `class`. – BalusC Jan 25 '10 at 12:56
  • Sorry BalusC. It was given just an example what i am looking for. didnt compile. and i will take care of it in future. Thanks – GuruKulki Jan 25 '10 at 12:58
  • BalusC: Why intenting is to be done with 4 spaces? I'm using tab to identing. Is there valid reason to type keyboard 4x times instead of 1x time ? – newbie Jan 24 '11 at 13:21

7 Answers7

5

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
  • 342,105
  • 78
  • 482
  • 720
4

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.
bmargulies
  • 97,814
  • 39
  • 186
  • 310
Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
4

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.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
2

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

Dmitry
  • 3,740
  • 15
  • 17
1

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
  • 8,007
  • 44
  • 58
1

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
  • 4,468
  • 12
  • 54
  • 85
1

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
  • 64,384
  • 92
  • 238
  • 332