【问题标题】:com.google.common.collect.Iterables and the instance typecom.google.common.collect.Iterables 和实例类型
【发布时间】:2018-08-01 19:50:15
【问题描述】:

我有这个在 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.Iterablesjava.lang.Iterable 的子类

根据以下答案,我可以做到:

Iterable<Component> components = null;

但是我如何在没有“猜测”的情况下找到com.google.common.collect.Iterables$4

【问题讨论】:

    标签: java guava


    【解决方案1】:

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

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

    总而言之,将components 定义为Iterable 应该适合您。


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

    1) 你写道它看起来不像 Iterables 实现 Iterable,你是对的 - 它没有。要了解com.google.common.collect.Iterables$4 的含义,您需要了解anonymous classescompilation naming rules

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

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

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

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

    但是,我知道那里会发生对 Guava 的 Iterables 实用程序类中的方法之一的调用,该方法又返回通过匿名类实现的 Iterable&lt;T&gt;,仅此而已。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 2016-01-06
      • 1970-01-01
      • 2012-04-10
      • 2017-01-14
      • 2017-09-18
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多