使用 AspectJ 注释获取方法输入的属性



我试图获得方法输入的一些属性,但我只能获得方法的输入,而没有任何访问属性或使用任何get方法的选项。

例如,这里是我的代码

@Around("execution (* *(cloudFile))")
public Object captureFileAttribute(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        System.err.println(joinPoint.getArgs()[0]);
        return result;
    }

其中cloudFile基本上是一个包含大量内容的类,包括一个文件,并且可以使用cloudFile.getSize()来获取文件的大小;(get方法)。cloudFile类基本上如下所示:

public class CloudFile implements Comparable<CloudFile> {
...
public CloudFile(CloudFolder folder, File file, FileSyncStatus syncStatus, TrustLevel trustLevel, String checksum) {
...
}
...
public long getSize() {
        return size.get();
    }
...
}

从上面的代码中,我只能打印文件名,比如.txt,但没有访问文件中包含的属性的选项(在这种情况下,我希望获得5 KB的大小)。有没有一种方法可以从AspectJ传递的参数中访问变量或方法?有什么选择吗?

感谢

更新

我在正常的AspectJ中找到了类似的东西来回答我的问题,比如:

public privileged aspect MemberAccessRecipe 
{
   /*
   Specifies calling advice whenever a method
   matching the following rules gets executed:
   Class Name: MyClass
   Method Name: foo
   Method Return Type: void
   Method Parameters: an int followed by a String
   */
   pointcut executionOfFooPointCut( ) : execution(
      void MyClass.foo(int, String));
   // Advice declaration
   after(MyClass myClass) : executionOfFooPointCut( ) && this(myClass)
   {
      System.out.println(
         "------------------- Aspect Advice Logic --------------------");
      System.out.println(
         "Accessing the set(float) member of the MyClass object");
      System.out.println(
         "Privileged access not required for this method call as it is 
         public");
      myClass.setF(2.0f);
      System.out.println(
         "Using the privileged aspect access to the private f member 
         variable");
      System.out.print("The current value of f is: ");
      System.out.println(myClass.f);
      System.out.println(
         "Signature: " + thisJoinPoint.getSignature( ));
      System.out.println(
         "Source Line: " + thisJoinPoint.getSourceLocation( ));
      System.out.println(
         "------------------------------------------------------------");
   }
}

现在我很困惑,试图在AspectJ注释中找到相同的写作。

试试这个:

@Around("execution (* *(fully.qualified.name.CloudFile)) && args(cloudFile)")
public Object captureFileAttribute(ProceedingJoinPoint joinPoint, CloudFile cloudFile) throws Throwable {
        Object result = joinPoint.proceed();
        System.err.println(cloudFile);
        return result;
    }

最新更新