自定義注解
@Retention()
@Target(allowedTargets = AnnotationTarget.FUNCTION)
public @interface CheckLogin{
}
CheckLoginAspect
@Pointcut("execution(@xxx.xxxx.CheckLogin * *(..))")
private void methodAnnotated(){
}
@Around("methodAnnotated()")
public void checkLogin(ProceedingJoinPoint joinPoint) throws Throwable {//一定要是public
Signature signature = joinPoint.getSignature();//方法簽名
Method method = ( (MethodSignature)signature).getMethod();
//這個(gè)方法才是目標(biāo)對象上有注解的方法
Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(method.getName(), method.getParameterTypes());
CheckLogin checkLogin = realMethod.getAnnotation(CheckLogin.class);
//下面這句代碼是獲得那個(gè)自定義的注解的對象
CheckLogin authorizationNeed = (method.getAnnotation(CheckLogin.class));
Log.e("checkLogin",(checkLogin == null) + "");
Log.e("authorizationNeed",(authorizationNeed == null) + "");
//這里兩種getAnnotation的方式都為null,請大神指教,百思不得其解
}
MainActivity
@CheckLogin()
private void test(){
Toast.makeText(this,"跳轉(zhuǎn)成功",Toast.LENGTH_SHORT).show();
}
胖子玩AOP的時(shí)候,報(bào):Cause: zip file is empty
baidu、google一晚,還是google靠譜,這里記錄下。
public void checkLogin(ProceedingJoinPoint joinPoint),這里的一定要是public。如果是private就會(huì)報(bào)Cause: zip file is empty