use of net.bytebuddy.implementation.bind.annotation.RuntimeType in project redisson by redisson.
the class AccessorInterceptor method intercept.
@RuntimeType
public Object intercept(@Origin Method method, @SuperCall Callable<?> superMethod, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectLiveMap") RMap<String, Object> liveMap) throws Exception {
if (isGetter(method, getREntityIdFieldName(me))) {
return ((RLiveObject) me).getLiveObjectId();
}
if (isSetter(method, getREntityIdFieldName(me))) {
((RLiveObject) me).setLiveObjectId(args[0]);
return null;
}
String fieldName = getFieldName(method);
Class<?> fieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
if (isGetter(method, fieldName)) {
Object result = liveMap.get(fieldName);
if (result == null) {
RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
if (ar != null) {
objectBuilder.store(ar, fieldName, liveMap);
return ar;
}
}
if (result instanceof RedissonReference) {
return RedissonObjectFactory.fromReference(redisson, (RedissonReference) result);
}
return result;
}
if (isSetter(method, fieldName)) {
Object arg = args[0];
if (arg != null && arg.getClass().isAnnotationPresent(REntity.class)) {
throw new IllegalStateException("REntity object should be attached to Redisson first");
}
if (arg instanceof RLiveObject) {
RLiveObject liveObject = (RLiveObject) arg;
Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, (Class) rEntity));
liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, fieldType, getREntityIdFieldName(liveObject), liveObject.getLiveObjectId())));
return me;
}
if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(me.getClass().getSuperclass().getAnnotation(REntity.class).fieldTransformation())) {
RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
if (arg != null) {
if (rObject instanceof Collection) {
Collection c = (Collection) rObject;
c.clear();
c.addAll((Collection) arg);
} else {
Map m = (Map) rObject;
m.clear();
m.putAll((Map) arg);
}
}
if (rObject != null) {
arg = rObject;
}
}
if (arg instanceof RObject) {
objectBuilder.store((RObject) arg, fieldName, liveMap);
return me;
}
if (arg == null) {
liveMap.remove(fieldName);
} else {
liveMap.fastPut(fieldName, arg);
}
return me;
}
return superMethod.call();
}
Aggregations