首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用字节伙伴代理通过aspectJ检测编织类

使用字节伙伴代理通过aspectJ检测编织类
EN

Stack Overflow用户
提问于 2019-09-06 03:24:37
回答 1查看 218关注 0票数 1

我想验证代码是由aspectJ编织的。我听说过的一种方法是使用字节伙伴库中的代理。不幸的是,我完全是新手,我不知道我能做什么。

我试着将JADE与代理结合使用,但byte-buddy对我来说更友好,我认为它更合适。

为了验证该方法,我创建了一个使用MySQL连接的简单springboot应用程序,并添加了一些aspectJ代码。我尝试过Spring AOP,但是AOP不能读取私有方法,所以我决定用aspectJ来表达我的想法。

我已经使用了另一个方面来定义joinPoints并抛出一个异常,但这很难,并且需要大量的工作才能得到低的结果。

关于使用字节伙伴检测aspectJ的代理的方法或实现,您有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2019-09-07 11:13:35

首先回答你的评论:

我想对不可信的代码有更多的经验。例如,如果我在Java中得到了一个大项目,然后我得到了一些由AspectJ创建的不受信任的插件,那么我更想知道如何检查我的类是否受到AspectJ的保护,或者AspectJ是否刚刚编织到我的类中。

对于AspectJ,有两种情况:

  • 编译时编织

代码语言:javascript
复制
- Your application does not use AspectJ, no AspectJ Maven plugin in your Maven build or something similar in your Gradle build: Even if your third-party library is on the classpath, nothing gets woven into your own code. How could it? There is no AspectJ compiler.
- Your application uses the AspectJ compiler: Even if the third-party library is on the classpath, no aspects are woven into your own code because you would have to explicitly put the library on the aspect path in order to achieve that. BTW, if the third-party library is aspect-enhanced, it will have a dependency on the AspectJ runtime _aspectjrt.jar_, otherwise it would not work. Even if the runtime was shaded into that other JAR, still nothing would happen to your own code.

  • 加载时编织

代码语言:javascript
复制
- Your application does not use LTW: Obviously nothing would happen, no aspects from the other JAR could be woven into your code without you starting the JVM with an active weaving agent.
- Your application uses LTW: You would need to provide an _aop.xml_ file and there you can explicitly list aspects you want to be applied, which target classes or packages to include into or exclude from weaving, turn on `-showWeaveInfo` in order too see on the console which aspects are woven into which joinpoints. There is one caveat, though: If the AspectJ weaver finds more than one _aop.xml_ or _aop-ajc.xml_ files on the classpath, it will conceptually merge them. I.e. if the third-party library comes with such a file, it would be merged into your own as explained in the [AspectJ development guide](https://www.eclipse.org/aspectj/doc/released/devguide/ltw-configuration.html) (search for "several configuration files" on the page). So this could lead to some undesired side effects. The simplest thing to do if you want to inspect your third-party libraries is to scan the JARs for the presence of _aop.xml_ or _aop-ajc.xml_, respectively. Then you can look at the content of those files, if any, and easily assess their possible effect on your code. But just checking the output of `-showWeaveInfo` on application start-up also reveals if unwanted aspects are woven into your own code.

因此,实际上你不需要扫描类文件中的任何东西来检测AspectJ标记,但是如果你绝对希望这样做,你可以这样做:

大多数AspectJ-woven类都包含字段或方法(可以是静态的或非静态的、私有的或公共的),在其名称中的某个位置(通常在开头)使用ajc$。这只是一种启发式的方法,但是嘿-不管怎样都行,对吧?

  • 如果我说“大多数类”,我的意思是在某些情况下,如果一个方面只使用ITD来使一个类实现一个接口,引入新的方法或公共字段,那么该类将只获得新的元素,而不是特定于AspectJ的代码。但它们只是普通的类,没有横切的建议代码或任何可能影响您自己的application.
  • If的东西。如果您找到这样的成员或字段,它将表明您要么找到由方面增强的类(即使在带有额外aop.xml的LTW场景中,这也不会影响您自己的类),要么发现方面本身,因为它们也包含相同的标记以及一些您可以扫描的其他东西。

那么,如何扫描这些第三方类文件呢?

扫描已经加载到(测试或检查) application:中的类的javap -p path/to/MyClass.class | grep 'ajc[$]'

  • Java代码:
  • 命令行

代码语言:javascript
复制
package de.scrum_master.aspect;

import java.lang.reflect.Member;
import java.util.stream.Stream;

public class AspectJDetector {
  public static boolean hasAspectJMarker(Class<?> clazz) {
    return Stream.concat(
      Stream.of((Member[]) clazz.getDeclaredMethods()),
      Stream.of((Member[]) clazz.getDeclaredFields())
    )
      .filter(member -> member.getName().contains("ajc$"))
      .findFirst()
      .isPresent();
  }
}

只需为要检查的每个类调用hasAspectJMarker(MyClassOfInterest.class)即可。如果您发现了AspectJ增强的类,也没有必要担心。只有当你找到一些方面的时候,才有可能。但正如我上面所说的:通过JAR中的aop.xml更容易检测到这一点。XML文件甚至会通过<aspect name="com.MyAspect"/>显式地列出这些方面,这基本上是不费吹灰之力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57811860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档