gpt4 book ai didi

java - 在 Java 代理中重定位 AspectJ 包

转载 作者:搜寻专家 更新时间:2023-10-30 20:53:39 24 4
gpt4 key购买 nike

我正在使用 AspectJ 来监控字段访问和字段修改。我有一个 gradle 项目,它编译这两个方面,并使用 gradle shadow 插件将该 jar 与 aspectjrt 和 aspectjweaver 一起打包在一个阴影 jar 中。代理仍然是 org.aspectj.weaver.loadtime.Agent。一切正常,但当我尝试重新定位 aspectj 包时出现错误。

影子插件配置为:

shadowJar {
relocate 'org.aspectj', 'shadow.org.aspectj'
relocate 'aj.org.objectweb.asm', 'shadow.aj.org.objectweb.asm'
}

list :

jar {
manifest {
attributes("Premain-Class": "shadow.org.aspectj.weaver.loadtime.Agent",
"Can-Redefine-Classes": true,
"Can-Retransform-Classes":true)
}
}

这是反编译的方面类,所以看起来是正确的:

package com.vfunction.singletonanalysis;

import shadow.org.aspectj.lang.JoinPoint;
import shadow.org.aspectj.lang.NoAspectBoundException;
import shadow.org.aspectj.lang.annotation.Aspect;
import shadow.org.aspectj.lang.annotation.Before;

@Aspect
public class StaticFieldBeforeAccessAspect extends AbstractFieldAccessAspect {
public StaticFieldBeforeAccessAspect() {
}

@Before("callAt()")
public void before(JoinPoint joinPoint) throws Throwable {
this.printJoinPoint(joinPoint);
}

public static StaticFieldBeforeAccessAspect aspectOf() {
if (ajc$perSingletonInstance == null) {
throw new NoAspectBoundException("com.vfunction.singletonanalysis.StaticFieldBeforeAccessAspect", ajc$initFailureCause);
} else {
return ajc$perSingletonInstance;
}
}

public static boolean hasAspect() {
return ajc$perSingletonInstance != null;
}

static {
try {
ajc$postClinit();
} catch (Throwable var1) {
ajc$initFailureCause = var1;
}

}
}

但是当我试图运行一个测试程序时,我仍然得到一个错误,说找到的类型不是一个方面:

[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.12 built on Friday Oct 20, 2017 at 21:58:11 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info using configuration file:***/workspace/singleton-analysis/agent/build/libs/agent-1.0.0-SNAPSHOT-all.jar!/META-INF/aop.xml
[AppClassLoader@18b4aac2] info register aspect com.vfunction.singletonanalysis.StaticFieldModifyAspect
[AppClassLoader@18b4aac2] error The specified aspect 'com.vfunction.singletonanalysis.StaticFieldModifyAspect' cannot be found
[AppClassLoader@18b4aac2] info register aspect com.vfunction.singletonanalysis.StaticFieldAccessAspect
[AppClassLoader@18b4aac2] error The specified aspect 'com.vfunction.singletonanalysis.StaticFieldAccessAspect' cannot be found
[AppClassLoader@18b4aac2] info register aspect com.vfunction.singletonanalysis.StaticFieldBeforeAccessAspect
[AppClassLoader@18b4aac2] error Cannot register 'com.vfunction.singletonanalysis.StaticFieldBeforeAccessAspect' because the type found with that name is not an aspect

最佳答案

检查阴影 jar 中 aop.xml 的内容。方面类名是否被遮蔽?我怀疑它没有给出错误消息。

As mentioned in documentation ,你可以让 Shadow 插件像这样转换 XML 文件:

shadowJar {
tranform(XmlAppendingTransformer.class) {
resource = 'aop.xml'
}
}

关于java - 在 Java 代理中重定位 AspectJ 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393662/

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