diff --git a/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java b/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java index 158646268..99dc8f119 100644 --- a/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java +++ b/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java @@ -48,8 +48,9 @@ public class RedissonObjectBuilder { public enum ReferenceType {RXJAVA, REACTIVE, DEFAULT} private static final Map, Class> SUPPORTED_CLASS_MAPPING = new LinkedHashMap<>(); - private static final Map, CodecMethodRef> REFERENCES = new HashMap<>(); - + private static final Map, Method> DEFAULT_CODEC_REFERENCES = new HashMap<>(); + private static final Map, Method> CUSTOM_CODEC_REFERENCES = new HashMap<>(); + static { SUPPORTED_CLASS_MAPPING.put(SortedSet.class, RedissonSortedSet.class); SUPPORTED_CLASS_MAPPING.put(Set.class, RedissonSet.class); @@ -61,9 +62,9 @@ public class RedissonObjectBuilder { SUPPORTED_CLASS_MAPPING.put(Queue.class, RedissonQueue.class); SUPPORTED_CLASS_MAPPING.put(List.class, RedissonList.class); - fillCodecMethods(REFERENCES, RedissonClient.class, RObject.class); - fillCodecMethods(REFERENCES, RedissonReactiveClient.class, RObjectReactive.class); - fillCodecMethods(REFERENCES, RedissonRxClient.class, RObjectRx.class); + fillCodecMethods(RedissonClient.class, RObject.class); + fillCodecMethods(RedissonReactiveClient.class, RObjectReactive.class); + fillCodecMethods(RedissonRxClient.class, RObjectRx.class); } private final Config config; @@ -71,19 +72,6 @@ public class RedissonObjectBuilder { private RedissonReactiveClient redissonReactive; 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(); public RedissonObjectBuilder(RedissonClient redisson) { @@ -185,23 +173,20 @@ public class RedissonObjectBuilder { return null; } - private static void fillCodecMethods(Map, CodecMethodRef> map, Class clientClazz, Class objectClazz) { + private static void fillCodecMethods(Class clientClazz, Class objectClazz) { for (Method method : clientClazz.getDeclaredMethods()) { if (!method.getReturnType().equals(Void.TYPE) && objectClazz.isAssignableFrom(method.getReturnType()) && method.getName().startsWith("get")) { + 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. && String.class == method.getParameterTypes()[0] && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) { - builder.customCodecMethod = method; + CUSTOM_CODEC_REFERENCES.put(cls, method); } else if (method.getParameterTypes().length == 1 && 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 { - Class type = rr.getType(); + Class type = rr.getType(); if (type != null) { if (ClassUtils.isAnnotationPresent(type, REntity.class)) { RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService(); @@ -231,20 +216,23 @@ public class RedissonObjectBuilder { return getObject(redisson, rr, type, codecProvider); } - private Object getObject(Object redisson, RedissonReference rr, Class type, + private Object getObject(Object redisson, RedissonReference rr, Class type, ReferenceCodecProvider codecProvider) throws ReflectiveOperationException { if (type != null) { - CodecMethodRef b = REFERENCES.get(type); - if (b == null && type.getInterfaces().length > 0) { + if (!DEFAULT_CODEC_REFERENCES.containsKey(type) && type.getInterfaces().length > 0) { type = type.getInterfaces()[0]; } - b = REFERENCES.get(type); - if (b != null) { - Method builder = b.get(isDefaultCodec(rr)); - if (isDefaultCodec(rr)) { - return builder.invoke(redisson, rr.getKeyName()); + + if (isDefaultCodec(rr)) { + 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()); @@ -256,7 +244,7 @@ public class RedissonObjectBuilder { } private Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws ReflectiveOperationException { - Class type = rr.getRxJavaType(); + Class type = rr.getRxJavaType(); /** * 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 { - Class type = rr.getReactiveType(); + Class type = rr.getReactiveType(); /** * Live Object from reference in reactive client is not supported yet. */ @@ -306,7 +294,7 @@ public class RedissonObjectBuilder { try { if (object instanceof RLiveObject) { - Class rEntity = object.getClass().getSuperclass(); + Class rEntity = object.getClass().getSuperclass(); NamingScheme ns = getNamingScheme(rEntity); return new RedissonReference(rEntity, @@ -321,14 +309,18 @@ public class RedissonObjectBuilder { private T createRObject(RedissonClient redisson, Class expectedType, String name, K codec) throws ReflectiveOperationException { Class[] interfaces = expectedType.getInterfaces(); 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, codec); + } else { + Method builder = CUSTOM_CODEC_REFERENCES.get(iType); + if (builder != null) { + return (T) builder.invoke(redisson, name, codec); + } } }