java - com.google.common.collect.Iterables 和实例类型
问题描述
我有这个在 nexus 上执行的 groovy 脚本:
def retentionDays = 30;
def retentionCount = 10;
def repositoryName = 'maven-releases';
def whitelist = ["org.javaee7.sample/javaee7-simple-sample", "org.javaee7.next/javaee7-another-sample"].toArray();
log.info(":::Cleanup script started!");
MaintenanceService service = container.lookup("org.sonatype.nexus.repository.maintenance.MaintenanceService");
def repo = repository.repositoryManager.get(repositoryName);
def tx = repo.facet(StorageFacet.class).txSupplier().get();
def components = null;
try {
tx.begin();
components = tx.browseComponents(tx.findBucket(repo));
}catch(Exception e){
log.info("Error: "+e);
}finally{
if(tx!=null)
tx.close();
}
log.info(" - - A - - Type of 'components.getClass().getName()' is: " + components.getClass().getName());
log.info(" - - B - - Type of 'components' is: " + components);
log.info(" - - C - - Type of 'components.getClass()' is: " + components.getClass());
log.info(" - - D - - Type of 'components[0].getClass()' is: " + components[0].getClass());
log.info(" - - components instanceof com.google.common.collect.Iterables = " + (components instanceof com.google.common.collect.Iterables));
当它运行时,我得到:
- - - A - - Type of 'components.getClass().getName()' is: com.google.common.collect.Iterables$4
- - - B - - Type of 'components' is: []
- - - C - - Type of 'components.getClass()' is: class com.google.common.collect.Iterables$4
- - - components instanceof com.google.common.collect.Iterables = false
- - - D - - Type of 'components[0].getClass()' is: class org.codehaus.groovy.runtime.NullObject
为什么components
不是一个实例com.google.common.collect.Iterables
?
我希望我能做到:
import com.google.common.collect.Iterables
Iterables components = null;
try {
tx.begin();
components = tx.browseComponents(tx.findBucket(repo));
}catch(Exception e){
log.info("Error: "+e);
}finally{
if(tx!=null)
tx.close();
}
但这给出了错误:
Error: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' with class 'com.google.common.collect.Iterables$4' to class 'com.google.common.collect.Iterables'
如何强输入变量:
def components = null;
?
当我查看 Java 文档时: https ://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html
它看起来不像com.google.common.collect.Iterables
是一个子类java.lang.Iterable
根据以下答案,我可以做到:
Iterable<Component> components = null;
但是我如何在com.google.common.collect.Iterables$4
不“猜测”的情况下找到它?
解决方案
Iterables
是一个实用类(它只包含静态方法,不能被实例化)。
我猜你com.google.common.collect.Iterables$4
只是一些匿名类实现java.lang.Iterable
。
总而言之,定义components
asIterable
应该适合你。
编辑:回答您的后续问题:
1)你写道它看起来不像Iterables
implements Iterable
,你是对的 - 它不是。为了理解什么com.google.common.collect.Iterables$4
意思,你需要了解匿名类的编译命名规则。
简而言之,com.google.common.collect.Iterables$4
意思是“嵌套在类中的第四个匿名com.google.common.collect.Iterables
类”。
2) 至于如何在不猜测的情况下找出类型 - 您只需跟踪 API 及其返回的内容:
Repository.facet
返回Facet
给定类型的 a (这里:StorageFacet
)StorageFacet.txSupplier
返回Supplier<StorageTx>
StorageTx.browseComponents
返回Iterable<Component>
注意上面都是接口,所以我们还是看不到返回Iterable<Component>
是怎么实现的。
要了解这一点,我们需要查看实现:StorageTxImpl
. 然而,这进一步委托了调用,我不想进一步跟踪它(如果你愿意,你可以自己做;不过,如果在 IDE 中打开这个项目会容易得多) .
但是,我知道那里发生的情况是调用了 GuavaIterables
实用程序类中的方法之一,然后返回Iterable<T>
通过匿名类实现的方法,仅此而已。
推荐阅读
- python - jupyter notebook 上的交互式绘图
- ios - 如何增加 UIActivityViewController 中的文本空间?
- python - 如何使用 Tweepy 和 Python 取消关注 5 个用户
- mongodb - Testcontainer 不适用于 Kotlin 和 Quarkus - 连接到服务器 mongo:27017 时监视器线程中出现异常
- java - GCP中setApplicationName()的意义是什么?
- forms - 如何从同一表格中的先前数据中删除在线表格中的自动条目?
- python - 转换字符串 tz 以添加到本地化的日期时间对象
- r - 如何将数据帧从宽格式转换为具有多列r的长格式
- flutter - 如何在抖音或 instagram 等 Flutter 中应用视频滤镜?
- ruby - 如何存储脚本中的计数?水豚/罗比