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

时间:2016-05-06 15:01:24

标签: java 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()来获取文件的大小; (得到方法)。基本上,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();
    }
...
}

从上面的代码我只能打印文件的名称,比如a.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注释中找到相同的写作。

1 个答案:

答案 0 :(得分:1)

试试这个:

@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;
    }