gpt4 book ai didi

java - AspectJ - 将接口(interface)实例变量传递给切入点

转载 作者:行者123 更新时间:2023-11-30 07:58:37 25 4
gpt4 key购买 nike

我需要将@interface 实例变量值传递给切入点和方法,但无法在谷歌上找到任何内容。

这是我目前所拥有的:

切入点:

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 方法”的值并在 before() 函数中使用它。

我该怎么做?

最佳答案

虽然我不清楚您要使用 MethodTypeFieldChangeType 类实现什么,但这里有一种方法可以访问 @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());
}

关于java - AspectJ - 将接口(interface)实例变量传递给切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311021/

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