Skip to content

Commit

Permalink
code review pass 1
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenschlansker committed Jan 3, 2019
1 parent 3c49992 commit ad8f789
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
*/
package org.jdbi.v3.core.argument;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import org.jdbi.v3.core.argument.internal.ObjectPropertyNamedArgumentFinder;
import org.jdbi.v3.core.argument.internal.TypedValue;
import org.jdbi.v3.core.qualifier.QualifiedType;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.UnableToCreateStatementException;

Expand Down Expand Up @@ -65,11 +64,11 @@ protected Optional<TypedValue> getValue(String name, StatementContext ctx) {
}

try {
Type type = field.getGenericType();
Set<Annotation> qualifiers = getQualifiers(field);
QualifiedType type = QualifiedType.of(field.getGenericType())
.with(getQualifiers(field));
Object value = field.get(obj);

return Optional.of(new TypedValue(type, qualifiers, value));
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]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@
*/
package org.jdbi.v3.core.argument;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import org.jdbi.v3.core.argument.internal.MethodReturnValueNamedArgumentFinder;
import org.jdbi.v3.core.argument.internal.TypedValue;
import org.jdbi.v3.core.qualifier.QualifiedType;
import org.jdbi.v3.core.statement.StatementContext;

import static org.jdbi.v3.core.qualifier.Qualifiers.getQualifiers;
Expand Down Expand Up @@ -76,11 +75,11 @@ protected Optional<TypedValue> getValue(String name, StatementContext ctx) {
return Optional.empty();
}

Type type = method.getGenericReturnType();
Set<Annotation> qualifiers = getQualifiers(method);
QualifiedType type = QualifiedType.of(method.getGenericReturnType())
.with(getQualifiers(method));
Object value = invokeMethod(method, ctx);

return Optional.of(new TypedValue(type, qualifiers, value));
return Optional.of(new TypedValue(type, value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@
*/
package org.jdbi.v3.core.argument.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;
import org.jdbi.v3.core.qualifier.QualifiedType;

public class TypedValue {
final QualifiedType type;
final Object value;

public TypedValue(Type type, Set<Annotation> qualifiers, Object value) {
this.type = QualifiedType.of(type).with(qualifiers);
this.value = value;
}

public TypedValue(QualifiedType qualifiedType, Object value) {
this.type = qualifiedType;
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* The mapped class must have a default constructor.
*/
public class BeanMapper<T> implements RowMapper<T> {
protected static final String DEFAULT_PREFIX = "";
static final String DEFAULT_PREFIX = "";

private static final String NO_MATCHING_COLUMNS =
"Mapping bean type %s didn't find any matching columns in result set";
Expand Down Expand Up @@ -102,17 +102,17 @@ public static <T> RowMapper<T> of(Class<T> type, String prefix) {

protected final Class<T> type;
protected final String prefix;
private final PojoProperties<T> beanInfo;
private final PojoProperties<T> properties;
private final Map<PojoProperty<T>, BeanMapper<?>> nestedMappers = new ConcurrentHashMap<>();

@SuppressWarnings("unchecked")
protected BeanMapper(Class<T> type, String prefix) {
BeanMapper(Class<T> type, String prefix) {
this(type, (PojoProperties<T>) BeanPropertiesFactory.propertiesFor(type), prefix);
}

BeanMapper(Class<T> type, PojoProperties<T> properties, String prefix) {
this.type = type;
this.beanInfo = properties;
this.properties = properties;
this.prefix = prefix.toLowerCase();
}

Expand Down Expand Up @@ -148,7 +148,7 @@ private Optional<RowMapper<T>> specialize0(StatementContext ctx,
final List<RowMapper<?>> mappers = new ArrayList<>();
final List<PojoProperty<T>> propList = new ArrayList<>();

for (PojoProperty<T> property : beanInfo.getProperties().values()) {
for (PojoProperty<T> property : properties.getProperties().values()) {
Nested anno = property.getAnnotation(Nested.class).orElse(null);

if (anno == null) {
Expand Down Expand Up @@ -183,7 +183,7 @@ private Optional<RowMapper<T>> specialize0(StatementContext ctx,
}

return Optional.of((r, c) -> {
final PojoBuilder<T> pojo = beanInfo.create();
final PojoBuilder<T> pojo = properties.create();

for (int i = 0; i < mappers.size(); i++) {
RowMapper<?> mapper = mappers.get(i);
Expand All @@ -198,13 +198,13 @@ private Optional<RowMapper<T>> specialize0(StatementContext ctx,
});
}

protected ColumnMapper<?> defaultColumnMapper(PojoProperty<T> property) {
ColumnMapper<?> defaultColumnMapper(PojoProperty<T> property) {
return (r, n, c) -> r.getObject(n);
}

@Beta
public PojoProperties<T> getBeanInfo() {
return beanInfo;
return properties;
}

private String getName(PojoProperty<T> property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ImmutablesMapperFactory<T> implements RowMapperFactory {
private final Class<? extends T> impl;
private final Function<Type, PojoProperties<T>> properties;

public ImmutablesMapperFactory(Class<T> defn, Class<? extends T> impl, Function<Type, PojoProperties<T>> properties) {
private ImmutablesMapperFactory(Class<T> defn, Class<? extends T> impl, Function<Type, PojoProperties<T>> properties) {
this.defn = defn;
this.impl = impl;
this.properties = properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private boolean isProperty(Method m) {
return properties;
}

protected abstract ImmutablesPojoProperty<T> createProperty(String name, Method m);
abstract ImmutablesPojoProperty<T> createProperty(String name, Method m);
}

static class ImmutablePojoProperties<T, B> extends BasePojoProperties<T, B> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.jdbi.v3.core.argument.internal.ObjectPropertyNamedArgumentFinder
import org.jdbi.v3.core.argument.internal.TypedValue
import org.jdbi.v3.core.kotlin.getQualifiers
import org.jdbi.v3.core.statement.StatementContext
import org.jdbi.v3.core.qualifier.QualifiedType
import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty1
Expand All @@ -37,16 +38,15 @@ class KotlinPropertyArguments(obj: Any,
override fun getValue(name: String, ctx: StatementContext): Optional<TypedValue> {
val property: KProperty1<*, *> = properties[name] ?: return Optional.empty()
val mutableProperty = property as? KMutableProperty1
val type = property.returnType.javaType
val qualifiers = getQualifiers(
kClass.primaryConstructor?.parameters?.find { it.name == name },
property,
property.getter,
mutableProperty?.setter,
mutableProperty?.setter?.parameters?.getOrNull(0)
)
val type = QualifiedType.of(property.returnType.javaType)
.with(getQualifiers(
kClass.primaryConstructor?.parameters?.find { it.name == name },
property,
property.getter,
mutableProperty?.setter,
mutableProperty?.setter?.parameters?.getOrNull(0)))
val value = property.getter.call(obj)
return Optional.of(TypedValue(type, qualifiers, value))
return Optional.of(TypedValue(type, value))
}

override fun getNestedArgumentFinder(obj: Any): NamedArgumentFinder {
Expand Down
2 changes: 1 addition & 1 deletion postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
Expand Down

0 comments on commit ad8f789

Please sign in to comment.