How to register / trace any call to an object method in java

I need something like a Python decorator. I need to save some information, in particular, the method called, the start time, the end time and its arguments. I cannot import anything outside of Java 1.5 libraries. Is there something like what is available in Python?

+4
source share
6 answers

A good tracking tool for Java is BTrace .

+5
source

You can try profiling with Java Management Extensions or java agent.

See http://java.sun.com/developer/technicalArticles/releases/j2se15/#mnm

+2
source

No, Python decorators do not allow you to control all the code without changing anything. You should still put your decorator everywhere (and essentially wrap each of your functions in a function that does the counting).

I’m not sure how much I understand your requirements (you don’t say whether you need code for production or testing, also the requirement “do not import anything” is strange: are all your classes in the package by default? Did you allow to use IDE and compiler?) but I think the easiest way would be to use a Javassist (the second link kindly provided by cschooley is a great introduction), but do not use the agent: use CtClass # write () instead in the instrument class files and save them to disk (maybe in custom for Aceh ant). Thus, the final assembly does not need any special configuration.

0
source

You can get away with the Java Proxy class if you can only rely on interface methods.

0
source

@Loggable annotation and AspectJ aspect from jcabi-aspects can help you. Annotate all your methods and each time they are called SLF4J, they will receive log messages:

 @Loggable(Loggable.DEBUG) public String load(URL url) { return url.openConnection().getContent(); } 

The following versions of jcabi-aspects will implement full class logging: https://github.com/yegor256/jcabi/issues/129

0
source

Source: https://habr.com/ru/post/1369541/


All Articles