我必须检测字段值的变化。我想将以前的值与新值进行比较。我不知道字段名称或其类型。 (更多背景 here 。)对于给定类的示例:
package eu.zacheusz.aspectjtries;
@eu.zacheusz.aspectjtries.MyAnnotation
public class Sample {
private String field;
public void modify(){
this.field = "new";
}
public static void main(String[] a){
new Sample().modify();
}
}
我有这个方面:
package eu.zacheusz.aspectjtries.aspects;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class SampleAspect {
@After(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(value) && target(m) ")
public void afterSetField(Object m, Object value){
System.out.println("After set field. value=" + value + " target=" + m.getClass());
}
}
问题是 args
公开了在字段集连接点处传递的值,而不是字段的当前值。 In this presentation在第 27 页我发现:
sets(int p._x)[oldVal] [newVal]
但它似乎根本无法使用我的代码(注释)进行编译。 当我尝试时:
@After(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *)[oldVal] [newVal] && target(m) ")
public void afterSetField(Object m, Object oldVal, Object newVal){
然后我得到了:
Syntax error on token " set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *)[oldVal] [newVal] && target(m)", "unexpected pointcut element: '['@53:53" expected
这是使用反射的有效解决方案:
@Around(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t) ")
public void aroundSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable{
Signature signature = jp.getSignature();
String fieldName = signature.getName();
Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object oldVal = field.get(t);
System.out.println("Before set field. "
+ "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
//TODO compare oldVal with newVal and do sth.
jp.proceed();
}
这是比反射性能更好的解决方案(我认为)。但是仍然有很大的开销(附加字段,以及将方面实例绑定(bind)到每个目标)。
@Aspect("perthis(set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *))")
public class SampleAspect {
private final Map<String, Object> values = new HashMap<String, Object>();
@Around(" set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t) ")
public void beforeSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable {
String fieldName = jp.getSignature().getName();
Object oldVal = this.values.get(fieldName);
System.out.println("Before set field. "
+ "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
//TODO compare oldVal with newVal and do sth.
this.values.put(fieldName, newVal);
jp.proceed();
}
}
这里是使用 declare parents 的解决方案:
@Aspect
public class AspectC {
public interface FieldTracker {
Map<String, Object> getValues();
}
// this implementation can be outside of the aspect
public static class FieldTrackerImpl implements FieldTracker {
private transient Map<String, Object> values;
@Override
public Map<String, Object> getValues() {
if (values == null) {
values = new HashMap<String, Object>();
}
return values;
}
}
// the field type must be the introduced interface. It can't be a class.
@DeclareParents(value = "@eu.zacheusz.aspectjtries.MyAnnotation *", defaultImpl = FieldTrackerImpl.class)
private FieldTracker implementedInterface;
@Around("set(!static !final !transient * (@eu.zacheusz.aspectjtries.MyAnnotation *) . *) && args(newVal) && target(t)")
public void beforeSetField(final ProceedingJoinPoint jp, final FieldTracker t, final Object newVal) throws Throwable{
final Map<String, Object> values = t.getValues();
final String fieldName = jp.getSignature().getName();
final Object oldVal = values.get(fieldName);
System.out.println("Before set field " + fieldName
+ " oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
//TODO compare oldVal with newVal and do sth.
values.put(fieldName, newVal);
jp.proceed();
}
总结一下有三种选择:
- pertarget/perthis around set with field values map
- 带反射的单例集
- 带有声明父项和字段值映射的单例集
最好的解决方案是直接从切入点获取先前的值(无需反射或记住切入点之间的字段值)。可能吗?如果不是,哪种替代方案具有最佳性能?
附加说明
我找到了 this discussion关于设置切入点中的先前值,但它已经很旧了。
所有这些机制都是为了detecting JSF session-scoped bean internal state changes - 修复了 Google App Engine。这样的 bean 通常少于 100 个字段。全部从一个线程调用。
最佳答案
不幸的是,目前 AspectJ 中没有内置功能来查看字段的旧值。
你已经得到的两个解决方案是相当标准的,在这种情况下反射可能是最好的。
另一种选择是:
public aspect FieldTracker {
public interface TrackingField {};
public Map<String,Object> TrackingField.fields;
declare parents : @Deprecated * : implements TrackingField;
void around(TrackingField t, Object val) :
set(!static !final !transient * TrackingField.*)
&& args(val)
&& target(t)
{
String fieldName = thisJoinPointStaticPart.getSignature().getName();
Object oldVal = t.fields == null ? null : t.fields.get(fieldName);
// do whatever
if (val != null) {
if (t.fields == null) t.fields = new HashMap<String,Object>();
t.fields.put(fieldName, val);
}
proceed(t,val);
}
}
(我在这里写了这段代码,所以可能会有一些错误)
但这会创建一个映射来跟踪每个实例,并在每个实例中创建一个额外的字段来保存该映射,因此它会给您或多或少与 pertarget 方面相同的开销。
我目前正在使用与此类似的一个方面,但在那种情况下,我需要该映射进行 json 序列化(它比使用反射快得多),并且查看旧值的能力只是一个副作用。
关于java - 在 AspectJ set-pointcut 中公开先前的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698203/