Skip to content

Instantly share code, notes, and snippets.

@pholser
Created December 15, 2015 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pholser/74891e1c1b966c288b13 to your computer and use it in GitHub Desktop.
Save pholser/74891e1c1b966c288b13 to your computer and use it in GitHub Desktop.
Possibly a bug in generics resolution library?
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import ru.vyarus.java.generics.resolver.GenericsResolver;
import static org.junit.Assert.*;
public class ResolvingGenericParameterTypesTest {
public static class Pair<F, S> {
public final F first;
public final S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
}
public static class PairProperties<F, S> {
public void firstAndSecond(F first, S second) {
}
}
public static class SelfPairProperties<F, S extends F> extends PairProperties<F, S> {
}
public static class IntegerPairProperties extends SelfPairProperties<Integer, Integer> {
}
@Test public void unresolvedPair() throws Exception {
Map<String, Type> expected = new HashMap<>();
expected.put("F", Object.class);
expected.put("S", Object.class);
Map<String, Type> actual =
GenericsResolver.resolve(PairProperties.class)
.method(firstAndSecond(PairProperties.class))
.genericsMap();
assertEquals(expected, actual);
}
@Test public void selfPair() throws Exception {
Map<String, Type> expected = new HashMap<>();
expected.put("F", Object.class);
expected.put("S", Object.class);
Map<String, Type> actual =
GenericsResolver.resolve(SelfPairProperties.class)
.method(firstAndSecond(SelfPairProperties.class))
.genericsMap();
assertEquals(expected, actual);
}
@Test public void integerPair() throws Exception {
Map<String, Type> expected = new HashMap<>();
expected.put("F", Integer.class);
expected.put("S", Integer.class);
Map<String, Type> actual =
GenericsResolver.resolve(IntegerPairProperties.class)
.method(firstAndSecond(IntegerPairProperties.class))
.genericsMap();
assertEquals(expected, actual);
}
private Method firstAndSecond(Class<?> target) throws Exception {
return target.getMethod("firstAndSecond", Object.class, Object.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment