com.google.common.reflect.TypeParameter Java Examples

The following examples show how to use com.google.common.reflect.TypeParameter. 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
Source File: TypeManifest.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
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 File: ModelMeta.java    From ProjectAres with 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 File: SchemaRepositoryTest.java    From endpoints-java with 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 File: ItemRequest.java    From java-asana with 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 File: CollectionRequest.java    From java-asana with 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 File: SingularityJsonTranscoderBinder.java    From Singularity with Apache License 2.0 5 votes vote down vote up
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 #7
Source File: Types.java    From riptide with 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 #8
Source File: Types.java    From riptide with 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 #9
Source File: Streams.java    From riptide with 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 #10
Source File: SingularityJsonTranscoderBinder.java    From Singularity with Apache License 2.0 5 votes vote down vote up
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 #11
Source File: SingularityJsonTranscoderBinder.java    From Singularity with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: MapTypeCoercer.java    From buck with 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 #13
Source File: EitherTypeCoercer.java    From buck with 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 #14
Source File: SortedSetTypeCoercer.java    From buck with 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 #15
Source File: VersionMatchedCollectionTypeCoercer.java    From buck with 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 #16
Source File: ListTypeCoercer.java    From buck with 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 #17
Source File: SortedMapTypeCoercer.java    From buck with 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 #18
Source File: OptionalTypeCoercer.java    From buck with 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 #19
Source File: PairTypeCoercer.java    From buck with 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 #20
Source File: PatternMatchedCollectionTypeCoercer.java    From buck with 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 #21
Source File: SetTypeCoercer.java    From buck with 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 #22
Source File: DatabaseMetadataModule.java    From presto with 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 #23
Source File: ThriftType.java    From drift with 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 #24
Source File: ThriftType.java    From drift with 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 #25
Source File: ThriftType.java    From drift with 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 #26
Source File: ThriftType.java    From drift with 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 #27
Source File: DriftClientBinder.java    From drift with 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 #28
Source File: ListConfigKey.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("serial")
private static <V> TypeToken<List<V>> typeTokenFor(TypeToken<V> subType) {
    return new TypeToken<List<V>>() {}
             .where(new TypeParameter<V>() {}, subType);
}
 
Example #29
Source File: ProvisionWrapper.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
static <T> Key<ProvisionWrapper<T>> keyOf(TypeLiteral<T> type) {
    return Key.get(new ResolvableType<ProvisionWrapper<T>>(){}.where(new TypeParameter<T>(){}, type));
}
 
Example #30
Source File: Aggregator.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
protected OptionalAggregator(TypeToken<T> innerType) {
    super(innerType, new TypeToken<Optional<T>>(){}.where(new TypeParameter<T>(){}, innerType));
}