使用多种方法注释Java类:
@CustomAnnotation
public class MyService {
public List<MyObject> getMyObjects(){
...
}
public SomeObject getSomeObject(){
...
}
}
我应该记录MyObject
和SomeObject
个实例的字段值。
aspectj
部分,其中MyService
对象得到了:
public void logg2(JoinPoint jp) {
Object target = jp.getTarget();
}
target
是MyService
类,它包含方法。
如何获取MyObject
和SomeObject
的实例字段的值,这些值是从运行时MyService
类中的方法返回的?
答案 0 :(得分:1)
@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;
}
答案 1 :(得分:0)
Object target = jp.getTarget();
for (Class<?> superclass = target.getClass();
superclass != Object.class;
superclass = superclass.getSuperclass())
{
Field[] fields = superclass.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (Field field : fields)
{
if (!Modifier.isStatic(field.getModifiers()))
{
System.out.println(field + " = " + field.get(target));
}
}
}