Search in sources :

Example 1 with InDefinedShape

use of net.bytebuddy.description.field.FieldDescription.InDefinedShape in project redisson by redisson.

the class RedissonLiveObjectService method delete.

@Override
public <T> long delete(Class<T> entityClass, Object... ids) {
    CommandBatchService ce = new CommandBatchService(commandExecutor);
    FieldList<InDefinedShape> fields = Introspectior.getFieldsWithAnnotation(entityClass.getSuperclass(), RIndex.class);
    Set<String> fieldNames = fields.stream().map(f -> f.getName()).collect(Collectors.toSet());
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    for (Object id : ids) {
        delete(id, entityClass, namingScheme, ce, fieldNames);
    }
    BatchResult<Object> r = (BatchResult<Object>) ce.execute();
    return r.getResponses().stream().filter(s -> s instanceof Long).mapToLong(s -> (Long) s).sum();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) org.redisson.api.annotation(org.redisson.api.annotation) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) ClassUtils(org.redisson.liveobject.misc.ClassUtils) CommandBatchService(org.redisson.command.CommandBatchService) RIdResolver(org.redisson.liveobject.resolver.RIdResolver) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) ObjectListReplayDecoder(org.redisson.client.protocol.decoder.ObjectListReplayDecoder) FieldDescription(net.bytebuddy.description.field.FieldDescription) BeanCopy(jodd.bean.BeanCopy) Condition(org.redisson.api.condition.Condition) Constructor(java.lang.reflect.Constructor) ConcurrentMap(java.util.concurrent.ConcurrentMap) ListScanResultReplayDecoder(org.redisson.client.protocol.decoder.ListScanResultReplayDecoder) Introspectior(org.redisson.liveobject.misc.Introspectior) org.redisson.api(org.redisson.api) AdvBeanCopy(org.redisson.liveobject.misc.AdvBeanCopy) org.redisson.liveobject.core(org.redisson.liveobject.core) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LiveObjectTemplate(org.redisson.liveobject.LiveObjectTemplate) Convertor(org.redisson.client.protocol.convertor.Convertor) LiveObjectSearch(org.redisson.liveobject.LiveObjectSearch) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) Field(java.lang.reflect.Field) ClassIntrospector(jodd.introspector.ClassIntrospector) Collectors(java.util.stream.Collectors) ElementMatchers(net.bytebuddy.matcher.ElementMatchers) FieldList(net.bytebuddy.description.field.FieldList) Entry(java.util.Map.Entry) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) FieldProxy(net.bytebuddy.implementation.bind.annotation.FieldProxy) RedisCommand(org.redisson.client.protocol.RedisCommand) ListMultiDecoder2(org.redisson.client.protocol.decoder.ListMultiDecoder2) BeanUtil(jodd.bean.BeanUtil) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape)

Example 2 with InDefinedShape

use of net.bytebuddy.description.field.FieldDescription.InDefinedShape in project redisson by redisson.

the class RedissonLiveObjectService method validateClass.

private <T> void validateClass(Class<T> entityClass) {
    if (entityClass.isAnonymousClass() || entityClass.isLocalClass()) {
        throw new IllegalArgumentException(entityClass.getName() + " is not publically accessable.");
    }
    if (!ClassUtils.isAnnotationPresent(entityClass, REntity.class)) {
        throw new IllegalArgumentException("REntity annotation is missing from class type declaration.");
    }
    FieldList<FieldDescription.InDefinedShape> fields = Introspectior.getFieldsWithAnnotation(entityClass, RIndex.class);
    fields = fields.filter(ElementMatchers.fieldType(ElementMatchers.hasSuperType(ElementMatchers.anyOf(Map.class, Collection.class, RObject.class))));
    for (InDefinedShape field : fields) {
        throw new IllegalArgumentException("RIndex annotation couldn't be defined for field '" + field.getName() + "' with type '" + field.getType() + "'");
    }
    FieldList<FieldDescription.InDefinedShape> fieldsWithRIdAnnotation = Introspectior.getFieldsWithAnnotation(entityClass, RId.class);
    if (fieldsWithRIdAnnotation.size() == 0) {
        throw new IllegalArgumentException("RId annotation is missing from class field declaration.");
    }
    if (fieldsWithRIdAnnotation.size() > 1) {
        throw new IllegalArgumentException("Only one field with RId annotation is allowed in class field declaration.");
    }
    FieldDescription.InDefinedShape idFieldDescription = fieldsWithRIdAnnotation.getOnly();
    String idFieldName = idFieldDescription.getName();
    Field idField = null;
    try {
        idField = ClassUtils.getDeclaredField(entityClass, idFieldName);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    if (ClassUtils.isAnnotationPresent(idField.getType(), REntity.class)) {
        throw new IllegalArgumentException("Field with RId annotation cannot be a type of which class is annotated with REntity.");
    }
    if (idField.getType().isAssignableFrom(RObject.class)) {
        throw new IllegalArgumentException("Field with RId annotation cannot be a type of RObject");
    }
}
Also used : Field(java.lang.reflect.Field) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape) FieldDescription(net.bytebuddy.description.field.FieldDescription)

Aggregations

Field (java.lang.reflect.Field)2 FieldDescription (net.bytebuddy.description.field.FieldDescription)2 InDefinedShape (net.bytebuddy.description.field.FieldDescription.InDefinedShape)2 Constructor (java.lang.reflect.Constructor)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Collectors (java.util.stream.Collectors)1 BeanCopy (jodd.bean.BeanCopy)1 BeanUtil (jodd.bean.BeanUtil)1 ClassIntrospector (jodd.introspector.ClassIntrospector)1 ByteBuddy (net.bytebuddy.ByteBuddy)1 FieldList (net.bytebuddy.description.field.FieldList)1 DynamicType (net.bytebuddy.dynamic.DynamicType)1 ClassLoadingStrategy (net.bytebuddy.dynamic.loading.ClassLoadingStrategy)1 MethodDelegation (net.bytebuddy.implementation.MethodDelegation)1 FieldProxy (net.bytebuddy.implementation.bind.annotation.FieldProxy)1 ElementMatchers (net.bytebuddy.matcher.ElementMatchers)1 org.redisson.api (org.redisson.api)1