Skip to content

Commit

Permalink
Merge pull request #992 from francofabio/master
Browse files Browse the repository at this point in the history
Improvement to support multiple generics class parameters
  • Loading branch information
Turini committed Aug 12, 2015
2 parents f22958f + b419f32 commit f4c7537
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,28 @@ private List<Target<Object>> createTargets(ControllerMethod method) {
for (Parameter p : nameProvider.parametersFor(javaMethod)) {
Type type = p.getParameterizedType();
if (type instanceof TypeVariable) {
type = extractType(method);
type = extractType(method, (TypeVariable<?>) type);
}

targets.add(new Target<>(type, p.getName()));
}

return targets;
}

private Type extractType(ControllerMethod method) {
ParameterizedType superclass = (ParameterizedType) method.getController().getType().getGenericSuperclass();
return superclass.getActualTypeArguments()[0];

private Type extractType(ControllerMethod method, TypeVariable<?> paramType) {
ParameterizedType parameterizedType = (ParameterizedType) method.getController().getType().getGenericSuperclass();
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
if (typeParameters.length > 0) {
for (int i = 0; i < typeParameters.length; i++) {
TypeVariable<?> typeVariable = typeParameters[i];
if (typeVariable.getName().equals(paramType.getName())) {
return (Class<?>) parameterizedType.getActualTypeArguments()[i];
}
}
}
return paramType;
}

private Parameters parseParameters(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ public void isCapableOfDealingWithGenerics() throws Exception {
assertThat(abc.x, is(123l));
}

@Test
public void isCapableOfDealingWithGenericsAndMultipleParameters() throws Exception {
requestParametersAre(ImmutableMap.of("key", new String[] { "age" }, "value", new String[] { "32" }));

ControllerMethod generic = method(SpecificKeyValueResource.class, GenericKeyValueResource.class, "put", Object.class, Object.class);
Object[] params = iogi.getParametersFor(generic, errors);
String key = (String) params[0];
Long value = (Long) params[1];

assertThat(key, is("age"));
assertThat(value, notNullValue());
assertThat(value, instanceOf(Long.class));
assertThat(value, is(32L));
}

@Test
public void isCapableOfDealingWithIndexedLists() throws Exception {
requestParameterIs("abc[2]", "1");
Expand Down Expand Up @@ -602,6 +617,14 @@ void generic(T t) { }

static class Specific extends Generic<ABC> {
}

static class GenericKeyValueResource<K, V> {
void put(K key, V value) { }
}

static class SpecificKeyValueResource extends GenericKeyValueResource<String, Long> {

}

public static class Cat {
private String id;
Expand Down

0 comments on commit f4c7537

Please sign in to comment.