Search in sources :

Example 1 with RuntimeType

use of net.bytebuddy.implementation.bind.annotation.RuntimeType in project redisson by redisson.

the class AccessorInterceptor method intercept.

@RuntimeType
public Object intercept(@Origin Method method, @SuperCall Callable<?> superMethod, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectLiveMap") RMap<String, Object> liveMap) throws Exception {
    if (isGetter(method, getREntityIdFieldName(me))) {
        return ((RLiveObject) me).getLiveObjectId();
    }
    if (isSetter(method, getREntityIdFieldName(me))) {
        ((RLiveObject) me).setLiveObjectId(args[0]);
        return null;
    }
    String fieldName = getFieldName(method);
    Class<?> fieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
    if (isGetter(method, fieldName)) {
        Object result = liveMap.get(fieldName);
        if (result == null) {
            RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
            if (ar != null) {
                objectBuilder.store(ar, fieldName, liveMap);
                return ar;
            }
        }
        if (result instanceof RedissonReference) {
            return RedissonObjectFactory.fromReference(redisson, (RedissonReference) result);
        }
        return result;
    }
    if (isSetter(method, fieldName)) {
        Object arg = args[0];
        if (arg != null && arg.getClass().isAnnotationPresent(REntity.class)) {
            throw new IllegalStateException("REntity object should be attached to Redisson first");
        }
        if (arg instanceof RLiveObject) {
            RLiveObject liveObject = (RLiveObject) arg;
            Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, (Class) rEntity));
            liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, fieldType, getREntityIdFieldName(liveObject), liveObject.getLiveObjectId())));
            return me;
        }
        if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(me.getClass().getSuperclass().getAnnotation(REntity.class).fieldTransformation())) {
            RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
            if (arg != null) {
                if (rObject instanceof Collection) {
                    Collection c = (Collection) rObject;
                    c.clear();
                    c.addAll((Collection) arg);
                } else {
                    Map m = (Map) rObject;
                    m.clear();
                    m.putAll((Map) arg);
                }
            }
            if (rObject != null) {
                arg = rObject;
            }
        }
        if (arg instanceof RObject) {
            objectBuilder.store((RObject) arg, fieldName, liveMap);
            return me;
        }
        if (arg == null) {
            liveMap.remove(fieldName);
        } else {
            liveMap.fastPut(fieldName, arg);
        }
        return me;
    }
    return superMethod.call();
}
Also used : RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) Collection(java.util.Collection) RLiveObject(org.redisson.api.RLiveObject) RObject(org.redisson.api.RObject) RMap(org.redisson.api.RMap) Map(java.util.Map) RuntimeType(net.bytebuddy.implementation.bind.annotation.RuntimeType)

Aggregations

Collection (java.util.Collection)1 Map (java.util.Map)1 RuntimeType (net.bytebuddy.implementation.bind.annotation.RuntimeType)1 RedissonReference (org.redisson.RedissonReference)1 RLiveObject (org.redisson.api.RLiveObject)1 RMap (org.redisson.api.RMap)1 RObject (org.redisson.api.RObject)1 REntity (org.redisson.api.annotation.REntity)1 Codec (org.redisson.client.codec.Codec)1 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)1