From d7e186eb1298c8af238e65e09a66fb846a143fda Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 19 Aug 2019 12:34:18 +0300 Subject: [PATCH] refactoring --- .../redisson/RedissonLiveObjectService.java | 2 +- .../java/org/redisson/RedissonReference.java | 21 +++----- .../redisson/command/CommandAsyncService.java | 53 +++++-------------- .../redisson/command/CommandBatchService.java | 8 ++- .../liveobject/core/AccessorInterceptor.java | 6 +-- .../core/RedissonObjectBuilder.java | 36 +++++++++---- 6 files changed, 54 insertions(+), 72 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java index 399f326cd..bdafc35d6 100644 --- a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java +++ b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java @@ -304,7 +304,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { continue; } - RObject rObject = commandExecutor.getObjectBuilder().createObject(id, detachedObject.getClass(), object.getClass(), field.getName(), redisson); + RObject rObject = commandExecutor.getObjectBuilder().createObject(id, detachedObject.getClass(), object.getClass(), field.getName()); if (rObject != null) { commandExecutor.getObjectBuilder().store(rObject, field.getName(), liveMap); if (rObject instanceof SortedSet) { diff --git a/redisson/src/main/java/org/redisson/RedissonReference.java b/redisson/src/main/java/org/redisson/RedissonReference.java index e56224086..ab3fd3774 100644 --- a/redisson/src/main/java/org/redisson/RedissonReference.java +++ b/redisson/src/main/java/org/redisson/RedissonReference.java @@ -111,23 +111,17 @@ public class RedissonReference implements Serializable { /** * @return the type - * @throws java.lang.Exception - which could be: - * LinkageError - if the linkage fails - * ExceptionInInitializerError - if the initialization provoked by this method fails - * ClassNotFoundException - if the class cannot be located + * @throws java.lang.ClassNotFoundException - if the class cannot be located */ - public Class getType() throws Exception { + public Class getType() throws ClassNotFoundException { return Class.forName(type); } /** * @return the type - * @throws java.lang.Exception - which could be: - * LinkageError - if the linkage fails - * ExceptionInInitializerError - if the initialization provoked by this method fails - * ClassNotFoundException - if the class cannot be located + * @throws java.lang.ClassNotFoundException - if the class cannot be located */ - public Class getReactiveType() throws Exception { + public Class getReactiveType() throws ClassNotFoundException { if (REACTIVE_MAP.containsValue(type)) { return Class.forName(REACTIVE_MAP.reverseGet(type)); //live object is not supported in reactive client } @@ -179,12 +173,9 @@ public class RedissonReference implements Serializable { /** * @return the codec - * @throws java.lang.Exception - which could be: - * LinkageError - if the linkage fails - * ExceptionInInitializerError - if the initialization provoked by this method fails - * ClassNotFoundException - if the class cannot be located + * @throws java.lang.ClassNotFoundException - if the class cannot be located */ - public Class getCodecType() throws Exception { + public Class getCodecType() throws ClassNotFoundException { if (codec != null) { return (Class) Class.forName(codec); } diff --git a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java index cbaaa45d1..eb53044ca 100644 --- a/redisson/src/main/java/org/redisson/command/CommandAsyncService.java +++ b/redisson/src/main/java/org/redisson/command/CommandAsyncService.java @@ -101,9 +101,6 @@ public class CommandAsyncService implements CommandAsyncExecutor { final ConnectionManager connectionManager; private RedissonObjectBuilder objectBuilder; - protected RedissonClient redisson; - protected RedissonReactiveClient redissonReactive; - protected RedissonRxClient redissonRx; public CommandAsyncService(ConnectionManager connectionManager) { this.connectionManager = connectionManager; @@ -116,47 +113,32 @@ public class CommandAsyncService implements CommandAsyncExecutor { @Override public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonClient redisson) { - if (redisson != null) { - this.redisson = redisson; - enableRedissonReferenceSupport(redisson.getConfig()); - this.redissonReactive = null; - this.redissonRx = null; - } + enableRedissonReferenceSupport(redisson.getConfig(), redisson, null, null); return this; } @Override public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonReactiveClient redissonReactive) { - if (redissonReactive != null) { - this.redissonReactive = redissonReactive; - enableRedissonReferenceSupport(redissonReactive.getConfig()); - this.redisson = null; - this.redissonRx = null; - } + enableRedissonReferenceSupport(redissonReactive.getConfig(), null, redissonReactive, null); return this; } @Override public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonRxClient redissonRx) { - if (redissonRx != null) { - this.redissonReactive = null; - enableRedissonReferenceSupport(redissonRx.getConfig()); - this.redisson = null; - this.redissonRx = redissonRx; - } + enableRedissonReferenceSupport(redissonRx.getConfig(), null, null, redissonRx); return this; } - private void enableRedissonReferenceSupport(Config config) { + private void enableRedissonReferenceSupport(Config config, RedissonClient redisson, RedissonReactiveClient redissonReactive, RedissonRxClient redissonRx) { Codec codec = config.getCodec(); - objectBuilder = new RedissonObjectBuilder(config); + objectBuilder = new RedissonObjectBuilder(config, redisson, redissonReactive, redissonRx); ReferenceCodecProvider codecProvider = objectBuilder.getReferenceCodecProvider(); codecProvider.registerCodec((Class) codec.getClass(), codec); } @Override public boolean isRedissonReferenceSupportEnabled() { - return redisson != null || redissonReactive != null || redissonRx != null; + return objectBuilder != null; } @Override @@ -1085,7 +1067,6 @@ public class CommandAsyncService implements CommandAsyncExecutor { AsyncDetails.release(details); } catch (Exception e) { handleError(details, details.getMainPromise(), e); - throw e; } } @@ -1093,7 +1074,7 @@ public class CommandAsyncService implements CommandAsyncExecutor { mainPromise.tryFailure(cause); } - protected void handleSuccess(AsyncDetails details, RPromise promise, RedisCommand command, R res) { + protected void handleSuccess(AsyncDetails details, RPromise promise, RedisCommand command, R res) throws ReflectiveOperationException { if (isRedissonReferenceSupportEnabled()) { handleReference(promise, res); } else { @@ -1101,11 +1082,11 @@ public class CommandAsyncService implements CommandAsyncExecutor { } } - private void handleReference(RPromise mainPromise, R res) { + private void handleReference(RPromise mainPromise, R res) throws ReflectiveOperationException { mainPromise.trySuccess((R) tryHandleReference(res)); } - protected Object tryHandleReference(Object o) { + protected Object tryHandleReference(Object o) throws ReflectiveOperationException { boolean hasConversion = false; if (o instanceof List) { List r = (List) o; @@ -1198,7 +1179,7 @@ public class CommandAsyncService implements CommandAsyncExecutor { } } - private Object tryHandleReference0(Object o) { + private Object tryHandleReference0(Object o) throws ReflectiveOperationException { if (o instanceof RedissonReference) { return fromReference(o); } else if (o instanceof ScoredEntry && ((ScoredEntry) o).getValue() instanceof RedissonReference) { @@ -1215,22 +1196,12 @@ public class CommandAsyncService implements CommandAsyncExecutor { return o; } - private Object fromReference(Object res) { + private Object fromReference(Object res) throws ReflectiveOperationException { if (objectBuilder == null) { return res; } - try { - if (redisson != null) { - return objectBuilder.fromReference(redisson, (RedissonReference) res); - } - if (redissonReactive != null) { - return objectBuilder.fromReference(redissonReactive, (RedissonReference) res); - } - return objectBuilder.fromReference(redissonRx, (RedissonReference) res); - } catch (Exception exception) { - throw new IllegalStateException(exception); - } + return objectBuilder.fromReference((RedissonReference) res); } protected void sendCommand(AsyncDetails details, RedisConnection connection) { diff --git a/redisson/src/main/java/org/redisson/command/CommandBatchService.java b/redisson/src/main/java/org/redisson/command/CommandBatchService.java index ca67cb7d2..34aa54091 100644 --- a/redisson/src/main/java/org/redisson/command/CommandBatchService.java +++ b/redisson/src/main/java/org/redisson/command/CommandBatchService.java @@ -205,7 +205,7 @@ public class CommandBatchService extends CommandAsyncService { } @Override - protected void handleSuccess(AsyncDetails details, RPromise promise, RedisCommand command, R res) { + protected void handleSuccess(AsyncDetails details, RPromise promise, RedisCommand command, R res) throws ReflectiveOperationException { if (RedisCommands.EXEC.getName().equals(command.getName())) { super.handleSuccess(details, promise, command, res); return; @@ -568,7 +568,11 @@ public class CommandBatchService extends CommandAsyncService { } else if (!commandEntry.getCommand().getName().equals(RedisCommands.MULTI.getName()) && !commandEntry.getCommand().getName().equals(RedisCommands.EXEC.getName())) { Object entryResult = commandEntry.getPromise().getNow(); - entryResult = tryHandleReference(entryResult); + try { + entryResult = tryHandleReference(entryResult); + } catch (ReflectiveOperationException exc) { + log.error("Unable to handle reference from " + entryResult, exc); + } responses.add(entryResult); } } diff --git a/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java b/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java index ccdb81d1b..e6e361bcf 100644 --- a/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java +++ b/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java @@ -79,7 +79,7 @@ public class AccessorInterceptor { if (isGetter(method, fieldName)) { Object result = liveMap.get(fieldName); if (result == null) { - RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName, redisson); + RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName); if (ar != null) { objectBuilder.store(ar, fieldName, liveMap); return ar; @@ -93,7 +93,7 @@ public class AccessorInterceptor { return result; } if (result instanceof RedissonReference) { - return objectBuilder.fromReference(redisson, (RedissonReference) result); + return objectBuilder.fromReference((RedissonReference) result); } return result; } @@ -121,7 +121,7 @@ public class AccessorInterceptor { && TransformationMode.ANNOTATION_BASED .equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(), REntity.class).fieldTransformation())) { - RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName, redisson); + RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName); if (arg != null) { if (rObject instanceof Collection) { Collection c = (Collection) rObject; 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 eecb33938..1513cd1ba 100644 --- a/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java +++ b/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java @@ -16,7 +16,6 @@ package org.redisson.liveobject.core; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Deque; @@ -89,6 +88,9 @@ public class RedissonObjectBuilder { } private final Config config; + private final RedissonClient redisson; + private final RedissonReactiveClient redissonReactive; + private final RedissonRxClient redissonRx; public static class CodecMethodRef { @@ -105,9 +107,13 @@ public class RedissonObjectBuilder { private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider(); - public RedissonObjectBuilder(Config config) { + public RedissonObjectBuilder(Config config, + RedissonClient redisson, RedissonReactiveClient redissonReactive, RedissonRxClient redissonRx) { super(); this.config = config; + this.redisson = redisson; + this.redissonReactive = redissonReactive; + this.redissonRx = redissonRx; } public ReferenceCodecProvider getReferenceCodecProvider() { @@ -123,7 +129,7 @@ public class RedissonObjectBuilder { new RedissonReference(ar.getClass(), ar.getName(), codec)); } - public RObject createObject(Object id, Class clazz, Class fieldType, String fieldName, RedissonClient redisson) { + public RObject createObject(Object id, Class clazz, Class fieldType, String fieldName) { Class mappedClass = getMappedClass(fieldType); try { if (mappedClass != null) { @@ -142,7 +148,7 @@ public class RedissonObjectBuilder { /** * WARNING: rEntity has to be the class of @This object. */ - private Codec getFieldCodec(Class rEntity, Class rObjectClass, String fieldName) throws Exception { + private Codec getFieldCodec(Class rEntity, Class rObjectClass, String fieldName) throws ReflectiveOperationException { Field field = ClassUtils.getDeclaredField(rEntity, fieldName); if (field.isAnnotationPresent(RObjectField.class)) { RObjectField anno = field.getAnnotation(RObjectField.class); @@ -197,7 +203,18 @@ public class RedissonObjectBuilder { } } - public Object fromReference(RedissonClient redisson, RedissonReference rr) throws Exception { + public Object fromReference(RedissonReference rr) throws ReflectiveOperationException { + if (redisson != null) { + return fromReference(redisson, rr); + } else if (redissonReactive != null) { + return fromReference(redissonReactive, rr); + } else if (redissonRx != null) { + return fromReference(redissonRx, rr); + } + throw new IllegalStateException(); + } + + private Object fromReference(RedissonClient redisson, RedissonReference rr) throws ReflectiveOperationException { Class type = rr.getType(); if (type != null) { if (ClassUtils.isAnnotationPresent(type, REntity.class)) { @@ -213,8 +230,7 @@ public class RedissonObjectBuilder { } private Object getObject(Object redisson, RedissonReference rr, Class type, - ReferenceCodecProvider codecProvider) - throws IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException { + ReferenceCodecProvider codecProvider) throws ReflectiveOperationException { if (type != null) { CodecMethodRef b = REFERENCES.get(type); if (b == null && type.getInterfaces().length > 0) { @@ -236,7 +252,7 @@ public class RedissonObjectBuilder { return rr.getCodec() == null; } - public Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws Exception { + private Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws ReflectiveOperationException { Class type = rr.getReactiveType(); /** * Live Object from reference in reactive client is not supported yet. @@ -244,7 +260,7 @@ public class RedissonObjectBuilder { return getObject(redisson, rr, type, codecProvider); } - public Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception { + private Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws ReflectiveOperationException { Class type = rr.getReactiveType(); /** * Live Object from reference in reactive client is not supported yet. @@ -294,7 +310,7 @@ public class RedissonObjectBuilder { return null; } - public T createRObject(RedissonClient redisson, Class expectedType, String name, K codec) throws Exception { + private T createRObject(RedissonClient redisson, Class expectedType, String name, K codec) throws ReflectiveOperationException { List> interfaces = Arrays.asList(expectedType.getInterfaces()); for (Class iType : interfaces) { if (REFERENCES.containsKey(iType)) {// user cache to speed up things a little.