Aleksandr
Aleksandr

Reputation: 4936

Using AspectJ for adding functionality for superclass method

I write the Android app, and I want to use custom fonts. So, I decided to use Calligraphy for that.

To use this library I need to override base method attachBaseContext in every Activity:

public class MainActivity extends AppCompatActivity {
...
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
...
}

Of course I can use OOP for that, but I want to do it using AspectJ.

To incude AspectJ in my gradle project I use gradle-android-aspectj-plugin.

Here is code of my aspect:

@Aspect
public class MyAspect {
    @Before("execution(void com.youtubeplaylist.app.ui.activity.*+.attachBaseContext(context))")
    public void beforeCalligraphy(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        Object[] args = thisJoinPoint.getArgs();
        Context c = (Context) args[0];
        c = CalligraphyContextWrapper.wrap(c);
        args[0] = c;
        thisJoinPoint.proceed(args);
        System.out.println("Aspect finished successfully");
    }
}

So, "Aspect finished successfully" never prints. Please, help me understand what's the problem.

Upvotes: 1

Views: 549

Answers (1)

XGouchet
XGouchet

Reputation: 10203

I'm quoting this answer on another question :

When using aspectJ with android you are limited to compile-time weaving, which basically means that you can only intercept code you own.

Compile time weaving will only change the way your own .class behave. If you override the attachBaseContext method yourself and leave it empty, and your aspect will work.

Upvotes: 2

Related Questions