首页 > 解决方案 > 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不“猜测”的情况下找到它?

标签: javaguava

解决方案


Iterables是一个实用类(它只包含静态方法,不能被实例化)。

我猜你com.google.common.collect.Iterables$4只是一些匿名类实现java.lang.Iterable

总而言之,定义componentsasIterable应该适合你。


编辑:回答您的后续问题:

1)你写道它看起来不像Iterablesimplements Iterable,你是对的 - 它不是。为了理解什么com.google.common.collect.Iterables$4意思,你需要了解匿名类编译命名规则

简而言之,com.google.common.collect.Iterables$4意思是“嵌套在类中的第四个匿名com.google.common.collect.Iterables类”。

2) 至于如何在不猜测的情况下找出类型 - 您只需跟踪 API 及其返回的内容:

注意上面都是接口,所以我们还是看不到返回Iterable<Component>是怎么实现的。

要了解这一点,我们需要查看实现:StorageTxImpl. 然而,这进一步委托了调用,我不想进一步跟踪它(如果你愿意,你可以自己做;不过,如果在 IDE 中打开这个项目会容易得多) .

但是,我知道那里发生的情况是调用了 GuavaIterables实用程序类中的方法之一,然后返回Iterable<T>通过匿名类实现的方法,仅此而已。


推荐阅读