refactoring

pull/2300/head
Nikita Koksharov 6 years ago
parent 90c19483c2
commit d7e186eb12

@ -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) {

@ -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<? extends Codec> getCodecType() throws Exception {
public Class<? extends Codec> getCodecType() throws ClassNotFoundException {
if (codec != null) {
return (Class<? extends Codec>) Class.forName(codec);
}

@ -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>) 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 <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) {
protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) throws ReflectiveOperationException {
if (isRedissonReferenceSupportEnabled()) {
handleReference(promise, res);
} else {
@ -1101,11 +1082,11 @@ public class CommandAsyncService implements CommandAsyncExecutor {
}
}
private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
private <R, V> void handleReference(RPromise<R> 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<Object> r = (List<Object>) 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 <R, V> void sendCommand(AsyncDetails<V, R> details, RedisConnection connection) {

@ -205,7 +205,7 @@ public class CommandBatchService extends CommandAsyncService {
}
@Override
protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) {
protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> 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);
}
}

@ -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;

@ -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<? extends RObject> 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<? extends RObject> rObjectClass, String fieldName) throws Exception {
private Codec getFieldCodec(Class<?> rEntity, Class<? extends RObject> 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<? extends Object> 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<? extends Object> 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<? extends Object> 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<? extends Object> 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 extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws Exception {
private <T extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws ReflectiveOperationException {
List<Class<?>> interfaces = Arrays.asList(expectedType.getInterfaces());
for (Class<?> iType : interfaces) {
if (REFERENCES.containsKey(iType)) {// user cache to speed up things a little.

Loading…
Cancel
Save