com.google.common.collect.Iterables 和实例类型
com.google.common.collect.Iterables and the instance type
我有这个 groovy 在 nexus 上执行的脚本:
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
- - - B - - Type of 'components' is: []
- - - C - - Type of 'components.getClass()' is: class com.google.common.collect.Iterables
- - - 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' 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
的子 class
根据下面的答案我可以做到:
Iterable<Component> components = null;
但是我如何找到没有 "guessing" 的 com.google.common.collect.Iterables
?
Iterables
是一个实用程序class(它只包含静态方法,不能实例化)。
我猜你的 com.google.common.collect.Iterables
只是一些匿名的 class 实现 java.lang.Iterable
.
总而言之,将 components
定义为 Iterable
应该适合您。
编辑:回答您的后续问题:
1) 你写道它看起来不像 Iterables
实现了 Iterable
,你是对的 - 它没有。为了理解com.google.common.collect.Iterables
是什么意思你需要理解编译命名规则 of anonymous classes.
简而言之,com.google.common.collect.Iterables
表示“嵌套在 com.google.common.collect.Iterables
class 中的第 4 个匿名 class”。
2) 至于如何在不猜测的情况下找出类型 - 您只需跟踪 API 及其内容 returns:
Repository.facet
returns 给定类型的 Facet
(此处:StorageFacet
)
StorageFacet.txSupplier
returns Supplier<StorageTx>
StorageTx.browseComponents
returns Iterable<Component>
注意上面都是接口,所以从这里还是看不出返回的Iterable<Component>
是如何实现的
要了解这一点,我们需要查看实现:StorageTxImpl
。然而,这进一步委托了调用,我不想再进一步跟踪它(如果你愿意,你可以自己做;如果在 IDE, 虽然).
但是,我知道那里发生的事情是调用 Guava 的 Iterables
实用程序 class 中的方法之一,然后 returns Iterable<T>
通过匿名实现class,仅此而已。
我有这个 groovy 在 nexus 上执行的脚本:
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
- - - B - - Type of 'components' is: []
- - - C - - Type of 'components.getClass()' is: class com.google.common.collect.Iterables
- - - 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' 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;
但是我如何找到没有 "guessing" 的 com.google.common.collect.Iterables
?
Iterables
是一个实用程序class(它只包含静态方法,不能实例化)。
我猜你的 com.google.common.collect.Iterables
只是一些匿名的 class 实现 java.lang.Iterable
.
总而言之,将 components
定义为 Iterable
应该适合您。
编辑:回答您的后续问题:
1) 你写道它看起来不像 Iterables
实现了 Iterable
,你是对的 - 它没有。为了理解com.google.common.collect.Iterables
是什么意思你需要理解编译命名规则 of anonymous classes.
简而言之,com.google.common.collect.Iterables
表示“嵌套在 com.google.common.collect.Iterables
class 中的第 4 个匿名 class”。
2) 至于如何在不猜测的情况下找出类型 - 您只需跟踪 API 及其内容 returns:
Repository.facet
returns 给定类型的Facet
(此处:StorageFacet
)StorageFacet.txSupplier
returnsSupplier<StorageTx>
StorageTx.browseComponents
returnsIterable<Component>
注意上面都是接口,所以从这里还是看不出返回的Iterable<Component>
是如何实现的
要了解这一点,我们需要查看实现:StorageTxImpl
。然而,这进一步委托了调用,我不想再进一步跟踪它(如果你愿意,你可以自己做;如果在 IDE, 虽然).
但是,我知道那里发生的事情是调用 Guava 的 Iterables
实用程序 class 中的方法之一,然后 returns Iterable<T>
通过匿名实现class,仅此而已。