Search in sources :

Example 1 with TypedValue

use of org.jdbi.v3.core.argument.internal.TypedValue in project jdbi by jdbi.

the class ObjectMethodArguments method load.

private static Map<String, Function<Object, TypedValue>> load(ConfigRegistry config, Class<?> type) {
    final HashMap<String, Function<Object, TypedValue>> methodMap = new HashMap<>();
    if (Modifier.isPublic(type.getModifiers())) {
        Arrays.stream(type.getMethods()).filter(m -> m.getParameterCount() == 0).collect(Collectors.toMap(Method::getName, Function.identity(), ObjectMethodArguments::bridgeMethodMerge)).forEach((name, method) -> {
            QualifiedType<?> qualifiedType = QualifiedType.of(method.getGenericReturnType()).withAnnotations(config.get(Qualifiers.class).findFor(method));
            MethodHandle mh = Unchecked.function(MethodHandles.lookup()::unreflect).apply(method);
            methodMap.put(name, Unchecked.function(value -> new TypedValue(qualifiedType, mh.invoke(value))));
        });
    } else {
        Optional.ofNullable(type.getSuperclass()).ifPresent(superclass -> methodMap.putAll(load(config, superclass)));
        Arrays.stream(type.getInterfaces()).forEach(interfaceClass -> methodMap.putAll(load(config, interfaceClass)));
    }
    return methodMap;
}
Also used : ObjectPropertyNamedArgumentFinder(org.jdbi.v3.core.argument.internal.ObjectPropertyNamedArgumentFinder) MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) JdbiCaches(org.jdbi.v3.core.config.JdbiCaches) MethodHandles(java.lang.invoke.MethodHandles) HashMap(java.util.HashMap) JdbiCache(org.jdbi.v3.core.config.JdbiCache) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) StatementContext(org.jdbi.v3.core.statement.StatementContext) Unchecked(org.jdbi.v3.core.internal.exceptions.Unchecked) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Optional(java.util.Optional) Qualifiers(org.jdbi.v3.core.qualifier.Qualifiers) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Method(java.lang.reflect.Method) TypedValue(org.jdbi.v3.core.argument.internal.TypedValue) Function(java.util.function.Function) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle) TypedValue(org.jdbi.v3.core.argument.internal.TypedValue)

Example 2 with TypedValue

use of org.jdbi.v3.core.argument.internal.TypedValue in project jdbi by jdbi.

the class ObjectFieldArguments method getValue.

@Override
Optional<TypedValue> getValue(String name, StatementContext ctx) {
    Field field = fields.get(name);
    if (field == null) {
        return Optional.empty();
    }
    try {
        Type type = field.getGenericType();
        Object value = field.get(object);
        return Optional.of(new TypedValue(type, value));
    } catch (IllegalAccessException e) {
        throw new UnableToCreateStatementException(String.format("Access exception getting field for " + "bean property [%s] on [%s]", name, object), e, ctx);
    }
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) UnableToCreateStatementException(org.jdbi.v3.core.statement.UnableToCreateStatementException)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)1 MethodHandles (java.lang.invoke.MethodHandles)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ObjectPropertyNamedArgumentFinder (org.jdbi.v3.core.argument.internal.ObjectPropertyNamedArgumentFinder)1 TypedValue (org.jdbi.v3.core.argument.internal.TypedValue)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 JdbiCache (org.jdbi.v3.core.config.JdbiCache)1 JdbiCaches (org.jdbi.v3.core.config.JdbiCaches)1 Unchecked (org.jdbi.v3.core.internal.exceptions.Unchecked)1 QualifiedType (org.jdbi.v3.core.qualifier.QualifiedType)1 Qualifiers (org.jdbi.v3.core.qualifier.Qualifiers)1