com.google.common.reflect.TypeToken.getTypes()方法的使用及代码示例

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

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

TypeToken.getTypes介绍

[英]Returns the set of interfaces and classes that this type is or is a subtype of. The returned types are parameterized with proper type arguments.

Subtypes are always listed before supertypes. But the reverse is not true. A type isn't necessarily a subtype of all the types following. Order between types without subtype relationship is arbitrary and not guaranteed.

If this type is a type variable or wildcard, upper bounds that are themselves type variables aren't included (their super interfaces and superclasses are).
[中]返回此类型是或是其子类型的接口和类的集合。返回的类型使用适当的类型参数进行参数化。
子类型总是列在超类型之前。但事实并非如此。类型不一定是以下所有类型的子类型。没有子类型关系的类型之间的顺序是任意的,不能保证。
如果该类型是类型变量或通配符,则不包括本身是类型变量的上限(它们的超级接口和超类是)。

代码示例

代码示例来源:origin: google/guava

@SuppressWarnings("RedundantTypeArguments")
 @Override
 public ImmutableSet<Class<?>> load(Class<?> concreteClass) {
  return ImmutableSet.<Class<?>>copyOf(
    TypeToken.of(concreteClass).getTypes().rawTypes());
 }
});

代码示例来源:origin: prestodb/presto

private static Stream<Class<?>> supertypes(Class<?> type)
{
  return TypeToken.of(type).getTypes().stream()
      .map(TypeToken::getRawType);
}

代码示例来源:origin: google/guava

private boolean isOwnedBySubtypeOf(Type supertype) {
 for (TypeToken<?> type : getTypes()) {
  Type ownerType = type.getOwnerTypeIfPresent();
  if (ownerType != null && of(ownerType).isSubtypeOf(supertype)) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: google/j2objc

@SuppressWarnings("RedundantTypeArguments")
 @Override
 public ImmutableSet<Class<?>> load(Class<?> concreteClass) {
  return ImmutableSet.<Class<?>>copyOf(
    TypeToken.of(concreteClass).getTypes().rawTypes());
 }
});

代码示例来源:origin: google/guava

/**
 * Returns a new proxy for {@code interfaceType}. Proxies of the same interface are equal to each
 * other if the {@link DummyProxy} instance that created the proxies are equal.
 */
final <T> T newProxy(TypeToken<T> interfaceType) {
 Set<Class<?>> interfaceClasses = Sets.newLinkedHashSet();
 interfaceClasses.addAll(interfaceType.getTypes().interfaces().rawTypes());
 // Make the proxy serializable to work with SerializableTester
 interfaceClasses.add(Serializable.class);
 Object dummy =
   Proxy.newProxyInstance(
     interfaceClasses.iterator().next().getClassLoader(),
     interfaceClasses.toArray(new Class<?>[interfaceClasses.size()]),
     new DummyHandler(interfaceType));
 @SuppressWarnings("unchecked") // interfaceType is T
 T result = (T) dummy;
 return result;
}

代码示例来源:origin: google/guava

private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
 Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes();
 Map<MethodIdentifier, Method> identifiers = Maps.newHashMap();
 for (Class<?> supertype : supertypes) {
  for (Method method : supertype.getDeclaredMethods()) {
   if (method.isAnnotationPresent(Subscribe.class) && !method.isSynthetic()) {
    // TODO(cgdecker): Should check for a generic parameter type and error out
    Class<?>[] parameterTypes = method.getParameterTypes();
    checkArgument(
      parameterTypes.length == 1,
      "Method %s has @Subscribe annotation but has %s parameters."
        + "Subscriber methods must have exactly 1 parameter.",
      method,
      parameterTypes.length);
    MethodIdentifier ident = new MethodIdentifier(method);
    if (!identifiers.containsKey(ident)) {
     identifiers.put(ident, method);
    }
   }
  }
 }
 return ImmutableList.copyOf(identifiers.values());
}

代码示例来源:origin: google/guava

private ImmutableList<Method> getVisibleMethods(Class<?> cls) {
  // Don't use cls.getPackage() because it does nasty things like reading
  // a file.
  String visiblePackage = Reflection.getPackageName(cls);
  ImmutableList.Builder<Method> builder = ImmutableList.builder();
  for (Class<?> type : TypeToken.of(cls).getTypes().rawTypes()) {
   if (!Reflection.getPackageName(type).equals(visiblePackage)) {
    break;
   }
   for (Method method : type.getDeclaredMethods()) {
    if (!method.isSynthetic() && isVisible(method)) {
     builder.add(method);
    }
   }
  }
  return builder.build();
 }
}

代码示例来源:origin: wildfly/wildfly

@SuppressWarnings("RedundantTypeArguments")
 @Override
 public ImmutableSet<Class<?>> load(Class<?> concreteClass) {
  return ImmutableSet.<Class<?>>copyOf(
    TypeToken.of(concreteClass).getTypes().rawTypes());
 }
});

代码示例来源:origin: kairosdb/kairosdb

@SuppressWarnings({"RedundantTypeArguments", "NullableProblems"})
  @Override
  public ImmutableSet<Class<?>> load(Class<?> concreteClass)
  {
    return ImmutableSet.<Class<?>>copyOf(
        TypeToken.of(concreteClass).getTypes().rawTypes());
  }
});

代码示例来源:origin: google/j2objc

