Guava TypeToken - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Guava TypeToken class.

This code example shows how to use the following methods:getRawType, getType
import com.google.common.reflect.Invokable;
import com.google.common.reflect.TypeToken;
 
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);
   } 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getRawType, getType
      } 
      return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType);
   } 
 
   static Type getReturnTypeFor(TypeToken<?> typeToken) {
      Type returnVal = typeToken.getType();
      if (typeToken.getRawType().getTypeParameters().length == 0) {
         returnVal = typeToken.getRawType();
      } 
      return returnVal;
   } 
 
   // TODO: refactor this out of here 
   @VisibleForTesting 
   @SuppressWarnings({ "rawtypes", "unchecked" }) 
   public Function<HttpResponse, ?> getTransformerForMethod(Invocation invocation, Injector injector) {
      Invokable<?, ?> invoked = invocation.getInvokable();
      Function<HttpResponse, ?> transformer;
      if (invoked.isAnnotationPresent(SelectJson.class)) {
         Type returnVal = getReturnTypeFor(invoked.getReturnType());
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:equals
    TypeToken<T> type = parse.getType();
 
    ObjectMapper objectMapper = getObjectMapper(opts);
    try { 
      InputStream inputStream = body.getInputStream();
      if (type.equals(JSON_NODE_TYPE)) {
        return cast(objectMapper.readTree(inputStream));
      } else { 
        return objectMapper.readValue(inputStream, toJavaType(type, objectMapper));
      } 
    } catch (IOException e) {
      throw uncheck(e);
    } 
  } 
 
  private <T> JavaType toJavaType(TypeToken<T> type, ObjectMapper objectMapper) {
    return objectMapper.getTypeFactory().constructType(type.getType());
  } 
 
  private ObjectMapper getObjectMapper(JsonParseOpts opts) {
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getRawType, getType
   * @param <T> the type of object to construct from the request body 
   * @return an instance of {@code T} if this parser can construct this type, otherwise {@code null} 
   * @throws Exception any exception thrown while parsing 
   */ 
  abstract protected <T> T parse(Context context, TypedData requestBody, TypeToken<T> type) throws Exception; 
 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getRawType, getType
    this.contentType = contentType;
 
    TypeToken<O> typeToken = new TypeToken<O>(getClass()) {
    }; 
 
    if (typeToken.getType() instanceof Class) {
      @SuppressWarnings("unchecked") Class<O> rawType = (Class<O>) typeToken.getRawType();
      this.optsType = rawType;
    } else { 
      throw new IllegalArgumentException("Type parameter O of ParserSupport must be a Class");
    } 
  } 
 
  /** 
   * {@inheritDoc} 
   */ 
  @Override 
  public final String getContentType() {
    return contentType;
  } 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getType
    super(new InjectorRegistryBacking(injector));
    this.injector = injector;
  } 
 
  public <T> Optional<T> maybeGet(TypeToken<T> type) {
    @SuppressWarnings("unchecked") TypeLiteral<T> typeLiteral = (TypeLiteral<T>) TypeLiteral.get(type.getType());
    try { 
      return Optional.of(injector.getInstance(Key.get(typeLiteral)));
    } catch (ConfigurationException e) { 
      return Optional.empty();
    } 
  } 
 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:equals
  } 
 
  @SuppressWarnings("unchecked") 
  @Override 
  public <T> T parse(Context context, TypedData requestBody, TypeToken<T> type) throws Exception {
    if (type.equals(FORM_TYPE)) {
      return (T) FormDecoder.parseForm(context, requestBody);
    } else { 
      return null; 
    } 
  } 
 
  public static FormParser multiPart() { 
    return new FormParser("multipart/form-data"); 
  } 
 
  public static FormParser urlEncoded() { 
    return new FormParser("application/x-www-form-urlencoded"); 
  } 
 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:toString
    return object;
  } 
 
  @Override 
  public String toString() {
    return "RegistryEntry{type=" + type.toString() + ", value=" + object + '}';
  } 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getType
        return (Key<Set<T>>) Key.get(setOf(clazz));
    } 
 
    private static <K> Type setOf(Class<K> clazz)
    { 
        return new TypeToken<Set<K>>() {}.where(new TypeParameter<K>() {}, TypeToken.of(clazz)).getType();
    } 
 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getRawType
     * 
     * @param type   the actor's type 
     * @param params the actor's constructor parameters. 
     */ 
    public static <Message, V, T extends Actor<Message, V>> ActorSpec<T, Message, V> of(TypeToken<T> type, Object... params) {
        return new ActorSpec<T, Message, V>((Class<T>) type.getRawType(), params);
    } 
 
    private final AtomicReference<Class<T>> classRef;
    private final String className;
    private final Object[] params; 
    private Class<?>[] ctorParamTypes;
    private Constructor<T> ctor;
 
    /** 
     * Specifies an actor of a given type and given constructor parameters. 
     * 
     * @param className the name of the actor class 
     * @param params    the parameters to pass to the actor's constructors 
     */ 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
See Code Examples for Guava TypeToken Methods: