AspectJ - 将接口实例变量传递给切入点
AspectJ - pass interface instance variable to pointcut
我需要将@interface 实例变量值传递给切入点和方法,但无法在 google 上找到任何内容。
这是我目前的情况:
切入点:
pointcut auditField(Object t, Object value): set(@ge.shemo.anotations.CaptureChanges * * ) && args(value) && target(t);
before (Object target, Object newValue, FieldChangeName fieldName):
auditField(target, newValue,fieldName) {
FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
Field field = sig.getField();
field.setAccessible(true);
Object oldValue;
try {
oldValue = field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create audit Action", e);
}
System.out.println("changed from " + oldValue + " to " + newValue);
}
和接口:
@Retention(RUNTIME)
@Target(value = FIELD)
public @interface CaptureChanges {
MethodType fieldType();
}
已更新
public enum MethodType {
NAME(FieldChangeType.STRING),
ID(FieldChangeType.STRING),
FIRST_NAME(FieldChangeType.STRING),
LAST_NAME(FieldChangeType.STRING);
private FieldChangeType type;
private FieldChangeName(FieldChangeType type) {
this.type = type;
}
public FieldChangeType getType() {
return this.type;
}
}
public enum FieldChangeType {
ENUM, STRING
}
我想从@interface CaptureChanges 中获取 'FieldChangeMethod method' 的值并在 before() 函数中使用它。
我该怎么做?
虽然我不清楚你想用 MethodType
和 FieldChangeType
类 实现什么,但这里有一种方法可以访问 @CaptureChanges
的值字段值即将更改时的注释:
pointcut auditField(Object t, Object value, CaptureChanges captureChanges):
set(* *) && @annotation(captureChanges) && args(value) && target(t);
before (Object target, Object newValue, CaptureChanges captureChanges):
auditField(target, newValue, captureChanges) {
FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
Field field = sig.getField();
field.setAccessible(true);
Object oldValue;
try {
oldValue = field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create audit Action", e);
}
System.out.println("changed from " + oldValue + " to " + newValue
+ ", fieldType=" + captureChanges.fieldType()
+ ", fieldChangeType=" + captureChanges.fieldType().getType());
}
我需要将@interface 实例变量值传递给切入点和方法,但无法在 google 上找到任何内容。
这是我目前的情况:
切入点:
pointcut auditField(Object t, Object value): set(@ge.shemo.anotations.CaptureChanges * * ) && args(value) && target(t);
before (Object target, Object newValue, FieldChangeName fieldName):
auditField(target, newValue,fieldName) {
FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
Field field = sig.getField();
field.setAccessible(true);
Object oldValue;
try {
oldValue = field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create audit Action", e);
}
System.out.println("changed from " + oldValue + " to " + newValue);
}
和接口:
@Retention(RUNTIME)
@Target(value = FIELD)
public @interface CaptureChanges {
MethodType fieldType();
}
已更新
public enum MethodType {
NAME(FieldChangeType.STRING),
ID(FieldChangeType.STRING),
FIRST_NAME(FieldChangeType.STRING),
LAST_NAME(FieldChangeType.STRING);
private FieldChangeType type;
private FieldChangeName(FieldChangeType type) {
this.type = type;
}
public FieldChangeType getType() {
return this.type;
}
}
public enum FieldChangeType {
ENUM, STRING
}
我想从@interface CaptureChanges 中获取 'FieldChangeMethod method' 的值并在 before() 函数中使用它。
我该怎么做?
虽然我不清楚你想用 MethodType
和 FieldChangeType
类 实现什么,但这里有一种方法可以访问 @CaptureChanges
的值字段值即将更改时的注释:
pointcut auditField(Object t, Object value, CaptureChanges captureChanges):
set(* *) && @annotation(captureChanges) && args(value) && target(t);
before (Object target, Object newValue, CaptureChanges captureChanges):
auditField(target, newValue, captureChanges) {
FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
Field field = sig.getField();
field.setAccessible(true);
Object oldValue;
try {
oldValue = field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create audit Action", e);
}
System.out.println("changed from " + oldValue + " to " + newValue
+ ", fieldType=" + captureChanges.fieldType()
+ ", fieldChangeType=" + captureChanges.fieldType().getType());
}