gpt4 book ai didi

java - 使用方法注释值来定位切入点

转载 作者:行者123 更新时间:2023-11-30 09:22:43 24 4
gpt4 key购买 nike

我正在使用 AspectJ 为任何用 @BindableClass 标记的类编织自定义 PropertyChangeSupport 引擎。它查找带有 @BindableMethod 标记的方法并拦截“set”调用以触发 propertyChangeListeners 链。一切正常,但我只想拦截其值为 @BindableMethod(type=Type.SET) 的方法,因为我将此注释用于 PCS 之外的功能。我在切入点的语法上遇到了一些困难,如果有人能帮助我,我将不胜感激。

我可以在寻找字段名称时通过检查注释的值来破解它,但我更愿意让 aspectJ 查找为我做这件事。我认为关键在于下面的“切入点”声明。

抱歉,如果我描述得不好,我已经好几年没玩过 AspectJ 了。我找到了一些关于注释查找的答案,但没有找到注释值的答案。

我的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindableMethod {
String fieldName();

Type type() default Type.SET;

public static enum Type {
GET, SET;
}
}

我的观点:

public aspect PropertySupportAspect {
/**
* Weave any class which is tagged with @BindableClass with NestedPropertyChangeSupport
*/
declare parents: @BindableClass * implements PropertySupport, IBindable;

NestedPropertyChangeSupport PropertySupport.support = new NestedPropertyChangeSupport(this);

public interface PropertySupport {
public void addPropertyChangeListener(PropertyChangeListener listener);

public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener);

public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener);

public void removePropertyChangeListener(PropertyChangeListener listener);

public boolean hasListeners(String propertyName);

public void firePropertyChange(Object b, String property, Object oldval, Object newval);
}

public PropertyChangeSupport PropertySupport.changeSupport() {
return support;
}

public void PropertySupport.addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}

public void PropertySupport.addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
support.addPropertyChangeListener(propertyName, listener);
}

public void PropertySupport.removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
support.removePropertyChangeListener(propertyName, listener);
}

public void PropertySupport.removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}

public boolean PropertySupport.hasListeners(String propertyName) {
return support.hasListeners(propertyName);
}

pointcut callSetter(PropertySupport b) :
call( @BindableMethod * *(..) )
&& target( b );

void around(PropertySupport b) : callSetter( b )
{
Field propertyField = getField(thisJoinPointStaticPart.getSignature());
try {
propertyField.setAccessible(true);
Object oldValue = propertyField.get(b);
proceed(b);
Object newValue = propertyField.get(b);
((PropertySupport) b).firePropertyChange(b, propertyField.getName(), oldValue, newValue);
} catch (Exception e) {
e.printStackTrace();
}
}

private Field getField(Signature signature) {
Field field = null;

try {
MethodSignature ms = (MethodSignature) signature;
Method m = ms.getMethod();
BindableMethod annotation = m.getAnnotation(BindableMethod.class);
field = signature.getDeclaringType().getDeclaredField(annotation.fieldName());
} catch (NoSuchFieldException nsfe) {
nsfe.printStackTrace();
}
return field;

}

public void PropertySupport.firePropertyChange(Object b, String property, Object oldval, Object newval) {
support.firePropertyChange(property, oldval, newval);

}

我的测试类:

@BindableClass
private class Child {
public static final String FLD_NAME = "name";
private String name;

@BindableMethod(fieldName = FLD_NAME)
public void setName(String name) {
this.name = name;
}

@BindableMethod(fieldName = FLD_NAME, type = Type.GET)
public String getName() {
return name;
}
}

最佳答案

pointcut callSetter(PropertySupport b, BindableMethod bm) : 
call(@BindableMethod * *(..))
&& target(b)
&& @annotation(bm);

void around(PropertySupport b,BindableMethod bm) : callSetter( b,bm ){
bm.type();
}

关于java - 使用方法注释值来定位切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16368552/

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