Android 最傻瓜式的 AOP 框架
表明一个类是 Aspect Class,且 class 必须是 public,如
@Aspect
public class FragmentInjector {
}
@BeforeCall
方法调用前织入代码,如
@Aspect
public class FragmentInjector {
private static final String TAG = "FragmentInjector";
@BeforeCall(clazz = FragmentTransaction.class, method = "replace")
public static void beforeCallFragmentReplace(FragmentTransaction transaction, int containerViewId, Fragment fragment) {
Log.e(TAG, "beforeCallFragmentReplace: transaction = " + transaction + ", containerViewId = " + containerViewId + " ,fragment = " + fragment);
}
}
织入前的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().replace(R.id.fragment, new BlankFragment()).commit();
}
}
织入后的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
BlankFragment fragment = new BlankFragment();
int containerViewId = R.id.fragment;
FragmentInjector.beforeCallFragmentReplace(transaction, containerViewId, fragment);
transaction.replace(containerViewId, fragment).commit();
}
}
@AfterCall
方法调用后织入代码,同上 @BeforeCall 这里不在累述
@BeforeSuperExecute
父类方法内部执行前织入代码,如果是子类没有重写父类方法的话,将强制实现该方法,且该方法只有只会在直接子类中只会织入一次,子类的子类不在织入,防止多次调用。
@Aspect
public class FragmentInjector {
private static final String TAG = "FragmentInjector";
@BeforeSuperExecute(clazz = Fragment.class, method = "onCreate")
public static void beforeFragmentCreate(Fragment fragment, Bundle savedInstanceState) {
Log.e(TAG, "beforeFragmentCreate: fragment = " + fragment + ", savedInstanceState = " + savedInstanceState);
}
@AfterSuperExecute(clazz = Fragment.class, method = "onResume")
@AfterSuperExecute(clazz = DialogFragment.class, method = "onResume")
@AfterSuperExecute(clazz = ListFragment.class, method = "onResume")
public static void afterFragmentRe
sume(Fragment fragment) {
Log.e(TAG, "afterFragmentResume: fragment = " + fragment);
}
}
织入前的代码
public class BlankFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
织入后的代码
public class BlankFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
FragmentInjector.beforeFragmentCreate(this, savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
FragmentInjector.afterFragmentResume(this);
}
}
@BeforeSuperExecute 同样适用于 interface,如下,且能适配 Lambda 表达式
@Aspect
public class ClickListenerInjector {
private static final String TAG = "ClickListenerInjector";
@BeforeSuperExecute(clazz = View.OnClickListener.class, method = "onClick")
public static void beforeViewOnClick(View.OnClickListener listener, View view) {
Log.e(TAG, "beforeViewOnClick: listener = " + listener + ", view = " + view);
}
}
@AfterSuperExecute
父类方法内部执行后织入代码,同上 @BeforeSuperExecute 这里不在累述
评论