-2

I have a java program. I would like to intercept every function entries and exits and log them, without changing the existing (~5k) function bodies.

I know AspectJ and Spring AOP are suitable for that. But unfortunately, I am not allowed to use them on the target system (this is not my decistion).

One possibility would be to use following:

void func()
{
    logger.logEntry();

    // here stuff

    logger.logExit();
}

How can I achieve this without any third-party component/tool (this is very important!) without needed to insert these mentioned extra traces? Is there any Java interface/abstract class or any other method/possibility that can be used for this purpose? Or any event handler/subsriber etc.?

As I googled, I found only third-party solutions for this.

What the best soultion would be, if an abstract class with a 2 abstract functions that is called if functions of the from it extended classes' methods are called. Like this:

public abstract class InterceptFunctionCalls
{
    // here the internal stuff that call the following methods below

    protected abstract void EnteredFunction(String func);
    protected abstract void ExitedFunction(String func);
}
  • "without any third-party component/tool" => Why? [aspectj](https://www.eclipse.org/aspectj/) (for example) does a very good job for this requirement. – Seelenvirtuose May 25 '18 at 08:13
  • @A.V. I don't downvote for this kind of thing. I think that this area is not the place to ask broad questions. A place as Quora looks much better. – davidxxx May 25 '18 at 08:14
  • @Seelenvirtuose, because the target system (where the application runs) does not permit any of them. –  May 25 '18 at 08:16
  • It's a simple library. How can that not be allowed. This is ridiculous. Nevertheless, you could adapt their source code. They are also using plain Java. – Seelenvirtuose May 25 '18 at 08:17
  • @Seelenvirtuose AspectJ is much more than just a library, it's also a compiler and a linker, as it needs to "weave" aspects into the code it advises. Spring AOP just uses pure Java, true, but relies a lot on Spring dependency injection, so you'd need to be using it – kumesana May 25 '18 at 09:07
  • @kumesana Well ... But the compiler and the linker won't be _installed_ on the target system. – Seelenvirtuose May 25 '18 at 10:48

1 Answers1

1

Is there any Java interface/abstract class or any other method/possibility that can be used for this purpose?

No... That's why some third-parties decided to show up and provide for the need. Java as the unextended language isn't trying to offer AOP.

kumesana
  • 2,495
  • 1
  • 9
  • 10