Skip to content

Commit

Permalink
DATACMNS-422 - Mitigate semantic changes in Spring 4.0's GenericTypeR…
Browse files Browse the repository at this point in the history
…esolver.

In case of an unresolvable generic parameter in TypeDiscoverer's getTypeArgument(…) we check for a generic super type for the given bound and fall back to Object in case we find a parameterized type.
  • Loading branch information
odrotbohm committed Jan 19, 2014
1 parent debf5c7 commit 5f8516f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/main/java/org/springframework/data/util/TypeDiscoverer.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -309,7 +309,7 @@ public boolean isMap() {
public TypeInformation<?> getMapValueType() {

if (isMap()) {
return getTypeArgument(getType(), Map.class, 1);
return getTypeArgument(Map.class, 1);
}

List<TypeInformation<?>> arguments = getTypeArguments();
Expand Down Expand Up @@ -349,11 +349,11 @@ public TypeInformation<?> getComponentType() {
}

if (isMap()) {
return getTypeArgument(rawType, Map.class, 0);
return getTypeArgument(Map.class, 0);
}

if (Iterable.class.isAssignableFrom(rawType)) {
return getTypeArgument(rawType, Iterable.class, 0);
return getTypeArgument(Iterable.class, 0);
}

List<TypeInformation<?>> arguments = getTypeArguments();
Expand Down Expand Up @@ -449,9 +449,16 @@ public boolean isAssignableFrom(TypeInformation<?> target) {
return target.getSuperTypeInformation(getType()).equals(this);
}

private TypeInformation<?> getTypeArgument(Class<?> type, Class<?> bound, int index) {
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(type, bound);
return arguments == null ? null : createInfo(arguments[index]);
private TypeInformation<?> getTypeArgument(Class<?> bound, int index) {

Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(getType(), bound);

if (arguments == null) {
return getSuperTypeInformation(bound) instanceof ParameterizedTypeInformation ? ClassTypeInformation.OBJECT
: null;
}

return createInfo(arguments[index]);
}

/*
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -285,6 +285,16 @@ public void rejectsNullClass() {
from(null);
}

/**
* @see DATACMNS-422
*/
@Test
public void returnsNullForRawTypesOnly() {

assertThat(from(MyRawIterable.class).getComponentType(), is(nullValue()));
assertThat(from(MyIterable.class).getComponentType(), is(notNullValue()));
}

static class StringMapContainer extends MapContainer<String> {

}
Expand Down Expand Up @@ -409,4 +419,9 @@ interface Category extends Identifiable {
interface Identifiable {
Long getId();
}

@SuppressWarnings("rawtypes")
interface MyRawIterable extends Iterable {}

interface MyIterable<T> extends Iterable<T> {}
}

0 comments on commit 5f8516f

Please sign in to comment.