java - 用于 ImmutableSet 和 java.lang.Iterable 的 TypeMirror isSubtype 和 isAssignable
问题描述
我无法理解子类型和可分配之间的区别。
给定参数是 ImmutableSet 类型,我有以下代码来检查 ImmutableSet 是子类型还是可分配的:
boolean isAssignable =
types.isAssignable(
types.erasure(parameters.asType()),
elements.getTypeElement(Iterable.class.getCanonicalName()).asType());
boolean isSubtype =
types.isSubtype(
types.erasure(parameters.asType()),
elements.getTypeElement(Iterable.class.getCanonicalName()).asType());
processingEnv.getMessager().printMessage(Kind.NOTE, "Type " + types.erasure(parameters.asType())
+ " is " + (isSubtype ? "" : " not ") + " a subtype of " + Iterable.class.getCanonicalName());
processingEnv.getMessager().printMessage(Kind.NOTE, "Type " + types.erasure(parameters.asType())
+ " is " + (isAssignable ? "" : " not ") + " a assignable of " + Iterable.class.getCanonicalName());
输出如下:
Type com.google.common.collect.ImmutableSet is not a subtype of java.lang.Iterable
Type com.google.common.collect.ImmutableSet is a assignable of java.lang.Iterable
我可以理解为什么 ImmutableSet 可以分配给 Iterable 接口,这很清楚。但是,我不明白为什么 ImmutableSet 不是 Iterable 的子类型。
我检查了其他类似的问题,但没有一个解释过这一点。任何人都可以帮忙吗?
解决方案
推荐阅读
- python - 在维护索引信息的同时迭代过滤的 Numpy 数组
- grafana - 使用 influx config 将空手道加特林数据写入监听不同端口的不同数据库
- angular - 以 Kendo UI 作为其依赖项发布 NPM 包的正确方法是什么?
- rdf - 如何绘制 RDF 设计图
- php - 使用 Drupal 7 更新数据库中是否已存在
- angular - 可折叠功能在 angular8 中不起作用
- java - Android Studio 按钮将我带到错误的活动
- git - 如何使用命令行遍历所有文件夹从 Github 下载特定文件?
- postgresql - odoo.service.server:无法加载服务器范围的模块`web`
- scikit-learn - 解释 SVC 和 GridSearch 结果