P
P
Pavel2015-11-21 09:17:18
Java
Pavel, 2015-11-21 09:17:18

Java. How to intercept an argument when calling a method?

There is a singleton object from third-party code, you need to intercept the argument when calling its method.
Or as an alternative, override the method in this object.
Is there such a possibility at all?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nirvimel, 2015-11-21
@nirvimel

Your case is rather difficult, but do not forget that reflection is capable of miracles .
PS In python, this (anti-pattern) is called monkey patching.

B
bromzh, 2015-11-21
@bromzh

If you use JavaEE, then you can intercept method calls from EJB beans through an annotation in which you point to the interceptor class. More details here , for example.

E
Evhen, 2015-11-23
@EugeneP2

If you call the singleton methods, then you can create a duplicate class that will repeat the singleton interface and redirect calls to it + log the passed parameters.
To override methods, one could use the CGLIB library, which can create a class proxy, but since this is a singleton, it is most likely the final class without a public constructor, Java will not allow such an override.
In general, no way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question