Reputation: 12382
I want to trace all invocations of methods from a Foo
class. Another class that would override all the methods from Foo
like this...
@Override
public void blah() {
System.out.println("blah()");
super.blah();
}
...would do - but do I really need to override them all (over 100 methods)?
Can this be done in some smarter way, e.g., using reflection?
EDIT: To clarify - Foo
is a JNI interface to a native library, and I need this to save all the calls to a file, to generate C code that does exactly what I did from Java.
Upvotes: 0
Views: 1211
Reputation: 15189
If you only need this for debugging/dev purposes and not in the final system, you can use Eclipse profiling. Eclipse profiling allows you to define probes that are executed at various times (e. g. at the start of a method) which allow you to execute arbitrary Java code while having access to stuff like method names and parameter values. You can define various filters in order to only execute the probes where it is currently necessary. I don't know exactly how it works but I assume that the class files are instrumented so the runtime impact should be relatively low.
If on the other hand you need this in production code, I would vote for AOP -- just as nanda proposes.
Upvotes: 1
Reputation: 10762
For debugging, if you are using IBM JVM you can use method trace (may disable some JIT optimisations for the traced methods):
Print to output stream as text:
-Xtrace:iprint=mt,methods{com/my/package/ClassName.methodName}
or to file in binary format (process with built-in com.ibm.jvm.format.TraceFormat app):
-Xtrace:maximal=mt,methods{com/my/package/ClassName.methodName},output=trace.dat
Full details in the Java Diagnostics Guide: http://www.ibm.com/developerworks/java/jdk/diagnosis/
Upvotes: 2
Reputation: 24788
AOP (Aspect Oriented Programming)
For example, you can use AJDT, take a look at its demo
Upvotes: 5
Reputation: 597076
First, if you have 100 methods in a class it is a prime candidate for refactoring.
Then - you can make a proxy (using CGLIB or javassist) around the object, and whenever a method is invoked, report the invocation.
Upvotes: 1