refactoring

pull/6080/head
Nikita Koksharov 8 months ago
parent a8e19f3fd6
commit 0a8e5de4e7

@ -48,7 +48,8 @@ public class RedissonObjectBuilder {
public enum ReferenceType {RXJAVA, REACTIVE, DEFAULT} public enum ReferenceType {RXJAVA, REACTIVE, DEFAULT}
private static final Map<Class<?>, Class<? extends RObject>> SUPPORTED_CLASS_MAPPING = new LinkedHashMap<>(); private static final Map<Class<?>, Class<? extends RObject>> SUPPORTED_CLASS_MAPPING = new LinkedHashMap<>();
private static final Map<Class<?>, CodecMethodRef> REFERENCES = new HashMap<>(); private static final Map<Class<?>, Method> DEFAULT_CODEC_REFERENCES = new HashMap<>();
private static final Map<Class<?>, Method> CUSTOM_CODEC_REFERENCES = new HashMap<>();
static { static {
SUPPORTED_CLASS_MAPPING.put(SortedSet.class, RedissonSortedSet.class); SUPPORTED_CLASS_MAPPING.put(SortedSet.class, RedissonSortedSet.class);
@ -61,9 +62,9 @@ public class RedissonObjectBuilder {
SUPPORTED_CLASS_MAPPING.put(Queue.class, RedissonQueue.class); SUPPORTED_CLASS_MAPPING.put(Queue.class, RedissonQueue.class);
SUPPORTED_CLASS_MAPPING.put(List.class, RedissonList.class); SUPPORTED_CLASS_MAPPING.put(List.class, RedissonList.class);
fillCodecMethods(REFERENCES, RedissonClient.class, RObject.class); fillCodecMethods(RedissonClient.class, RObject.class);
fillCodecMethods(REFERENCES, RedissonReactiveClient.class, RObjectReactive.class); fillCodecMethods(RedissonReactiveClient.class, RObjectReactive.class);
fillCodecMethods(REFERENCES, RedissonRxClient.class, RObjectRx.class); fillCodecMethods(RedissonRxClient.class, RObjectRx.class);
} }
private final Config config; private final Config config;
@ -71,19 +72,6 @@ public class RedissonObjectBuilder {
private RedissonReactiveClient redissonReactive; private RedissonReactiveClient redissonReactive;
private RedissonRxClient redissonRx; private RedissonRxClient redissonRx;
public static class CodecMethodRef {
Method defaultCodecMethod;
Method customCodecMethod;
Method get(boolean value) {
if (value) {
return defaultCodecMethod;
}
return customCodecMethod;
}
}
private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider(); private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider();
public RedissonObjectBuilder(RedissonClient redisson) { public RedissonObjectBuilder(RedissonClient redisson) {
@ -185,23 +173,20 @@ public class RedissonObjectBuilder {
return null; return null;
} }
private static void fillCodecMethods(Map<Class<?>, CodecMethodRef> map, Class<?> clientClazz, Class<?> objectClazz) { private static void fillCodecMethods(Class<?> clientClazz, Class<?> objectClazz) {
for (Method method : clientClazz.getDeclaredMethods()) { for (Method method : clientClazz.getDeclaredMethods()) {
if (!method.getReturnType().equals(Void.TYPE) if (!method.getReturnType().equals(Void.TYPE)
&& objectClazz.isAssignableFrom(method.getReturnType()) && objectClazz.isAssignableFrom(method.getReturnType())
&& method.getName().startsWith("get")) { && method.getName().startsWith("get")) {
Class<?> cls = method.getReturnType(); Class<?> cls = method.getReturnType();
if (!map.containsKey(cls)) {
map.put(cls, new CodecMethodRef());
}
CodecMethodRef builder = map.get(cls);
if (method.getParameterTypes().length == 2 //first param is name, second param is codec. if (method.getParameterTypes().length == 2 //first param is name, second param is codec.
&& String.class == method.getParameterTypes()[0] && String.class == method.getParameterTypes()[0]
&& Codec.class.isAssignableFrom(method.getParameterTypes()[1])) { && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) {
builder.customCodecMethod = method; CUSTOM_CODEC_REFERENCES.put(cls, method);
} else if (method.getParameterTypes().length == 1 } else if (method.getParameterTypes().length == 1
&& String.class == method.getParameterTypes()[0]) { && String.class == method.getParameterTypes()[0]) {
builder.defaultCodecMethod = method; DEFAULT_CODEC_REFERENCES.put(cls, method);
} }
} }
} }
@ -217,7 +202,7 @@ public class RedissonObjectBuilder {
} }
private Object fromReference(RedissonClient redisson, RedissonReference rr) throws ReflectiveOperationException { private Object fromReference(RedissonClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getType(); Class<?> type = rr.getType();
if (type != null) { if (type != null) {
if (ClassUtils.isAnnotationPresent(type, REntity.class)) { if (ClassUtils.isAnnotationPresent(type, REntity.class)) {
RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService(); RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService();
@ -231,20 +216,23 @@ public class RedissonObjectBuilder {
return getObject(redisson, rr, type, codecProvider); return getObject(redisson, rr, type, codecProvider);
} }
private Object getObject(Object redisson, RedissonReference rr, Class<? extends Object> type, private Object getObject(Object redisson, RedissonReference rr, Class<?> type,
ReferenceCodecProvider codecProvider) throws ReflectiveOperationException { ReferenceCodecProvider codecProvider) throws ReflectiveOperationException {
if (type != null) { if (type != null) {
CodecMethodRef b = REFERENCES.get(type); if (!DEFAULT_CODEC_REFERENCES.containsKey(type) && type.getInterfaces().length > 0) {
if (b == null && type.getInterfaces().length > 0) {
type = type.getInterfaces()[0]; type = type.getInterfaces()[0];
} }
b = REFERENCES.get(type);
if (b != null) {
Method builder = b.get(isDefaultCodec(rr));
if (isDefaultCodec(rr)) { if (isDefaultCodec(rr)) {
return builder.invoke(redisson, rr.getKeyName()); Method m = DEFAULT_CODEC_REFERENCES.get(type);
if (m != null) {
return m.invoke(redisson, rr.getKeyName());
}
} else {
Method m = CUSTOM_CODEC_REFERENCES.get(type);
if (m != null) {
return m.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType()));
} }
return 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.getCodec()); throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodec());
@ -256,7 +244,7 @@ public class RedissonObjectBuilder {
} }
private Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws ReflectiveOperationException { private Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getRxJavaType(); Class<?> type = rr.getRxJavaType();
/** /**
* Live Object from reference in rxjava client is not supported yet. * Live Object from reference in rxjava client is not supported yet.
*/ */
@ -264,7 +252,7 @@ public class RedissonObjectBuilder {
} }
private Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws ReflectiveOperationException { private Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getReactiveType(); Class<?> type = rr.getReactiveType();
/** /**
* Live Object from reference in reactive client is not supported yet. * Live Object from reference in reactive client is not supported yet.
*/ */
@ -306,7 +294,7 @@ public class RedissonObjectBuilder {
try { try {
if (object instanceof RLiveObject) { if (object instanceof RLiveObject) {
Class<? extends Object> rEntity = object.getClass().getSuperclass(); Class<?> rEntity = object.getClass().getSuperclass();
NamingScheme ns = getNamingScheme(rEntity); NamingScheme ns = getNamingScheme(rEntity);
return new RedissonReference(rEntity, return new RedissonReference(rEntity,
@ -321,16 +309,20 @@ public class RedissonObjectBuilder {
private <T extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws ReflectiveOperationException { private <T extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws ReflectiveOperationException {
Class<?>[] interfaces = expectedType.getInterfaces(); Class<?>[] interfaces = expectedType.getInterfaces();
for (Class<?> iType : interfaces) { for (Class<?> iType : interfaces) {
if (REFERENCES.containsKey(iType)) {
boolean isDefaultCodec = codec.getClass() == config.getCodec().getClass(); boolean isDefaultCodec = codec.getClass() == config.getCodec().getClass();
Method builder = REFERENCES.get(iType).get(isDefaultCodec);
if (isDefaultCodec) { if (isDefaultCodec) {
Method builder = DEFAULT_CODEC_REFERENCES.get(iType);
if (builder != null) {
return (T) builder.invoke(redisson, name); return (T) builder.invoke(redisson, name);
} }
} else {
Method builder = CUSTOM_CODEC_REFERENCES.get(iType);
if (builder != null) {
return (T) builder.invoke(redisson, name, codec); return (T) builder.invoke(redisson, name, codec);
} }
} }
}
String codecName = null; String codecName = null;
if (codec != null) { if (codec != null) {

Loading…
Cancel
Save