Created
May 23, 2011 16:38
-
-
Save webdevwilson/987011 to your computer and use it in GitHub Desktop.
Generic type erasure tests
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
package com.goodercode; | |
import static org.hamcrest.CoreMatchers.equalTo; | |
import static org.hamcrest.CoreMatchers.not; | |
import static org.junit.Assert.assertThat; | |
import java.lang.reflect.ParameterizedType; | |
import java.lang.reflect.TypeVariable; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class GenericTypeTest { | |
private MyGenericClass instance; | |
@Before | |
public void setup() { | |
instance = new MyGenericClass(); | |
} | |
@Test | |
public void can_get_generic_superclass_type() throws Exception { | |
final ParameterizedType pt = (ParameterizedType)MyGenericClass.class.getGenericSuperclass(); | |
assertThat( (Class<Float>)pt.getActualTypeArguments()[0], equalTo(Float.class)); | |
} | |
@Test | |
public void can_get_generic_interface_type() throws Exception { | |
final ParameterizedType pt = (ParameterizedType)MyGenericClass.class.getGenericInterfaces()[0]; | |
assertThat( (Class<Long>)pt.getActualTypeArguments()[0], equalTo(Long.class)); | |
} | |
@Test | |
public void can_get_generic_method_return_type() throws Exception { | |
final ParameterizedType pt = (ParameterizedType)MyGenericClass.class.getDeclaredMethod("genericMethodReturn").getGenericReturnType(); | |
assertThat( (Class<String>)pt.getActualTypeArguments()[0], equalTo(String.class)); | |
} | |
@Test | |
public void can_get_generic_method_parameter_type() throws Exception { | |
final ParameterizedType pt = (ParameterizedType)MyGenericClass.class.getDeclaredMethod("genericMethodParameter", GenericInterface.class).getGenericParameterTypes()[0]; | |
assertThat( (Class<Double>)pt.getActualTypeArguments()[0], equalTo(Double.class)); | |
} | |
@Test | |
public void can_get_generic_local_interface_type() throws Exception { | |
final GenericInterface<Float> local = new GenericInterface<Float>() {}; | |
final ParameterizedType pt = (ParameterizedType)local.getClass().getGenericInterfaces()[0]; | |
assertThat( (Class<Float>)pt.getActualTypeArguments()[0], equalTo(Float.class)); | |
} | |
@Test | |
public void cannot_get_generic_local_class_type() throws Exception { | |
final GenericSuperclass local = new GenericSuperclass<Double>(); | |
final TypeVariable tv = local.getClass().getTypeParameters()[0]; | |
assertThat( (Class<String>)tv.getBounds()[0], not(equalTo(String.class))); | |
} | |
public interface GenericInterface<T> { | |
} | |
public class GenericSuperclass<T> { | |
} | |
public class MyGenericClass extends GenericSuperclass<Float> implements GenericInterface<Long> { | |
private GenericInterface<Integer> genericField; | |
public GenericInterface<String> genericMethodReturn() { | |
return new GenericInterface<String>() {}; | |
} | |
public void genericMethodParameter(GenericInterface<Double> genericArgument) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment