Java Code Examples for com.google.common.reflect.TypeParameter

The following examples show how to use com.google.common.reflect.TypeParameter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public TypeManifest(@Nullable TypeLiteral<T> nullableType) {
    this.typeToken = nullableType != null ? Types.toToken(nullableType)
                                          : new TypeToken<T>(getClass()){};
    this.type = nullableType != null ? nullableType
                                     : Types.toLiteral(typeToken);

    Types.assertFullySpecified(this.type);

    this.typeParameter = new TypeParameter<T>(){};
    this.typeArg = new TypeArgument<T>(this.type){};

    TypeResolver resolver = new TypeResolver();
    for(Class<?> cls = getClass(); cls != null; cls = cls.getSuperclass()) {
        if(cls.getTypeParameters().length > 0) {
            resolver = resolver.where(cls.getTypeParameters()[0], type.getType());
        }
    }
    this.resolver = resolver;
}
 
Example 2
Source Project: ProjectAres   Source File: ModelMeta.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
@Inject ModelMeta(TypeLiteral<M> completeType, TypeLiteral<P> partialType, Provider<Optional<ModelStore<M>>> store) {
    this.partialType = Types.toToken(partialType);
    this.completeType = Types.toToken(completeType);

    partialUpdateType = assertFullySpecified(new TypeToken<PartialModelUpdate<P>>(){}.where(new TypeParameter<P>(){}, this.partialType));
    completeUpdateType = assertFullySpecified(new TypeToken<ModelUpdate<M>>(){}.where(new TypeParameter<M>(){}, this.completeType));
    multiResponseType = assertFullySpecified(new TypeToken<FindMultiResponse<M>>(){}.where(new TypeParameter<M>(){}, this.completeType));

    this.store = store;

    final ModelName annot = completeType.getRawType().getAnnotation(ModelName.class);
    if(annot != null) {
        name = annot.value();
        singularName = annot.singular().length() > 0 ? annot.singular() : name.toLowerCase();
        pluralName = annot.plural().length() > 0 ? annot.plural() : StringUtils.pluralize(singularName);
    } else {
        name = completeType.getRawType().getSimpleName();
        singularName = name.toLowerCase();
        pluralName = StringUtils.pluralize(singularName);
    }
}
 
Example 3
Source Project: endpoints-java   Source File: SchemaRepositoryTest.java    License: Apache License 2.0 6 votes vote down vote up
private <T> void checkParameterizedSchema(Schema schema, Class<T> type) {
  assertThat(schema).isEqualTo(Schema.builder()
      .setName("Parameterized_" + type.getSimpleName())
      .setType("object")
      .addField("foo", Field.builder()
          .setName("foo")
          .setType(FieldType.fromType(TypeToken.of(type)))
          .build())
      .addField("next", Field.builder()
          .setName("next")
          .setType(FieldType.OBJECT)
          .setSchemaReference(
              SchemaReference.create(repo, config, new TypeToken<Parameterized<T>>() {}
                  .where(new TypeParameter<T>() {}, TypeToken.of(type))))
          .build())
      .addField("testEnum", Field.builder()
          .setName("testEnum")
          .setType(FieldType.ENUM)
          .setSchemaReference(SchemaReference.create(repo, config, TypeToken.of(TestEnum.class)))
          .build())
      .build());
}
 
Example 4
Source Project: java-asana   Source File: ItemRequest.java    License: MIT License 6 votes vote down vote up
/**
 * Executes the request, returning the full response body
 *
 * @return Body containing the "data" object and other metadata
 * @throws IOException
 */
public ResultBody<T> executeRaw() throws IOException {
    HttpResponse response = this.client.request(this);

    if (this.client.logAsanaChangeWarnings) {
        HttpHeaders reqHeaders = new HttpHeaders();
        reqHeaders.putAll(this.client.headers);
        handleAsanaChangeHeader(reqHeaders, response.getHeaders());
    }

    return Json.getInstance().fromJson(
            new BufferedReader(new InputStreamReader(response.getContent(), StandardCharsets.UTF_8)),
            new TypeToken<ResultBody<T>>() {
            }.where(
                    new TypeParameter<T>() {
                    },
                    this.elementClass
            ).getType()
    );
}
 
Example 5
Source Project: java-asana   Source File: CollectionRequest.java    License: MIT License 6 votes vote down vote up
/**
 * Executes the request, returning the full response body
 *
 * @return Body containing the "data" object and other metadata
 * @throws IOException
 */
public ResultBodyCollection<T> executeRaw() throws IOException {
    HttpResponse response = this.client.request(this);

    if (this.client.logAsanaChangeWarnings) {
        HttpHeaders reqHeaders = new HttpHeaders();
        reqHeaders.putAll(this.client.headers);
        handleAsanaChangeHeader(reqHeaders, response.getHeaders());
    }

    return Json.getInstance().fromJson(
            new BufferedReader(new InputStreamReader(response.getContent(), StandardCharsets.UTF_8)),
            new com.google.common.reflect.TypeToken<ResultBodyCollection<T>>() {
            }.where(
                    new TypeParameter<T>() {
                    },
                    this.elementClass
            ).getType()
    );
}
 
Example 6
Source Project: presto   Source File: DatabaseMetadataModule.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> TypeLiteral<DaoSupplier<? extends T>> daoSupplierTypeToken(Class<T> type)
{
    Type javaType = new TypeToken<DaoSupplier<T>>() {}
            .where(new TypeParameter<T>() {}, TypeToken.of(type))
            .getType();
    return (TypeLiteral<DaoSupplier<? extends T>>) TypeLiteral.get(javaType);
}
 
Example 7
Source Project: drift   Source File: ThriftType.java    License: Apache License 2.0 5 votes vote down vote up
public static <K, V> ThriftType map(ThriftTypeReference keyTypeReference,
        ThriftTypeReference valueTypeReference)
{
    requireNonNull(keyTypeReference, "keyTypeReference is null");
    requireNonNull(valueTypeReference, "valueTypeReference is null");

    @SuppressWarnings("serial")
    Type javaType = new TypeToken<Map<K, V>>() {}
            .where(new TypeParameter<K>() {}, (TypeToken<K>) TypeToken.of(keyTypeReference.getJavaType()))
            .where(new TypeParameter<V>() {}, (TypeToken<V>) TypeToken.of(valueTypeReference.getJavaType()))
            .getType();
    return new ThriftType(ThriftProtocolType.MAP, javaType, keyTypeReference, valueTypeReference, null);
}
 
Example 8
Source Project: drift   Source File: ThriftType.java    License: Apache License 2.0 5 votes vote down vote up
public static <E> ThriftType set(ThriftTypeReference valueTypeReference)
{
    requireNonNull(valueTypeReference, "valueTypeReference is null");

    @SuppressWarnings("serial")
    Type javaType = new TypeToken<Set<E>>() {}
            .where(new TypeParameter<E>() {}, (TypeToken<E>) TypeToken.of(valueTypeReference.getJavaType()))
            .getType();
    return new ThriftType(ThriftProtocolType.SET, javaType, null, valueTypeReference, null);
}
 
Example 9
Source Project: drift   Source File: ThriftType.java    License: Apache License 2.0 5 votes vote down vote up
public static <E> ThriftType list(ThriftTypeReference valueTypeReference)
{
    requireNonNull(valueTypeReference, "valueTypeReference is null");

    @SuppressWarnings("serial")
    Type javaType = new TypeToken<List<E>>() {}
            .where(new TypeParameter<E>() {}, (TypeToken<E>) TypeToken.of(valueTypeReference.getJavaType()))
            .getType();
    return new ThriftType(ThriftProtocolType.LIST, javaType, null, valueTypeReference, null);
}
 
Example 10
Source Project: drift   Source File: ThriftType.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> ThriftType optional(ThriftTypeReference valueTypeReference)
{
    requireNonNull(valueTypeReference, "valueTypeReference is null");

    @SuppressWarnings("serial")
    Type javaType = new TypeToken<Optional<T>>() {}
            .where(new TypeParameter<T>() {}, (TypeToken<T>) TypeToken.of(valueTypeReference.getJavaType()))
            .getType();
    return new ThriftType(valueTypeReference.getProtocolType(), javaType, null, valueTypeReference, Optional.empty());
}
 
Example 11
Source Project: drift   Source File: DriftClientBinder.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> TypeLiteral<DriftClient<T>> driftClientTypeLiteral(Class<T> clientInterface)
{
    Type javaType = new TypeToken<DriftClient<T>>() {}
            .where(new TypeParameter<T>() {}, TypeToken.of(clientInterface))
            .getType();
    return (TypeLiteral<DriftClient<T>>) TypeLiteral.get(javaType);
}
 
Example 12
Source Project: riptide   Source File: Types.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("serial")
public static <T> TypeToken<List<T>> listOf(final TypeToken<T> entityType) {
    final TypeToken<List<T>> listType = new TypeToken<List<T>>() {
        // nothing to implement!
    };

    final TypeParameter<T> elementType = new TypeParameter<T>() {
        // nothing to implement!
    };

    return listType.where(elementType, entityType);
}
 
Example 13
Source Project: riptide   Source File: Types.java    License: MIT License 5 votes vote down vote up
public static <T> TypeToken<ResponseEntity<T>> responseEntityOf(final TypeToken<T> entityType) {
    final TypeToken<ResponseEntity<T>> responseEntityType = new TypeToken<ResponseEntity<T>>() {
        // nothing to implement!
    };

    final TypeParameter<T> elementType = new TypeParameter<T>() {
        // nothing to implement!
    };

    return responseEntityType.where(elementType, entityType);
}
 
Example 14
Source Project: riptide   Source File: Streams.java    License: MIT License 5 votes vote down vote up
/**
 * Creates specialized stream {@link TypeToken type token} for the given element {@link TypeToken type token}. Used
 * to declare the expected stream response {@link TypeToken type token} in Riptide org.zalando.riptide.Route route} as follows:
 * 
 * <pre>
 *     on(...).call(streamOf(resultTypeToken),...)
 * </pre>
 *
 * @param <T> generic stream element type
 * @param type element token type.
 * @return stream token type.
 */
@SuppressWarnings("serial")
public static <T> TypeToken<Stream<T>> streamOf(final TypeToken<T> type) {
    final TypeToken<Stream<T>> streamType = new TypeToken<Stream<T>>() {
        // no overriding needed.
    };

    final TypeParameter<T> elementType = new TypeParameter<T>() {
        // no overriding needed.
    };

    return streamType.where(elementType, type);
}
 
Example 15
public <T> void asJson(Class<T> clazz) {
  TypeToken<Transcoder<T>> typeToken = new TypeToken<Transcoder<T>>() {}
  .where(new TypeParameter<T>() {}, clazz);
  @SuppressWarnings("unchecked")
  Key<Transcoder<T>> key = (Key<Transcoder<T>>) Key.get(typeToken.getType());
  binder
    .bind(key)
    .toProvider(new JsonTranscoderProvider<T>(clazz))
    .in(Scopes.SINGLETON);
}
 
Example 16
public <T> void asCompressedJson(Class<T> clazz) {
  TypeToken<Transcoder<T>> typeToken = new TypeToken<Transcoder<T>>() {}
  .where(new TypeParameter<T>() {}, clazz);
  @SuppressWarnings("unchecked")
  Key<Transcoder<T>> key = (Key<Transcoder<T>>) Key.get(typeToken.getType());
  binder
    .bind(key)
    .toProvider(new CompressingJsonTranscoderProvider<T>(clazz))
    .in(Scopes.SINGLETON);
}
 
