Skip to content

Instantly share code, notes, and snippets.

@bufferings
Created December 15, 2015 23:01
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 bufferings/e146c006aeb728ce0123 to your computer and use it in GitHub Desktop.
Save bufferings/e146c006aeb728ce0123 to your computer and use it in GitHub Desktop.
import org.glassfish.jersey.internal.util.collection.Ref;
import org.junit.Test;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
abstract class MyTypeLiteral<T> {
Type getType() {
ParameterizedType paramterizeType = (ParameterizedType) this.getClass().getGenericSuperclass();
return paramterizeType.getActualTypeArguments()[0];
}
}
public class MyTypeLiteralTest {
@Test
public void testGetType() throws Exception {
Type type = (new MyTypeLiteral<Ref<HttpServletRequest>>() {
}).getType();
assertThat(type, instanceOf(ParameterizedType.class));
ParameterizedType parameterizedType = (ParameterizedType) type;
assertThat(parameterizedType.getTypeName(),
is("org.glassfish.jersey.internal.util.collection.Ref<javax.servlet.http.HttpServletRequest>"));
assertThat(parameterizedType.getOwnerType(), is(nullValue()));
assertThat(parameterizedType.getRawType(), is((Type) Ref.class));
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
assertThat(actualTypeArguments.length, is(1));
assertThat(actualTypeArguments[0], is((Type) HttpServletRequest.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment