com.google.inject.util.Types类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(108)

本文整理了Java中com.google.inject.util.Types类的一些代码示例,展示了Types类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types类的具体详情如下:
包路径:com.google.inject.util.Types
类名称:Types

Types介绍

[英]Static methods for working with types.
[中]使用类型的静态方法。

代码示例

代码示例来源:origin: apache/incubator-druid

public T get()
 final ParameterizedType mapType = Types.mapOf(
   String.class, Types.newParameterizedType(Provider.class, key.getTypeLiteral().getType())
 );
 if (key.getAnnotation() != null) {
  implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType, key.getAnnotation()));
 } else if (key.getAnnotationType() != null) {
  implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType, key.getAnnotationType()));
 } else {
  implsMap = (Map<String, Provider<T>>) injector.getInstance(Key.get(mapType));

代码示例来源:origin: apache/shiro

@SuppressWarnings({"unchecked"})
private Key<Set<Realm>> realmSetKey() {
  return (Key<Set<Realm>>) Key.get(TypeLiteral.get(Types.setOf(Realm.class)));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a provider entry <K, V> is safely a Map.Entry<K, Provider<V>>
static <K, V> TypeLiteral<Map.Entry<K, Provider<V>>> entryOfProviderOf(
  TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map.Entry<K, Provider<V>>>)
   TypeLiteral.get(
     newParameterizedTypeWithOwner(
       Map.class,
       Map.Entry.class,
       keyType.getType(),
       Types.providerOf(valueType.getType())));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a provider map <K, Set<V>> is safely a Map<K, Set<Provider<V>>>
static <K, V>
  TypeLiteral<Map<K, Collection<javax.inject.Provider<V>>>> mapOfCollectionOfJavaxProviderOf(
    TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map<K, Collection<javax.inject.Provider<V>>>>)
   TypeLiteral.get(
     Types.mapOf(
       keyType.getType(), Types.collectionOf(Types.javaxProviderOf(valueType.getType()))));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a provider map <K, Set<V>> is safely a Map<K, Set<Provider<V>>>
static <K, V> TypeLiteral<Map<K, Set<javax.inject.Provider<V>>>> mapOfSetOfJavaxProviderOf(
  TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map<K, Set<javax.inject.Provider<V>>>>)
   TypeLiteral.get(
     Types.mapOf(
       keyType.getType(), Types.setOf(Types.javaxProviderOf(valueType.getType()))));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a provider map <K, Set<V>> is safely a Map<K, Set<Provider<V>>>
static <K, V> TypeLiteral<Map<K, Set<Provider<V>>>> mapOfSetOfProviderOf(
  TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map<K, Set<Provider<V>>>>)
   TypeLiteral.get(
     Types.mapOf(keyType.getType(), Types.setOf(Types.providerOf(valueType.getType()))));
}

代码示例来源:origin: jclouds/legacy-jclouds

Object getInstanceOfTypeWithQualifier(Type genericReturnType, Annotation qualifier) {
 // look for an existing binding
 Binding<?> binding = injector.getExistingBinding(Key.get(genericReturnType, qualifier));
 if (binding != null)
   return binding.getProvider().get();
 // then, try looking via supplier
 binding = injector
    .getExistingBinding(Key.get(newParameterizedType(Supplier.class, genericReturnType), qualifier));
 if (binding != null)
   return Supplier.class.cast(binding.getProvider().get()).get();
 // else try to create an instance
 return injector.getInstance(Key.get(genericReturnType, qualifier));
}

代码示例来源:origin: org.seedstack.business/business-core

private Object getInstance(Type rawType, Class<? extends Annotation> qualifier, Type... typeArguments) {
  return injector.getInstance(
      Key.get(TypeLiteral.get(Types.newParameterizedType(rawType, typeArguments)), qualifier));
}

代码示例来源:origin: org.seedstack.business/business-core

@SuppressWarnings("unchecked")
private DataImporter<Object> getImporterInstance(Class<?> importedClass) {
  return injector.getInstance(
      (Key<DataImporter<Object>>) Key.get(Types.newParameterizedType(DataImporter.class, importedClass)));
}

代码示例来源:origin: apache/shiro

@SuppressWarnings({"unchecked"})
private Key<Collection<Realm>> realmCollectionKey() {
  return (Key<Collection<Realm>>) Key.get(Types.newParameterizedType(Collection.class, Realm.class));
}

代码示例来源:origin: io.github.javaeden.orchid/OrchidCore

public <T> Set<T> resolveSet(Class<T> clazz) {
  Injector injector = getInjector();
  try {
    TypeLiteral<Set<T>> lit = (TypeLiteral<Set<T>>) TypeLiteral.get(Types.setOf(clazz));
    Key<Set<T>> key = Key.get(lit);
    Set<T> bindings = injector.getInstance(key);
    if (bindings != null) {
      return bindings;
    }
  }
  catch (Exception e) {
  }
  return new TreeSet<>();
}

代码示例来源:origin: apache/incubator-druid

@SuppressWarnings("unchecked")
public static <T> void bindInstance(
  Binder binder,
  Key<T> bindKey,
  T instance
)
{
 binder.bind(bindKey).toInstance(instance);
 final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType());
 final Key supplierKey;
 if (bindKey.getAnnotationType() != null) {
  supplierKey = Key.get(supType, bindKey.getAnnotationType());
 } else if (bindKey.getAnnotation() != null) {
  supplierKey = Key.get(supType, bindKey.getAnnotation());
 } else {
  supplierKey = Key.get(supType);
 }
 binder.bind(supplierKey).toInstance(Suppliers.ofInstance(instance));
}

代码示例来源:origin: com.netflix.karyon/karyon-governator

@SuppressWarnings("unchecked")
  public static <T> Key<T> keyFor(Class<?> type, Class<?> typeArg1, Class<?> typeArg2, Annotation annotation) {
    TypeLiteral<T> typeLiteral = (TypeLiteral<T>) TypeLiteral.get(Types.newParameterizedType(type, typeArg1, typeArg2));
    return Key.get(typeLiteral, annotation);
  }
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // wrapping a T in a Set safely returns a Set<T>
static <T> TypeLiteral<Set<T>> setOf(TypeLiteral<T> elementType) {
 Type type = Types.setOf(elementType.getType());
 return (TypeLiteral<Set<T>>) TypeLiteral.get(type);
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked")
static <T> TypeLiteral<Optional<javax.inject.Provider<T>>> optionalOfJavaxProvider(
  TypeLiteral<T> type) {
 return (TypeLiteral<Optional<javax.inject.Provider<T>>>)
   TypeLiteral.get(
     Types.newParameterizedType(
       Optional.class, newParameterizedType(javax.inject.Provider.class, type.getType())));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a provider entry <K, V> is safely a Map.Entry<K, Provider<V>>
static <K, V> TypeLiteral<Map.Entry<K, Provider<V>>> entryOfJavaxProviderOf(
  TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map.Entry<K, Provider<V>>>)
   TypeLiteral.get(
     newParameterizedTypeWithOwner(
       Map.class,
       Map.Entry.class,
       keyType.getType(),
       Types.javaxProviderOf(valueType.getType())));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked")
static <K, V> TypeLiteral<Map<K, javax.inject.Provider<V>>> mapOfJavaxProviderOf(
  TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map<K, javax.inject.Provider<V>>>)
   TypeLiteral.get(
     Types.mapOf(
       keyType.getType(),
       newParameterizedType(javax.inject.Provider.class, valueType.getType())));
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked")
static <T> TypeLiteral<Collection<javax.inject.Provider<T>>> collectionOfJavaxProvidersOf(
  TypeLiteral<T> elementType) {
 Type providerType =
   Types.newParameterizedType(javax.inject.Provider.class, elementType.getType());
 Type type = Types.collectionOf(providerType);
 return (TypeLiteral<Collection<javax.inject.Provider<T>>>) TypeLiteral.get(type);
}

代码示例来源:origin: net.stickycode.bootstrap/sticky-bootstrap-guice4

@SuppressWarnings({ "rawtypes", "unchecked" })
protected void bindParameterizedType(Class<?> annotatedClass, Type type) {
 Type wildcard = Types.subtypeOf(Object.class);
 Type rawType = ((ParameterizedType) type).getRawType();
 Type target = Types.newParameterizedType(rawType, wildcard);
 TypeLiteral literal = TypeLiteral.get(target);
 debug("multi bind paramterized type {} to {}", literal, annotatedClass);
 Multibinder.newSetBinder(binder(), literal).addBinding().to(annotatedClass);
}

代码示例来源:origin: com.google.inject/guice

@SuppressWarnings("unchecked") // a map of <K, V> is safely a Map<K, V>
static <K, V> TypeLiteral<Map<K, V>> mapOf(TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
 return (TypeLiteral<Map<K, V>>)
   TypeLiteral.get(Types.mapOf(keyType.getType(), valueType.getType()));
}

相关文章