use of org.redisson.api.RObject in project redisson by redisson.
the class RedissonLiveObjectServiceTest method testRObject.
@Test
public void testRObject() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass myObject = new TestClass();
myObject = service.persist(myObject);
try {
((RObject) myObject).isExists();
} catch (Exception e) {
assertEquals("Please use RLiveObjectService instance for this type of functions", e.getMessage());
}
}
use of org.redisson.api.RObject in project redisson by redisson.
the class RedissonObjectBuilder method createObject.
public RObject createObject(Object id, Class<?> clazz, Class<?> fieldType, String fieldName) {
Class<? extends RObject> mappedClass = getMappedClass(fieldType);
try {
if (mappedClass != null) {
Codec fieldCodec = getFieldCodec(clazz, mappedClass, fieldName);
NamingScheme fieldNamingScheme = getFieldNamingScheme(clazz, fieldName, fieldCodec);
RObject obj = RedissonObjectFactory.createRObject(redisson, mappedClass, fieldNamingScheme.getFieldReferenceName(clazz, id, mappedClass, fieldName, null), fieldCodec);
return obj;
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return null;
}
use of org.redisson.api.RObject in project redisson by redisson.
the class RedissonObjectFactory method toReference.
public static RedissonReference toReference(RedissonClient redisson, Object object) {
if (object != null && object.getClass().isAnnotationPresent(REntity.class)) {
throw new IllegalArgumentException("REntity should be attached to Redisson before save");
}
if (object instanceof RObject && !(object instanceof RLiveObject)) {
RObject rObject = ((RObject) object);
redisson.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
return new RedissonReference(object.getClass(), ((RObject) object).getName(), ((RObject) object).getCodec());
}
try {
if (object instanceof RLiveObject) {
Class<? extends Object> rEntity = object.getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(redisson.getCodecProvider().getCodec(anno, (Class) rEntity));
String name = Introspectior.getFieldsWithAnnotation(rEntity, RId.class).getOnly().getName();
Class<?> type = rEntity.getDeclaredField(name).getType();
return new RedissonReference(rEntity, ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return null;
}
use of org.redisson.api.RObject in project redisson by redisson.
the class RedissonLiveObjectService method persist.
private <T> T persist(T detachedObject, Map<Object, Object> alreadyPersisted, RCascadeType type) {
String idFieldName = getRIdFieldName(detachedObject.getClass());
Object id = ClassUtils.getField(detachedObject, idFieldName);
if (id == null) {
try {
id = generateId(detachedObject.getClass());
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException(e);
}
ClassUtils.setField(detachedObject, idFieldName, id);
}
T attachedObject = attach(detachedObject);
alreadyPersisted.put(detachedObject, attachedObject);
RMap<String, Object> liveMap = getMap(attachedObject);
List<String> excludedFields = new ArrayList<String>();
excludedFields.add(idFieldName);
boolean fastResult = liveMap.fastPut("redisson_live_object", "1");
if (type == RCascadeType.PERSIST && !fastResult) {
throw new IllegalArgumentException("This REntity already exists.");
}
for (FieldDescription.InDefinedShape field : Introspectior.getFieldsDescription(detachedObject.getClass())) {
Object object = ClassUtils.getField(detachedObject, field.getName());
if (object == null) {
continue;
}
RObject rObject = objectBuilder.createObject(id, detachedObject.getClass(), object.getClass(), field.getName());
if (rObject != null) {
objectBuilder.store(rObject, field.getName(), liveMap);
if (rObject instanceof SortedSet) {
((RSortedSet) rObject).trySetComparator(((SortedSet) object).comparator());
}
if (rObject instanceof Collection) {
for (Object obj : (Collection<Object>) object) {
if (obj != null && obj.getClass().isAnnotationPresent(REntity.class)) {
Object persisted = alreadyPersisted.get(obj);
if (persisted == null) {
if (checkCascade(detachedObject, type, field.getName())) {
persisted = persist(obj, alreadyPersisted, type);
}
}
obj = persisted;
}
((Collection) rObject).add(obj);
}
} else if (rObject instanceof Map) {
Map<Object, Object> rMap = (Map<Object, Object>) rObject;
Map<?, ?> map = (Map<?, ?>) rObject;
for (Entry<?, ?> entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null && key.getClass().isAnnotationPresent(REntity.class)) {
Object persisted = alreadyPersisted.get(key);
if (persisted == null) {
if (checkCascade(detachedObject, type, field.getName())) {
persisted = persist(key, alreadyPersisted, type);
}
}
key = persisted;
}
if (value != null && value.getClass().isAnnotationPresent(REntity.class)) {
Object persisted = alreadyPersisted.get(value);
if (persisted == null) {
if (checkCascade(detachedObject, type, field.getName())) {
persisted = persist(value, alreadyPersisted, type);
}
}
value = persisted;
}
rMap.put(key, value);
}
}
excludedFields.add(field.getName());
} else if (object.getClass().isAnnotationPresent(REntity.class)) {
Object persisted = alreadyPersisted.get(object);
if (persisted == null) {
if (checkCascade(detachedObject, type, field.getName())) {
persisted = persist(object, alreadyPersisted, type);
}
}
excludedFields.add(field.getName());
BeanUtil.pojo.setSimpleProperty(attachedObject, field.getName(), persisted);
} else {
validateAnnotation(detachedObject, field.getName());
}
}
copy(detachedObject, attachedObject, excludedFields);
return attachedObject;
}
use of org.redisson.api.RObject 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