gpt4 book ai didi

java - 反射:get Object state from return type of method in runtime

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:40 24 4
gpt4 key购买 nike

使用多种方法注释 Java 类:

@CustomAnnotation
public class MyService {

public List<MyObject> getMyObjects(){
...
}

public SomeObject getSomeObject(){
...
}

}

我应该记录 MyObjectSomeObject 实例的字段值。

aspectj 部分,其中 MyService 对象得到:

public void logg2(JoinPoint jp) {
Object target = jp.getTarget();

}

targetMyService 类,它包含方法。

如何获取从运行时 MyService 类中的方法返回的 MyObjectSomeObject 实例字段的值?

最佳答案

@Around(value = "traceReturnedObjectsFields()")
public Object traceAnotherOne(ProceedingJoinPoint jp) throws Throwable {
Object res = null;
res = jp.proceed();
if (res == null)
return res;
Class<?> c1 = res.getClass();
Field[] fields = c1.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())) {
System.out.println(field + " = " + field.get(res));
}
}
return res;
}

关于java - 反射:get Object state from return type of method in runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7778249/

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