buildscript
section of the buildscript
script of your project, add the following line to the dependencies
: classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
build.gradle
the application module: apply plugin: "android-aspectj"
dependencies { compile 'org.aspectj:aspectjrt:1.8.+' }
@Aspect public class MainActivityAspect { private static final String TAG = "helloaspectj"; @Pointcut("execution(* ru.hoticecream.helloaspectj.ui.MainActivity.onCreate(..))") public void onCreateMethod(){} @Before("onCreateMethod()") public void doBeforeOnCreate(JoinPoint joinPoint) { Log.i(TAG, "AspectJ was here"); } }
MainActivity.onCreate
method, displaying the corresponding message in the log.... I/helloaspectj﹕ AspectJ was here
private TextView mGreetingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGreetingText = (TextView) findViewById(R.id.greeting_message); setGreetingMessage("Hello Android"); } private void setGreetingMessage(String message) { mGreetingText.setText(message); }
@Pointcut("execution(* ru.hoticecream.helloaspectj.ui.MainActivity.setGreetingMessage(..))") public void setGreetingMessageMethod(){} @Around("setGreetingMessageMethod()") public void makeNasty(final ProceedingJoinPoint pjp) throws Throwable { Log.d(TAG, pjp.getSignature().toLongString()); pjp.proceed(new Object[] { "Hello AspectJ" }); }
setGreetingMessage
method and replaces its call with the contents of the makeNasty
method, using the @Around @Around
for this @Around
MainActivity.java
we did not change the MainActivity.java
.Source: https://habr.com/ru/post/231287/
All Articles