Code example for TypeToken

Methods: getRawTypegetType

public class Optionals2 { 
   public static Class<?> unwrapIfOptional(TypeToken<?> type) {
      if (type.getRawType().isAssignableFrom(Optional.class)) {
         ParameterizedType futureType = ParameterizedType.class.cast(type.getType());
         // TODO: error checking in case this is a type, not a class. 
         Type t = futureType.getActualTypeArguments()[0];
         if (t instanceof WildcardType) {
            t = ((WildcardType) t).getUpperBounds()[0];
         return Class.class.cast(t);
      return type.getRawType();
   public static boolean isReturnTypeOptional(Invokable<?, ?> method) {
      return method.getReturnType().getRawType().isAssignableFrom(Optional.class);
Connect your IDE to all the code out there  Get Codota for Java