Created
June 28, 2016 00:35
-
-
Save smilingleo/7966a9c811053408f2aabab9feab6e83 to your computer and use it in GitHub Desktop.
a way to get actual parameter type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testJavaGeneric() { | |
// have to use a empty `{ }`, so that the `getClass().getGenericSuperclass()` will return a `ParameterizedType` instance. | |
List<Integer> list = new LinkedList<Integer>() { | |
}; | |
// superclass contains the unerased info. | |
Type supers = list.getClass().getGenericSuperclass(); | |
assertTrue(supers instanceof ParameterizedType); | |
ParameterizedType paramType = (ParameterizedType) supers; | |
Type[] actualTypes = paramType.getActualTypeArguments(); | |
assertEquals(1, actualTypes.length); | |
assertEquals(Integer.class, actualTypes[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment