I have this groovy script that is executed on 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));
When that is run I get:
- - - 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
Why is components
not an instance of com.google.common.collect.Iterables
?
I would expect that I could do:
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();
}
But that gives the error:
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'
How do I strongly type the variable:
def components = null;
?
When I look at the java docs: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html
its does not look like com.google.common.collect.Iterables
is a sub class of java.lang.Iterable
Based on below answer I can do:
Iterable<Component> components = null;
But how do I find that com.google.common.collect.Iterables$4
without "guessing"?