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

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

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

TypeToken.is介绍

[英]A.is(B) is defined as Foo[.isSubtypeOf(Foo).

Specifically, returns true if any of the following conditions is met:

  1. 'this' and formalType are equal.
  2. 'this' and formalType have equal canonical form.
  3. formalType is and 'this' is a subtype of Foo.
  4. formalType is and 'this' is a supertype of Foo.
    Note that condition 2 isn't technically accurate under the context of a recursively bounded type variables. For example, Enum> canonicalizes to Enum where E is the type variable declared on the Enum class declaration. It's technically not true that Foo>> is a subtype of Foo> according to JLS. See testRecursiveWildcardSubtypeBug() for a real example.

It appears that properly handling recursive type bounds in the presence of implicit type bounds is not easy. For now we punt, hoping that this defect should rarely cause issues in real code.

](https://www.tabnine.com/code/java/methods/com.google.common.reflect.TypeToken/is#)
[中]A.is(B)被定义为Foo[.isSubtypeOf(Foo).

Specifically, returns true if any of the following conditions is met:

  1. 'this' and formalType are equal.
  2. 'this' and formalType have equal canonical form.
  3. formalType is and 'this' is a subtype of Foo.
  4. formalType is and 'this' is a supertype of Foo.
    Note that condition 2 isn't technically accurate under the context of a recursively bounded type variables. For example, Enum> canonicalizes to Enum where E is the type variable declared on the Enum class declaration. It's technically not true that Foo>> is a subtype of Foo> according to JLS. See testRecursiveWildcardSubtypeBug() for a real example.

It appears that properly handling recursive type bounds in the presence of implicit type bounds is not easy. For now we punt, hoping that this defect should rarely cause issues in real code.

](https://www.tabnine.com/code/java/methods/com.google.common.reflect.TypeToken/is#)

代码示例

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

private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) {
 Class<?> matchedClass = of(supertype).getRawType();
 if (!someRawTypeIsSubclassOf(matchedClass)) {
  return false;
 }
 TypeVariable<?>[] typeVars = matchedClass.getTypeParameters();
 Type[] supertypeArgs = supertype.getActualTypeArguments();
 for (int i = 0; i < typeVars.length; i++) {
  Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]);
  // If 'supertype' is "List<? extends CharSequence>"
  // and 'this' is StringArrayList,
  // First step is to figure out StringArrayList "is-a" List<E> where <E> = String.
  // String is then matched against <? extends CharSequence>, the supertypeArgs[0].
  if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) {
   return false;
  }
 }
 // We only care about the case when the supertype is a non-static inner class
 // in which case we need to make sure the subclass's owner type is a subtype of the
 // supertype's owner.
 return Modifier.isStatic(((Class<?>) supertype.getRawType()).getModifiers())
   || supertype.getOwnerType() == null
   || isOwnedBySubtypeOf(supertype.getOwnerType());
}

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

private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) {
 Class<?> matchedClass = of(supertype).getRawType();
 if (!someRawTypeIsSubclassOf(matchedClass)) {
  return false;
 }
 TypeVariable<?>[] typeVars = matchedClass.getTypeParameters();
 Type[] supertypeArgs = supertype.getActualTypeArguments();
 for (int i = 0; i < typeVars.length; i++) {
  Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]);
  // If 'supertype' is "List<? extends CharSequence>"
  // and 'this' is StringArrayList,
  // First step is to figure out StringArrayList "is-a" List<E> where <E> = String.
  // String is then matched against <? extends CharSequence>, the supertypeArgs[0].
  if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) {
   return false;
  }
 }
 // We only care about the case when the supertype is a non-static inner class
 // in which case we need to make sure the subclass's owner type is a subtype of the
 // supertype's owner.
 return Modifier.isStatic(((Class<?>) supertype.getRawType()).getModifiers())
   || supertype.getOwnerType() == null
   || isOwnedBySubtypeOf(supertype.getOwnerType());
}

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

private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) {
 Class<?> matchedClass = of(supertype).getRawType();
 if (!someRawTypeIsSubclassOf(matchedClass)) {
  return false;
 }
 TypeVariable<?>[] typeVars = matchedClass.getTypeParameters();
 Type[] supertypeArgs = supertype.getActualTypeArguments();
 for (int i = 0; i < typeVars.length; i++) {
  Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]);
  // If 'supertype' is "List<? extends CharSequence>"
  // and 'this' is StringArrayList,
  // First step is to figure out StringArrayList "is-a" List<E> where <E> = String.
  // String is then matched against <? extends CharSequence>, the supertypeArgs[0].
  if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) {
   return false;
  }
 }
 // We only care about the case when the supertype is a non-static inner class
 // in which case we need to make sure the subclass's owner type is a subtype of the
 // supertype's owner.
 return Modifier.isStatic(((Class<?>) supertype.getRawType()).getModifiers())
   || supertype.getOwnerType() == null
   || isOwnedBySubtypeOf(supertype.getOwnerType());
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) {
 Class<?> matchedClass = of(supertype).getRawType();
 if (!someRawTypeIsSubclassOf(matchedClass)) {
  return false;
 }
 TypeVariable<?>[] typeVars = matchedClass.getTypeParameters();
 Type[] supertypeArgs = supertype.getActualTypeArguments();
 for (int i = 0; i < typeVars.length; i++) {
  Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]);
  // If 'supertype' is "List<? extends CharSequence>"
  // and 'this' is StringArrayList,
  // First step is to figure out StringArrayList "is-a" List<E> where <E> = String.
  // String is then matched against <? extends CharSequence>, the supertypeArgs[0].
  if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) {
   return false;
  }
 }
 // We only care about the case when the supertype is a non-static inner class
 // in which case we need to make sure the subclass's owner type is a subtype of the
 // supertype's owner.
 return Modifier.isStatic(((Class<?>) supertype.getRawType()).getModifiers())
   || supertype.getOwnerType() == null
   || isOwnedBySubtypeOf(supertype.getOwnerType());
}

代码示例来源:origin: org.kill-bill.billing/killbill-platform-osgi-bundles-logger

private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) {
 Class<?> matchedClass = of(supertype).getRawType();
 if (!someRawTypeIsSubclassOf(matchedClass)) {
  return false;
 }
 TypeVariable<?>[] typeVars = matchedClass.getTypeParameters();
 Type[] supertypeArgs = supertype.getActualTypeArguments();
 for (int i = 0; i < typeVars.length; i++) {
  Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]);
  // If 'supertype' is "List<? extends CharSequence>"
  // and 'this' is StringArrayList,
  // First step is to figure out StringArrayList "is-a" List<E> where <E> = String.
  // String is then matched against <? extends CharSequence>, the supertypeArgs[0].
  if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) {
   return false;
  }
 }
 // We only care about the case when the supertype is a non-static inner class
 // in which case we need to make sure the subclass's owner type is a subtype of the
 // supertype's owner.
 return Modifier.isStatic(((Class<?>) supertype.getRawType()).getModifiers())
   || supertype.getOwnerType() == null
   || isOwnedBySubtypeOf(supertype.getOwnerType());
}

相关文章

微信公众号

最新文章

更多

TypeToken类方法