gpt4 book ai didi

java - 运行 AspectJ 导致 NoSuchMethodError : Aspect. aspectOf

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:14 64 4
gpt4 key购买 nike

我有一个非常简单的 AspectJ 切面(使用 @AspectJ),它只打印一条日志消息。我的目标是在我的 android 应用程序中建议代码。现在只要我的应用程序源代码中有方面类本身,这个方面就可以很好地工作。 一旦我将方面移动到不同的模块(java -> .jar 或 android lib -> .aar),在我的应用程序中运行建议的代码时,我会得到以下运行时异常:

java.lang.NoSuchMethodError: com.xxx.xxx.TraceAspect.aspectOf

基本上我的结构是这样的:

Root
+ app (com.android.application)
- MainActivity (with annotation to be adviced)
+ library (android-library)
- TraceAspect (aspect definition)

从 ajc 编译器,我可以看到 ajc 编译器选择了我的类并正确地建议它们,所以我真的不知道为什么只要我的源代码中有 @AspectJ 类它就可以工作,但停止工作一旦我将它移动到 jar 存档。

我正在使用 gradle。我的应用程序的构建脚本非常简单。我按照 http://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/ 中的说明进行操作

import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'org.aspectj:aspectjtools:1.8.1'
}
}

apply plugin: 'com.android.application'

repositories {
mavenCentral()
}

dependencies {
compile 'org.aspectj:aspectjrt:1.8.1'
compile project (':library')
}


android.applicationVariants.all { variant ->
AppPlugin plugin = project.plugins.getPlugin(AppPlugin)
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.5",
"-XnoInline",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", plugin.project.android.bootClasspath.join(File.pathSeparator)]

MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler)

def log = project.logger
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}

不确定是否重要,但以防万一,我方面的代码:

@Aspect
public class TraceAspect {
private static final String POINTCUT_METHOD = "execution(@com.xxx.TraceAspect * *(..))";

@Pointcut(POINTCUT_METHOD)
public void annotatedMethod() {}

@Around("annotatedMethod()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Aspect works...");
return joinPoint.proceed();
}
}

类路径

我还检查了 javaCompile.classPath,它正确地包含了 library-classes.jar 和我的 app-classes.jar。将 -log file 添加到 ajc 任务也显示文件已正确编织。

有什么想法吗?

重现此问题的最小示例

https://github.com/fschoellhammer/test-aspectj

最佳答案

该消息暗示方面文件尚未通过 aspectj 编织器。编织者将负责添加 aspectOf() 方法。尽管您的注释样式方面可以用 javac 编译得很好,但它们必须在某些时候由 aspectj“完成”以引入支持编织的基础结构方法。如果您是加载时织入,这是在加载方面时完成的,但如果您是编译时或编译后时织入,则需要通过其他方式将它们传送到 ajc。如果你有一个像这样构建的库:

javac MyAspect.java
jar -cvMf code.jar MyAspect.class

然后你需要编织那个 jar 来“完成”这些方面:

ajc -inpath code.jar -outjar myfinishedcode.jar

或者您可以在初始步骤中使用 ajc 而不是 javac

ajc MyAspect.java

或者您可以在将方面应用于您的其他代码时执行此操作:

ajc <myAppSourceFiles> -inpath myaspects.jar 

通过将 myaspects.jar 包含在 inpath 中,其中的任何方面类都将作为此编译步骤的一部分“完成”,并且完成的版本与已编译的应用程序源文件放在一起。请注意,这与您使用方面路径不同:

ajc <myAppSourceFiles> -aspectpath myaspects.jar

此处方面路径上的方面应用于您的代码,但它们仅从那里加载,它们完成,因此您不会获得已编译的应用程序源代码旁边的完成版本文件。

关于java - 运行 AspectJ 导致 NoSuchMethodError : Aspect. aspectOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802394/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com