Example 17
public <T extends SingularityId> void asSingularityId(Class<T> clazz) {
  TypeToken<IdTranscoder<T>> typeToken = new TypeToken<IdTranscoder<T>>() {}
  .where(new TypeParameter<T>() {}, clazz);
  @SuppressWarnings("unchecked")
  Key<IdTranscoder<T>> key = (Key<IdTranscoder<T>>) Key.get(typeToken.getType());
  binder.bind(key).toInstance(new IdTranscoder<T>(clazz));
}
 
Example 18
Source Project: buck   Source File: EitherTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public EitherTypeCoercer(
    TypeCoercer<LU, Left> leftTypeCoercer, TypeCoercer<RU, Right> rightTypeCoercer) {
  this.leftTypeCoercer = leftTypeCoercer;
  this.rightTypeCoercer = rightTypeCoercer;
  this.typeToken =
      new TypeToken<Either<Left, Right>>() {}.where(
              new TypeParameter<Left>() {}, leftTypeCoercer.getOutputType())
          .where(new TypeParameter<Right>() {}, rightTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<Either<LU, RU>>() {}.where(
              new TypeParameter<LU>() {}, leftTypeCoercer.getUnconfiguredType())
          .where(new TypeParameter<RU>() {}, rightTypeCoercer.getUnconfiguredType());
}
 
Example 19
Source Project: buck   Source File: SortedSetTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public SortedSetTypeCoercer(TypeCoercer<U, T> elementTypeCoercer) {
  super(elementTypeCoercer);
  this.typeToken =
      new TypeToken<ImmutableSortedSet<T>>() {}.where(
          new TypeParameter<T>() {}, elementTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<ImmutableList<U>>() {}.where(
          new TypeParameter<U>() {}, elementTypeCoercer.getUnconfiguredType());
}
 
Example 20
Source Project: buck   Source File: VersionMatchedCollectionTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public VersionMatchedCollectionTypeCoercer(
    TypeCoercer<?, ImmutableMap<BuildTarget, Version>> versionsTypeCoercer,
    TypeCoercer<?, T> valueTypeCoercer) {
  this.versionsTypeCoercer = versionsTypeCoercer;
  this.valueTypeCoercer = valueTypeCoercer;
  this.typeToken =
      new TypeToken<VersionMatchedCollection<T>>() {}.where(
          new TypeParameter<T>() {}, valueTypeCoercer.getOutputType());
}
 
Example 21
Source Project: buck   Source File: ListTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public ListTypeCoercer(TypeCoercer<U, T> elementTypeCoercer) {
  super(elementTypeCoercer);
  this.typeToken =
      new TypeToken<ImmutableList<T>>() {}.where(
          new TypeParameter<T>() {}, elementTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<ImmutableList<U>>() {}.where(
          new TypeParameter<U>() {}, elementTypeCoercer.getUnconfiguredType());
}
 
Example 22
Source Project: buck   Source File: SortedMapTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
SortedMapTypeCoercer(TypeCoercer<KU, K> keyTypeCoercer, TypeCoercer<VU, V> valueTypeCoercer) {
  this.keyTypeCoercer = keyTypeCoercer;
  this.valueTypeCoercer = valueTypeCoercer;
  this.typeToken =
      new TypeToken<ImmutableSortedMap<K, V>>() {}.where(
              new TypeParameter<K>() {}, keyTypeCoercer.getOutputType())
          .where(new TypeParameter<V>() {}, valueTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<ImmutableSortedMap<KU, VU>>() {}.where(
              new TypeParameter<KU>() {}, keyTypeCoercer.getUnconfiguredType())
          .where(new TypeParameter<VU>() {}, valueTypeCoercer.getUnconfiguredType());
}
 
Example 23
Source Project: buck   Source File: OptionalTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public OptionalTypeCoercer(TypeCoercer<U, T> coercer) {
  Preconditions.checkArgument(
      !coercer.getOutputType().getRawType().isAssignableFrom(Optional.class),
      "Nested optional fields are ambiguous.");
  this.coercer = coercer;
  this.typeToken =
      new TypeToken<Optional<T>>() {}.where(new TypeParameter<T>() {}, coercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<Optional<U>>() {}.where(
          new TypeParameter<U>() {}, coercer.getUnconfiguredType());
}
 
Example 24
Source Project: buck   Source File: PairTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public PairTypeCoercer(
    TypeCoercer<FU, FIRST> firstTypeCoercer, TypeCoercer<SU, SECOND> secondTypeCoercer) {
  this.firstTypeCoercer = firstTypeCoercer;
  this.secondTypeCoercer = secondTypeCoercer;
  this.typeToken =
      new TypeToken<Pair<FIRST, SECOND>>() {}.where(
              new TypeParameter<FIRST>() {}, firstTypeCoercer.getOutputType())
          .where(new TypeParameter<SECOND>() {}, secondTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<Pair<FU, SU>>() {}.where(
              new TypeParameter<FU>() {}, firstTypeCoercer.getUnconfiguredType())
          .where(new TypeParameter<SU>() {}, secondTypeCoercer.getUnconfiguredType());
}
 
Example 25
Source Project: buck   Source File: PatternMatchedCollectionTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public PatternMatchedCollectionTypeCoercer(
    TypeCoercer<?, Pattern> patternTypeCoercer, TypeCoercer<?, T> valueTypeCoercer) {
  this.patternTypeCoercer = patternTypeCoercer;
  this.valueTypeCoercer = valueTypeCoercer;
  this.typeToken =
      new TypeToken<PatternMatchedCollection<T>>() {}.where(
          new TypeParameter<T>() {}, valueTypeCoercer.getOutputType());
}
 
Example 26
Source Project: buck   Source File: SetTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
SetTypeCoercer(TypeCoercer<U, T> elementTypeCoercer) {
  super(elementTypeCoercer);
  this.typeToken =
      new TypeToken<ImmutableSet<T>>() {}.where(
          new TypeParameter<T>() {}, elementTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<ImmutableSet<U>>() {}.where(
          new TypeParameter<U>() {}, elementTypeCoercer.getUnconfiguredType());
}
 
Example 27
Source Project: buck   Source File: MapTypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
public MapTypeCoercer(TypeCoercer<KU, K> keyTypeCoercer, TypeCoercer<VU, V> valueTypeCoercer) {
  this.keyTypeCoercer = keyTypeCoercer;
  this.valueTypeCoercer = valueTypeCoercer;
  this.typeToken =
      new TypeToken<ImmutableMap<K, V>>() {}.where(
              new TypeParameter<K>() {}, keyTypeCoercer.getOutputType())
          .where(new TypeParameter<V>() {}, valueTypeCoercer.getOutputType());
  this.typeTokenUnconfigured =
      new TypeToken<ImmutableMap<KU, VU>>() {}.where(
              new TypeParameter<KU>() {}, keyTypeCoercer.getUnconfiguredType())
          .where(new TypeParameter<VU>() {}, valueTypeCoercer.getUnconfiguredType());
}
 
Example 28
static <T> Key<ProvisionWrapper<T>> keyOf(TypeLiteral<T> type) {
    return Key.get(new ResolvableType<ProvisionWrapper<T>>(){}.where(new TypeParameter<T>(){}, type));
}
 
Example 29
protected OptionalAggregator(TypeToken<T> innerType) {
    super(innerType, new TypeToken<Optional<T>>(){}.where(new TypeParameter<T>(){}, innerType));
}
 
Example 30
protected ListAggregator(TypeToken<T> innerType) {
    super(innerType, new TypeToken<List<T>>(){}.where(new TypeParameter<T>(){}, innerType));
}