gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:57:27 25 4
gpt4 key购买 nike

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

例如,这是我的代码

@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 注释中找到相同的书写方式。

最佳答案

试试这个:

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

关于java - 使用 AspectJ 注释获取方法输入的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37075638/

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