Search in sources :

Example 1 with ColumnName

use of org.jdbi.v3.core.mapper.reflect.ColumnName in project jdbi by jdbi.

the class PojoBuilderUtils method findBuilderSetter.

public static MethodHandle findBuilderSetter(final Class<?> builderClass, String name, Method decl, Type type) throws IllegalAccessException {
    final List<NoSuchMethodException> failures = new ArrayList<>();
    final Set<String> names = new LinkedHashSet<>();
    names.add(PojoBuilderUtils.defaultSetterName(decl.getName()));
    names.add(name);
    if (name.length() > 1) {
        names.addAll(setterNames(name));
    }
    ColumnName columnName = decl.getAnnotation(ColumnName.class);
    if (columnName != null && columnName.value().equals(name)) {
        names.addAll(setterNames(PojoBuilderUtils.defaultSetterName(decl.getName())));
    }
    for (String tryName : names) {
        try {
            return MethodHandles.lookup().unreflect(builderClass.getMethod(tryName, GenericTypes.getErasedType(type)));
        } catch (NoSuchMethodException e) {
            failures.add(e);
        }
    }
    for (Method m : builderClass.getMethods()) {
        if (names.contains(m.getName()) && m.getParameterCount() == 1) {
            return MethodHandles.lookup().unreflect(m);
        }
    }
    final IllegalArgumentException iae = new IllegalArgumentException("Failed to find builder setter for property " + name + " on " + builderClass);
    failures.forEach(iae::addSuppressed);
    return MethodHandles.dropArguments(MethodHandles.throwException(Object.class, IllegalArgumentException.class), 1, Arrays.asList(Object.class, Object.class)).bindTo(iae);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnName(org.jdbi.v3.core.mapper.reflect.ColumnName) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 2 with ColumnName

use of org.jdbi.v3.core.mapper.reflect.ColumnName in project jdbi by jdbi.

the class ConstructorMapper method specialize0.

private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
    final int count = constructor.getParameterCount();
    final Parameter[] parameters = constructor.getParameters();
    final RowMapper<?>[] mappers = new RowMapper<?>[count];
    for (int i = 0; i < count; i++) {
        final Parameter parameter = parameters[i];
        Nested anno = parameter.getAnnotation(Nested.class);
        if (anno == null) {
            final String paramName = prefix + paramName(parameters, i, constructorProperties);
            final int columnIndex = findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(parameter)).orElseThrow(() -> new IllegalArgumentException(String.format("Constructor '%s' parameter '%s' has no column in the result set. " + "Verify that the Java compiler is configured to emit parameter names, " + "that your result set has the columns expected, or annotate the " + "parameter names explicitly with @ColumnName", constructor, paramName)));
            final Type type = parameter.getParameterizedType();
            mappers[i] = ctx.findColumnMapperFor(type).map(mapper -> new SingleColumnMapper(mapper, columnIndex + 1)).orElseThrow(() -> new IllegalArgumentException(String.format("Could not find column mapper for type '%s' of parameter '%s' for constructor '%s'", type, paramName, constructor)));
            unmatchedColumns.remove(columnNames.get(columnIndex));
        } else {
            String nestedPrefix = prefix + anno.value();
            mappers[i] = nestedMappers.computeIfAbsent(parameter, p -> new ConstructorMapper<>(findConstructorFor(p.getType()), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
        }
    }
    return (r, c) -> {
        final Object[] params = new Object[count];
        for (int i = 0; i < count; i++) {
            params[i] = mappers[i].map(r, c);
        }
        return construct(params);
    };
}
Also used : ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) RowMapperFactory(org.jdbi.v3.core.mapper.RowMapperFactory) ConstructorProperties(java.beans.ConstructorProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Nested(org.jdbi.v3.core.mapper.Nested) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) Constructor(java.lang.reflect.Constructor) JdbiConstructors.findConstructorFor(org.jdbi.v3.core.mapper.reflect.JdbiConstructors.findConstructorFor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) StatementContext(org.jdbi.v3.core.statement.StatementContext) SQLException(java.sql.SQLException) List(java.util.List) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) ResultSet(java.sql.ResultSet) Map(java.util.Map) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Type(java.lang.reflect.Type) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) Nested(org.jdbi.v3.core.mapper.Nested) Parameter(java.lang.reflect.Parameter) RowMapper(org.jdbi.v3.core.mapper.RowMapper)

Aggregations

ArrayList (java.util.ArrayList)2 ConstructorProperties (java.beans.ConstructorProperties)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Nested (org.jdbi.v3.core.mapper.Nested)1 RowMapper (org.jdbi.v3.core.mapper.RowMapper)1 RowMapperFactory (org.jdbi.v3.core.mapper.RowMapperFactory)1 SingleColumnMapper (org.jdbi.v3.core.mapper.SingleColumnMapper)1 ColumnName (org.jdbi.v3.core.mapper.reflect.ColumnName)1 JdbiConstructors.findConstructorFor (org.jdbi.v3.core.mapper.reflect.JdbiConstructors.findConstructorFor)1 ReflectionMapperUtil.findColumnIndex (org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex)1