Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static <T> Optional<Field> findField(Class<T> type, String name, Map<Str
String mapping = fieldNameMapping.get(name);
String fieldName = mapping == null ? name : mapping;

Optional<Field> field = Stream.of(type.getDeclaredFields())
Optional<Field> field = getAllFields(type)
.filter(f -> f.isAnnotationPresent(Parameter.class))
.filter(f -> fieldName.equals(f.getAnnotation(Parameter.class).name()))
.findFirst();
Expand All @@ -137,6 +137,16 @@ private static Optional<Field> findField(Class<?> type, String fieldName) {
return Optional.empty();
}

private static Stream<Field> getAllFields(Class<?> type) {
if (type == Object.class) {
return Stream.empty();
}
return Stream.concat(
Stream.of(type.getDeclaredFields()),
getAllFields(type.getSuperclass())
);
}

@SuppressWarnings("unchecked")
private <T> T convert(String value, Type type) {
return (T) parameterConverters.convert(value, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void shouldConvertParametersToTypeWithFieldMapping() {
row.put("firstName", "Boba");
row.put("l_name", "Fett");
row.put("identifier", "boba-fett-clone");
row.put("idx", "1");

Parameters parameters = new ConvertedParameters(row, converters);

Expand All @@ -100,6 +101,7 @@ void shouldConvertParametersToTypeWithFieldMapping() {
assertThat(person.getAge(), is(53));
assertThat(person.getFirstName(), is("Boba"));
assertThat(person.getLastName(), is("Fett"));
assertThat(person.getIndex(), is(1));
}

@Test
Expand All @@ -119,6 +121,7 @@ void shouldConvertParametersToPersonType() {
row.put("years", "38");
row.put("firstName", "Din");
row.put("l_name", "Djarin");
row.put("idx", "1");

converters.addConverters(new ParametersToPersonConverter());
Parameters parameters = new ConvertedParameters(row, converters);
Expand All @@ -128,16 +131,26 @@ void shouldConvertParametersToPersonType() {
assertThat(person.getAge(), is(38));
assertThat(person.getFirstName(), is("Din"));
assertThat(person.getLastName(), is("Djarin"));
assertThat(person.getIndex(), is(1));
}

public static class Identifier {

private String identifier;
@Parameter(name = "idx")
private int index;

public String getIdentifier() {
return identifier;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}
}

public static final class ParametersToPersonConverter extends AbstractParameterConverter<Parameters, Person> {
Expand All @@ -148,6 +161,7 @@ public Person convertValue(Parameters value, Type type) {
person.setAge(value.valueAs("years", int.class));
person.setFirstName(value.valueAs("firstName", String.class));
person.setLastName(value.valueAs("l_name", String.class));
person.setIndex(value.valueAs("idx", Integer.class));
return person;
}

Expand Down
Loading