use of org.redisson.api.annotation.REntity 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.annotation.REntity in project redisson by redisson.
the class RedissonObjectFactory method toReference.
public static RedissonReference toReference(RedissonReactiveClient redissonReactive, Object object) {
if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
RObjectReactive rObject = ((RObjectReactive) object);
redissonReactive.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
return new RedissonReference(object.getClass(), ((RObjectReactive) object).getName(), ((RObjectReactive) 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(redissonReactive.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.annotation.REntity in project redisson by redisson.
the class RedissonObjectFactory method fromReference.
public static <T> T fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
Class<? extends Object> type = rr.getType();
CodecProvider codecProvider = redisson.getConfig().getCodecProvider();
if (type != null) {
if (type.isAnnotationPresent(REntity.class)) {
RLiveObjectService liveObjectService = redisson.getLiveObjectService();
REntity anno = type.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, type));
return (T) liveObjectService.get(type, ns.resolveId(rr.getKeyName()));
}
List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
for (Class<?> iType : interfaces) {
if (builders.containsKey(iType)) {
// user cache to speed up things a little.
Method builder = builders.get(iType).get(isDefaultCodec(rr));
return (T) (isDefaultCodec(rr) ? builder.invoke(redisson, rr.getKeyName()) : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
}
}
}
throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
}
use of org.redisson.api.annotation.REntity in project redisson by redisson.
the class RedissonObjectBuilder method getNamingScheme.
public NamingScheme getNamingScheme(Class<?> entityClass) {
REntity anno = ClassUtils.getAnnotation(entityClass, REntity.class);
Codec codec = codecProvider.getCodec(anno, entityClass, config);
return getNamingScheme(entityClass, codec);
}
use of org.redisson.api.annotation.REntity in project redisson by redisson.
the class RedissonObjectBuilder method getFieldNamingScheme.
/**
* WARNING: rEntity has to be the class of @This object.
*/
private NamingScheme getFieldNamingScheme(Class<?> rEntity, String fieldName, Codec c) throws Exception {
if (!namingSchemeCache.containsKey(fieldName)) {
REntity anno = rEntity.getAnnotation(REntity.class);
namingSchemeCache.putIfAbsent(fieldName, anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(c));
}
return namingSchemeCache.get(fieldName);
}
Aggregations