Search in sources :

Example 1 with TypeInformation

use of io.lettuce.core.dynamic.support.TypeInformation in project lettuce-core by lettuce-io.

the class OutputRegistry method getOutputComponentType.

/**
 * Retrieve {@link OutputType} for a {@link CommandOutput} type.
 *
 * @param commandOutputClass
 * @return
 */
static OutputType getOutputComponentType(Class<? extends CommandOutput> commandOutputClass) {
    ClassTypeInformation<? extends CommandOutput> classTypeInformation = ClassTypeInformation.from(commandOutputClass);
    TypeInformation<?> superTypeInformation = classTypeInformation.getSuperTypeInformation(CommandOutput.class);
    if (superTypeInformation == null) {
        return null;
    }
    List<TypeInformation<?>> typeArguments = superTypeInformation.getTypeArguments();
    return new OutputType(commandOutputClass, typeArguments.get(2), false) {

        @Override
        public ResolvableType withCodec(RedisCodec<?, ?> codec) {
            TypeInformation<?> typeInformation = ClassTypeInformation.from(codec.getClass());
            ResolvableType resolvableType = ResolvableType.forType(commandOutputClass, new CodecVariableTypeResolver(typeInformation));
            while (!resolvableType.getRawClass().equals(CommandOutput.class)) {
                resolvableType = resolvableType.getSuperType();
            }
            return resolvableType.getGeneric(2);
        }
    };
}
Also used : RedisCodec(io.lettuce.core.codec.RedisCodec) ResolvableType(io.lettuce.core.dynamic.support.ResolvableType) ClassTypeInformation(io.lettuce.core.dynamic.support.ClassTypeInformation) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation)

Example 2 with TypeInformation

use of io.lettuce.core.dynamic.support.TypeInformation in project lettuce-core by lettuce-io.

the class ParameterWrappersUnitTests method shouldReturnValueTypeForList.

@Test
void shouldReturnValueTypeForList() {
    Method method = ReflectionUtils.findMethod(CommandMethods.class, "withList", List.class);
    TypeInformation typeInformation = new Parameter(method, 0).getTypeInformation();
    assertThat(ParameterWrappers.hasKeyType(typeInformation)).isFalse();
    assertThat(ParameterWrappers.supports(typeInformation)).isTrue();
    assertThat(ParameterWrappers.getValueType(typeInformation).getType()).isEqualTo(String.class);
}
Also used : Parameter(io.lettuce.core.dynamic.parameter.Parameter) Method(java.lang.reflect.Method) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation) Test(org.junit.jupiter.api.Test)

Example 3 with TypeInformation

use of io.lettuce.core.dynamic.support.TypeInformation in project lettuce-core by lettuce-io.

the class ParameterWrappersUnitTests method shouldReturnValueTypeForKeyValue.

@Test
void shouldReturnValueTypeForKeyValue() {
    Method method = ReflectionUtils.findMethod(CommandMethods.class, "keyValue", KeyValue.class);
    TypeInformation typeInformation = new Parameter(method, 0).getTypeInformation();
    assertThat(ParameterWrappers.hasKeyType(typeInformation)).isTrue();
    assertThat(ParameterWrappers.getKeyType(typeInformation).getType()).isEqualTo(Integer.class);
    assertThat(ParameterWrappers.hasValueType(typeInformation)).isTrue();
    assertThat(ParameterWrappers.getValueType(typeInformation).getType()).isEqualTo(String.class);
}
Also used : Parameter(io.lettuce.core.dynamic.parameter.Parameter) Method(java.lang.reflect.Method) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation) Test(org.junit.jupiter.api.Test)

Example 4 with TypeInformation

use of io.lettuce.core.dynamic.support.TypeInformation in project lettuce-core by lettuce-io.

the class OutputRegistry method getStreamingType.

/**
 * Retrieve {@link OutputType} for a {@link StreamingOutput} type.
 *
 * @param commandOutputClass
 * @return
 */
@SuppressWarnings("rawtypes")
static OutputType getStreamingType(Class<? extends CommandOutput> commandOutputClass) {
    ClassTypeInformation<? extends CommandOutput> classTypeInformation = ClassTypeInformation.from(commandOutputClass);
    TypeInformation<?> superTypeInformation = classTypeInformation.getSuperTypeInformation(StreamingOutput.class);
    if (superTypeInformation == null) {
        return null;
    }
    List<TypeInformation<?>> typeArguments = superTypeInformation.getTypeArguments();
    return new OutputType(commandOutputClass, typeArguments.get(0), true) {

        @Override
        public ResolvableType withCodec(RedisCodec<?, ?> codec) {
            TypeInformation<?> typeInformation = ClassTypeInformation.from(codec.getClass());
            ResolvableType resolvableType = ResolvableType.forType(commandOutputClass, new CodecVariableTypeResolver(typeInformation));
            while (resolvableType != ResolvableType.NONE) {
                ResolvableType[] interfaces = resolvableType.getInterfaces();
                for (ResolvableType resolvableInterface : interfaces) {
                    if (resolvableInterface.getRawClass().equals(StreamingOutput.class)) {
                        return resolvableInterface.getGeneric(0);
                    }
                }
                resolvableType = resolvableType.getSuperType();
            }
            throw new IllegalStateException();
        }
    };
}
Also used : RedisCodec(io.lettuce.core.codec.RedisCodec) ResolvableType(io.lettuce.core.dynamic.support.ResolvableType) ClassTypeInformation(io.lettuce.core.dynamic.support.ClassTypeInformation) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation)

Example 5 with TypeInformation

use of io.lettuce.core.dynamic.support.TypeInformation in project lettuce-core by lettuce-io.

the class ParameterWrappersUnitTests method shouldReturnValueTypeForMap.

@Test
void shouldReturnValueTypeForMap() {
    Method method = ReflectionUtils.findMethod(CommandMethods.class, "withMap", Map.class);
    TypeInformation typeInformation = new Parameter(method, 0).getTypeInformation();
    assertThat(ParameterWrappers.hasKeyType(typeInformation)).isTrue();
    assertThat(ParameterWrappers.getKeyType(typeInformation).getType()).isEqualTo(Integer.class);
    assertThat(ParameterWrappers.hasValueType(typeInformation)).isTrue();
    assertThat(ParameterWrappers.getValueType(typeInformation).getType()).isEqualTo(String.class);
}
Also used : Parameter(io.lettuce.core.dynamic.parameter.Parameter) Method(java.lang.reflect.Method) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation) Test(org.junit.jupiter.api.Test)

Aggregations

TypeInformation (io.lettuce.core.dynamic.support.TypeInformation)9 Parameter (io.lettuce.core.dynamic.parameter.Parameter)7 Method (java.lang.reflect.Method)7 Test (org.junit.jupiter.api.Test)7 RedisCodec (io.lettuce.core.codec.RedisCodec)2 ClassTypeInformation (io.lettuce.core.dynamic.support.ClassTypeInformation)2 ResolvableType (io.lettuce.core.dynamic.support.ResolvableType)2