Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to support multiple generics class parameters #992

Merged
merged 2 commits into from
Aug 12, 2015
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 @@ -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