Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java com.google.common.collect.Iterables和实例类型_Java_Guava - Fatal编程技术网

Java com.google.common.collect.Iterables和实例类型

Java com.google.common.collect.Iterables和实例类型,java,guava,Java,Guava,我有一个在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 s

我有一个在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文档时:

它看起来不像
com.google.common.collect.Iterables
java.lang.Iterable

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

Iterable<Component> components = null;
Iterable components=null;
但是我如何才能在没有“猜测”的情况下找到
com.google.common.collect.Iterables$4

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

我猜你的
com.google.common.collect.Iterables$4
只是一个匿名类实现的

总之,将
组件
定义为
Iterable
应该适合您


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

1) 你写道它看起来不像是工具,你是对的——它不是。为了理解
com.google.common.collect.Iterables$4
意味着什么,您需要了解的是

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

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

  • 返回给定类型的
    Facet
    (此处:
    StorageFacet
  • 返回
    供应商
  • 返回
    Iterable
请注意,以上都是接口,因此我们仍然不知道返回的
Iterable
是如何实现的

要了解这一点,我们需要了解实现:。但是,这会进一步委托调用,我不想再进一步跟踪它(如果您愿意,您可以自己完成;但是,如果您在IDE中打开此项目,这将更容易完成)

然而,我知道在那里发生的事情是,对Guava的
Iterables
实用程序类中的一个方法进行调用,该类反过来返回通过匿名类实现的
Iterable
,仅此而已。

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

我猜你的
com.google.common.collect.Iterables$4
只是一个匿名类实现的

总之,将
组件
定义为
Iterable
应该适合您


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

1) 你写道它看起来不像是工具,你是对的——它不是。为了理解
com.google.common.collect.Iterables$4
意味着什么,您需要了解的是

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

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

  • 返回给定类型的
    Facet
    (此处:
    StorageFacet
  • 返回
    供应商
  • 返回
    Iterable
请注意,以上都是接口,因此我们仍然不知道返回的
Iterable
是如何实现的

要了解这一点,我们需要了解实现:。但是,这会进一步委托调用,我不想再进一步跟踪它(如果您愿意,您可以自己完成;但是,如果您在IDE中打开此项目,这将更容易完成)

但是,我知道会发生什么,对Guava的
Iterables
实用程序类中的一个方法进行调用,然后返回通过匿名类实现的
Iterable
,仅此而已