Ihsan Haikal
Ihsan Haikal

Reputation: 1215

Get Method Input's Attributes using AspectJ Annotation

I'm trying to get some attributes of method's input but I'm only able to get the method's input only without any option to access the attributes or use any get method.

For example here is my code

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

where cloudFile is basically a class that contains a lot of stuff, including a file and it's possible to get the size of the file by using cloudFile.getSize(); (get method). Basically the cloudFile class looks like this:

public class CloudFile implements Comparable<CloudFile> {
...

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

From the above code I'm only able to print the name of file, like a.txt but without option of accessing the attributes contained in the file (in this case I would like to get the size of 5 KB). Is there a way to access the variables or methods from the arguments passed on by the AspectJ? Is there any option to do that?

Thanks

UPDATE

I have found something similar to answer my question in normal AspectJ something like this:

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(
         "------------------------------------------------------------");
   }
}

and now I am confused and trying to find the equal writing in AspectJ annotation.

Upvotes: 0

Views: 539

Answers (1)

Leonard Br&#252;nings
Leonard Br&#252;nings

Reputation: 13222

Try this:

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

Upvotes: 1

Related Questions