Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.github.joschi.jersey.util;
This class is a trick used to extract GenericType information at runtime. Java does not allow you get generic type information easily, so this class does the trick. For example:

 Type genericType = (new GenericType<List<String>>() {}).getGenericType();

The above code will get you the genericType for List<String>

Bill Burke
$Revision: 1 $
public class GenericType<T>
   final Class<T> type;
   final Type genericType;

Constructs a new generic entity. Derives represented class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.

entity the entity instance, must not be null
java.lang.IllegalArgumentException if entity is null
   protected GenericType()
      Type superclass = getClass().getGenericSuperclass();
      if (!(superclass instanceof ParameterizedType))
         throw new RuntimeException("Missing type parameter.");
      ParameterizedType parameterized = (ParameterizedTypesuperclass;
      this. = parameterized.getActualTypeArguments()[0];
      this. = (Class<T>) Types.getRawType();

Gets the raw type of the enclosed entity. Note that this is the raw type of the instance, not the raw type of the type parameter. I.e. in the example in the introduction, the raw type is ArrayList not List.

the raw type
   public final Class<T> getType()
      return ;

Gets underlying Type instance. Note that this is derived from the type parameter, not the enclosed instance. I.e. in the example in the introduction, the type is List<String> not ArrayList<String>.

the type
   public final Type getGenericType()
      return ;
New to GrepCode? Check out our FAQ X