com.google.common.collect.Iterables和实例类型 [英] com.google.common.collect.Iterables and the instance type

查看:162
本文介绍了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.Iterablesjava.lang.Iterable的子类

根据下面的答案,我可以做:

Iterable<Component> components = null;

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

解决方案

java.lang.Iterable .

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


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

1)您写道,它看起来不像 Iterable ,您说得对-事实并非如此.为了了解com.google.common.collect.Iterables$4的含义,您需要了解编译命名规则. /docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html"rel =" nofollow noreferrer>匿名类.

简而言之,com.google.common.collect.Iterables$4表示嵌套在com.google.common.collect.Iterables类中的第四个匿名类".

2)关于如何找到类型而不进行猜测-您只需跟踪API及其返回的内容即可:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