Siddhant Jain Siddhant Jain - 3 months ago 19
Java Question

Intercepting third party functions in android using AspectJ

I have been trying to intercept all the third party functions which are part of my application using aspectJ, but somehow am only able to intercept the functions declared by me and not the ones declared by third party libraries.

i am using this aspectJ gradle configuration referenced from this tutorial.

Here's what my aspect looks like :

private static final String POINTCUT_METHOD = "execution(* *(..))";

@Pointcut(POINTCUT_METHOD)
public void methodAnnotatedWithDebugTrace() {}

@Around("methodAnnotatedWithDebugTrace()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint)
throws Throwable {
// ...
}


what is the definition of methodAnnotatedWithDebugTrace ? – XGouchet 1 hour ago

@Pointcut(POINTCUT_METHOD) public void methodAnnotatedWithDebugTrace() { } is it the right definition

Is there any way by which we can start intercepting third party functions as well ??

Answer

To paraphrase another answer that has been given multiple times :

You can only weave your own code

Basically Aspects with android only works at compile time and will usually weave your own code. If you're using existing code for which you don't have source (like the Android framework for example), the compiler won't have access to modify those. In your case, you can only catch when your code is accessing the third party library.

Meaning that if you want to intercept third party libraries you need to use "call(* *(..))" instead of "execution(* *(..))"