use of org.redisson.api.annotation.REntity in project redisson by redisson.
the class RedissonObjectBuilder method toReference.
public RedissonReference toReference(Object object) {
if (object != null && ClassUtils.isAnnotationPresent(object.getClass(), REntity.class)) {
throw new IllegalArgumentException("REntity should be attached to Redisson before save");
}
if (object instanceof RObject && !(object instanceof RLiveObject)) {
Class<?> clazz = object.getClass().getInterfaces()[0];
RObject rObject = (RObject) object;
if (rObject.getCodec() != null) {
codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
}
return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
}
if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
Class<?> clazz = object.getClass().getInterfaces()[0];
RObjectReactive rObject = (RObjectReactive) object;
if (rObject.getCodec() != null) {
codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
}
return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
}
if (object instanceof RObjectRx && !(object instanceof RLiveObject)) {
Class<?> clazz = object.getClass().getInterfaces()[0];
RObjectRx rObject = (RObjectRx) object;
if (rObject.getCodec() != null) {
codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
}
return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
}
try {
if (object instanceof RLiveObject) {
Class<? extends Object> rEntity = object.getClass().getSuperclass();
NamingScheme ns = getNamingScheme(rEntity);
return new RedissonReference(rEntity, ns.getName(rEntity, ((RLiveObject) object).getLiveObjectId()));
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return null;
}
use of org.redisson.api.annotation.REntity in project redisson by redisson.
the class AccessorInterceptor method intercept.
@RuntimeType
@SuppressWarnings("NestedIfDepth")
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(me.getClass().getSuperclass(), method);
Field field = ClassUtils.getDeclaredField(me.getClass().getSuperclass(), fieldName);
Class<?> fieldType = field.getType();
if (isGetter(method, fieldName)) {
Object result = liveMap.get(fieldName);
if (result == null) {
RObject ar = commandExecutor.getObjectBuilder().createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
if (ar != null) {
commandExecutor.getObjectBuilder().store(ar, fieldName, liveMap);
return ar;
}
}
if (result != null && fieldType.isEnum()) {
if (result instanceof String) {
return Enum.valueOf((Class) fieldType, (String) result);
}
return result;
}
if (result instanceof RedissonReference) {
return commandExecutor.getObjectBuilder().fromReference((RedissonReference) result, RedissonObjectBuilder.ReferenceType.DEFAULT);
}
return result;
}
if (isSetter(method, fieldName)) {
Object arg = args[0];
if (arg != null && ClassUtils.isAnnotationPresent(arg.getClass(), REntity.class)) {
throw new IllegalStateException("REntity object should be attached to Redisson first");
}
if (arg instanceof RLiveObject) {
RLiveObject liveObject = (RLiveObject) arg;
removeIndex(liveMap, me, field);
storeIndex(field, me, liveObject.getLiveObjectId());
Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
NamingScheme ns = commandExecutor.getObjectBuilder().getNamingScheme(rEntity);
if (commandExecutor instanceof CommandBatchService) {
liveMap.fastPutAsync(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
} else {
liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
}
return me;
}
if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(), REntity.class).fieldTransformation())) {
RObject rObject = commandExecutor.getObjectBuilder().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) {
if (commandExecutor instanceof CommandBatchService) {
commandExecutor.getObjectBuilder().storeAsync((RObject) arg, fieldName, liveMap);
} else {
commandExecutor.getObjectBuilder().store((RObject) arg, fieldName, liveMap);
}
return me;
}
removeIndex(liveMap, me, field);
if (arg != null) {
storeIndex(field, me, arg);
if (commandExecutor instanceof CommandBatchService) {
liveMap.fastPutAsync(fieldName, arg);
} else {
liveMap.fastPut(fieldName, arg);
}
}
return me;
}
return superMethod.call();
}
Aggregations