- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个非常简单的 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
任务也显示文件已正确编织。
有什么想法吗?
重现此问题的最小示例
最佳答案
该消息暗示方面文件尚未通过 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/
我的问题与 Spring 的 AspectJ 模式有关,特别是如何启用它: 交易管理 缓存 1)我注意到,为了启用 AspectJ 模式进行事务管理,我只需执行以下操作: @Configuration
当我尝试使用 Java 17 运行 AspectJ 检测时,我总是会遇到如下错误: java.lang.reflect.InvocationTargetException at jav
我有一个应该记录的跟踪方面: 进入 退出(返回类型为 void) 返回[返回对象] 抛出[异常消息] 我对第二个有问题。我如何在不重复记录所有退出的情况下为这种情况创建建议,就像现在我有一个 @Aft
我已经使用 @Aspect 注释声明了我的切面,但建议似乎没有得到应用。该方面适用于我拥有的其他一些项目,主要区别似乎是其他项目完全使用注释连接,并且这个特定项目是 xml 连接的。唯一连接注释的 b
我正在尝试使用加载时编织将 perf4j 绑定(bind)到程序中,但它似乎在我的类路径中找不到 aop.xml。要么是这样,要么它没有编织这个方面,因为它没有找到它。我已启用 aop.xml 的详细
我是 spring 框架的新手,正在尝试一些示例来理解 AOP,这是我到目前为止所做的,但它不起作用。 问题是我一添加 对于 spring.xml,我的构建失败说无法创建具有空指针异常的 bean。但
我尝试使用 AspectJ 围绕 Kotlin 函数编织方面,但没有成功。也许我只是配置不正确,或者 AspectJ 不支持这个。 有谁知道这是否可以使用例如 maven 和 Eclipse(或 In
我正在使用 Eclipse 4 和 AspectJ 的最新版本进行开发。我正在用修改后的库(二进制编织)替换 Java 6 库。问题是当前正在编织的代码是 Java 7 代码,而我需要它是 Java
我正在将我的项目从 java 7 迁移到 java 8,我遇到的问题与使用 aspectj-maven-plugin 的 aspectj 编织有关。 我可以根据 Haus documentation
嘿,我想将 AOP 添加到我的 Web 项目中。我下载了 eclipse 3.4.1 的 ajdt2.0.1。但是当我将此项目转换为 AspectJ 项目时,出现了很多不应该发生的编译错误。比如“XX
我最近在我的 Windows 7 机器上从 eclipse Juno 升级到 Luna,我的 aspectj 编译出现问题。我收到此错误: [ERROR] Failed to execute goal
我想创建一个注释,它使用环绕方面来使用该注释清理参数。 例如,一个方法可能如下所示: public void setName(@Scrubbed String name) { ... } 也许 pub
我无法理解aspectJ的编译时和加载时编织,也无法弄清楚使用什么(以及如何使用ajc)来编译和构建我的项目。 这是我的项目结构:- TestProject:一个java服务库。这正被其他一些人使
我想拦截给定类的任何子类的非注释方法的执行。 例如,假设我有类 Base: public class Base { public void baseMethod() { //shouldn't
我正在尝试使用 AspectJ 和运行时编织。我创建了一个方面 @Aspect(value = "TraceAspect") public class TraceAspect { @Arou
我只是像下面描述的那样实现了AspectJ:https://stackoverflow.com/a/10998044/2182503 此解决方案工作正常,直到我注意到@Autowired中的@Init
我正在使用 AspectJ 来建议所有具有所选类参数的公共(public)方法。我尝试了以下方法: pointcut permissionCheckMethods(Session sess) :
我正在尝试创建一个 AspectJ Aspect 来拦截具有通用接口(interface)的返回方法。 这是我的 AspectJ @AspectJ public class MyAspect {
使用 Aspect annotation 创建方面时如所述 here ,是否可以将此注释与包含状态的类一起使用(例如,一旦命中切入点,成员变量就会发生变化)?或者换句话说:方面类是单例吗?注释的源代码
当我尝试使用 Roo 创建的 JPA 对象时出现此错误。 Entity manager has not been injected (is the Spring Aspects JAR configu
我是一名优秀的程序员,十分优秀!