我试图在调用方法之前注入一些样板代码,我一直在使用AOP.
我成功地制作了一个自定义注释,现在管理我需要创建另一个并管理流程的流程.但是第二个引发NoSuchMethodFound错误.我需要注释一个Overrided方法.我也尝试注释一个简单的方法,但未能这样做.这是Aspect Class
@Aspect
public class AnnotationAspect {
private static final String TAG = "AnnotationAspect";
private static final String POINTCUT_METHOD =
"execution(@org.android10.gintonic.annotation.MyAnnotation * *(..))";
@Pointcut(POINTCUT_METHOD)
public void methodToAnnotate() {
}
@Around("methodToAnnotate()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
Log.d(TAG, "weaveJoinPoint: Inside the AnnotationAspect Class");
joinPoint.proceed();
return null;
}
}
这是注释类
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface MyAnnotation {
}
而这就是我的称呼方式.我也通过像onResume这样的重写方法调用它来尝试相同的注释.
@MyAnnotation
public void myMethod() {
Toast.makeText(this, "Hello myMethod", Toast.LENGTH_SHORT).show();
}
这是完整的GITHUB存储库Github的链接
错误块
java.lang.NoSuchMethodError: No static method aspectOf()Lorg/android10/gintonic/aspect/AnnotationAspect; in class Lorg/android10/gintonic/aspect/AnnotationAspect; or its super classes (declaration of 'org.android10.gintonic.aspect.AnnotationAspect' appears in /data/app/android10.org.viewgroupperformance-2/split_lib_slice_9_apk.apk)
at org.android10.viewgroupperformance.activity.LinearLayoutTestActivity.myMethod(LinearLayoutTestActivity.java:51)
at org.android10.viewgroupperformance.activity.LinearLayoutTestActivity.onCreate(LinearLayoutTestActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6582)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
解决方法:
很抱歉写这个作为答案,但这是共享代码的唯一方法.它只是为了帮助您,而不是在第一次迭代中回答问题.
因此,我在Android世界之外的纯Java AspectJ中重新创建了您的情况,以便查看问题是您的代码还是其他问题,例如目标设备上的编译,打包,部署,运行时类路径.
标记注释:
我使用了切入点中给出的包名.
package org.android10.gintonic.annotation;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target({ METHOD, CONSTRUCTOR })
public @interface MyAnnotation {}
司机申请:
package de.scrum_master.app;
import org.android10.gintonic.annotation.MyAnnotation;
public class Application {
@MyAnnotation
public void myMethod() {
System.out.println("Hi there!");
}
public static void main(String[] args) {
new Application().myMethod();
}
}
方面:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AnnotationAspect {
private static final String POINTCUT_METHOD =
"execution(@org.android10.gintonic.annotation.MyAnnotation * *(..))";
@Pointcut(POINTCUT_METHOD)
public void methodToAnnotate() {}
@Around("methodToAnnotate()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint);
joinPoint.proceed();
return null;
}
}
控制台日志:
execution(void de.scrum_master.app.Application.myMethod())
Hi there!
所以中间结果是你的注释和方面看起来都没问题.问题必须在其他地方.我在前面的评论中提到的第一个猜测是目标设备上的类路径上缺少aspectjrt.jar.
更新:也许您应该使用我刚才记得的my old answer here中提到的Gradle AspectJ插件.您遇到的问题和错误消息似乎是一样的.