private boolean isOwnedBySubtypeOf(Type supertype) {
 for (TypeToken<?> type : getTypes()) {
  Type ownerType = type.getOwnerTypeIfPresent();
  if (ownerType != null && of(ownerType).isSubtypeOf(supertype)) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: google/guava

public void testDespiteGenericSignatureFormatError() {
 ImmutableSet<?> unused =
   ImmutableSet.copyOf(
     TypeToken.of(ToReproduceGenericSignatureFormatError.SubOuter.SubInner.class)
       .getTypes()
       .rawTypes());
}

代码示例来源:origin: google/j2objc

private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
 Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes();
 Map<MethodIdentifier, Method> identifiers = Maps.newHashMap();
 for (Class<?> supertype : supertypes) {
  for (Method method : supertype.getDeclaredMethods()) {
   if (method.isAnnotationPresent(Subscribe.class) && !method.isSynthetic()) {
    // TODO(cgdecker): Should check for a generic parameter type and error out
    Class<?>[] parameterTypes = method.getParameterTypes();
    checkArgument(
      parameterTypes.length == 1,
      "Method %s has @Subscribe annotation but has %s parameters."
        + "Subscriber methods must have exactly 1 parameter.",
      method,
      parameterTypes.length);
    MethodIdentifier ident = new MethodIdentifier(method);
    if (!identifiers.containsKey(ident)) {
     identifiers.put(ident, method);
    }
   }
  }
 }
 return ImmutableList.copyOf(identifiers.values());
}

代码示例来源:origin: wildfly/wildfly

private boolean isOwnedBySubtypeOf(Type supertype) {
 for (TypeToken<?> type : getTypes()) {
  Type ownerType = type.getOwnerTypeIfPresent();
  if (ownerType != null && of(ownerType).isSubtypeOf(supertype)) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: wildfly/wildfly

private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
 Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes();
 Map<MethodIdentifier, Method> identifiers = Maps.newHashMap();
 for (Class<?> supertype : supertypes) {
  for (Method method : supertype.getDeclaredMethods()) {
   if (method.isAnnotationPresent(Subscribe.class) && !method.isSynthetic()) {
    // TODO(cgdecker): Should check for a generic parameter type and error out
    Class<?>[] parameterTypes = method.getParameterTypes();
    checkArgument(
      parameterTypes.length == 1,
      "Method %s has @Subscribe annotation but has %s parameters."
        + "Subscriber methods must have exactly 1 parameter.",
      method,
      parameterTypes.length);
    MethodIdentifier ident = new MethodIdentifier(method);
    if (!identifiers.containsKey(ident)) {
     identifiers.put(ident, method);
    }
   }
  }
 }
 return ImmutableList.copyOf(identifiers.values());
}

代码示例来源:origin: kairosdb/kairosdb

private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz)
  Set<? extends Class<?>> supertypes = TypeToken.of(clazz).getTypes().rawTypes();
  Map<MethodIdentifier, Method> identifiers = Maps.newHashMap();
  for (Class<?> supertype : supertypes)

代码示例来源:origin: google/guava

public <A extends Interface1 & Interface2 & Interface3<String>> void testGetTypes_manyBounds() {
 TypeToken<?>.TypeSet types = TypeToken.of(new TypeCapture<A>() {}.capture()).getTypes();
 makeUnmodifiable(types.rawTypes())
   .containsExactly(Interface1.class, Interface2.class, Interface3.class, Iterable.class);
}

代码示例来源:origin: google/guava

public <A extends Iterable<? extends String>, B extends A> void testSerializable() {
 reserialize(TypeToken.of(String.class));
 reserialize(TypeToken.of(String.class).getTypes());
 reserialize(TypeToken.of(String.class).getTypes().classes());
 reserialize(TypeToken.of(String.class).getTypes().interfaces());
 reserialize(TypeToken.of(String.class).getTypes().rawTypes());
 reserialize(TypeToken.of(String.class).getTypes().classes().rawTypes());
 reserialize(TypeToken.of(String.class).getTypes().interfaces().rawTypes());
 reserialize(new TypeToken<int[]>() {});
 reserialize(new TypeToken<Map<String, Integer>>() {});
 reserialize(new IKnowMyType<Map<? super String, ? extends int[]>>() {}.type());
 reserialize(TypeToken.of(new TypeCapture<B>() {}.capture()).getTypes().rawTypes());
 try {
  SerializableTester.reserialize(TypeToken.of(new TypeCapture<B>() {}.capture()));
  fail();
 } catch (RuntimeException expected) {
 }
}

代码示例来源:origin: google/guava

public <A extends Class1 & Interface1, B extends A>
  void testGetTypes_ignoresTypeVariablesByDefault() {
 TypeToken<?>.TypeSet types = TypeToken.of(new TypeCapture<B>() {}.capture()).getTypes();
 makeUnmodifiable(types)
   .containsExactly(
     TypeToken.of(Interface1.class), TypeToken.of(Class1.class), TypeToken.of(Object.class));
 assertSubtypeFirst(types);
 makeUnmodifiable(types.interfaces()).containsExactly(TypeToken.of(Interface1.class));
 makeUnmodifiable(types.classes())
   .containsExactly(TypeToken.of(Class1.class), TypeToken.of(Object.class))
   .inOrder();
}

代码示例来源:origin: google/guava

public void testGetTypes_fromPrimitive() {
 TypeToken<Integer>.TypeSet types = TypeToken.of(int.class).getTypes();
 assertThat(types).contains(TypeToken.of(int.class));
 assertThat(types.rawTypes()).contains(int.class);
 assertThat(types.interfaces()).isEmpty();
 assertThat(types.interfaces().rawTypes()).isEmpty();
 assertThat(types.classes()).contains(TypeToken.of(int.class));
 assertThat(types.classes().rawTypes()).contains(int.class);
}

代码示例来源:origin: google/guava

public <A extends Class1 & Interface1, B extends A>
  void testGetTypes_rawTypes_ignoresTypeVariablesByDefault() {
 TypeToken<?>.TypeSet types = TypeToken.of(new TypeCapture<B>() {}.capture()).getTypes();
 makeUnmodifiable(types.rawTypes())
   .containsExactly(Interface1.class, Class1.class, Object.class);
 makeUnmodifiable(types.interfaces().rawTypes()).containsExactly(Interface1.class);
 makeUnmodifiable(types.classes().rawTypes())
   .containsExactly(Class1.class, Object.class)
   .inOrder();
}

相关文章

微信公众号

最新文章

更多

TypeToken类方法