字符串不能转换为Iterable错误?

时间:2015-07-23 18:25:11

标签: maven groovy iterator guava

所以我试图浏览一个groovyObject的字段并获得该字段的属性。所以这就是我所得到的(对不起它有点粗糙,所以清理会受到赞赏但不是必要的,我还做了一些调试和其他东西与日志,什么不是。):

public void traverse(final GroovyObject groovy) throws RepositoryException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException 
{       
    Field[] theFields = groovy.getClass().getDeclaredFields();
    final ArrayList<Field> fields = new ArrayList<Field>();
    int count =0;
    for(Field field : theFields)
    {
        fields.add(field);
        LOG.error("{} = {}",field.getName(), groovy.getProperty(field.getName()));  

    }



//this is the guava tree traverser      
TreeTraverser<GroovyObject> traverser = new TreeTraverser<GroovyObject>()
        {

        @Override
        public Iterable<GroovyObject> children(GroovyObject root)
        {               

            return (Iterable<GroovyObject>)root.getProperty(fields.get(0).getName());
 //|-->Here I get the String cannot be cast to Iterable. Which I find odd since it is still an object just getProperty takes a string. right?

        }

    };

对此的想法?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

GroovyObject.getProperty(String)检索给定属性的值。如果该值恰好是String,则无法将其转换为Iterable

如果调整日志语句,则可以检查字段的类型:

LOG.error("{} of type {} = {}", field.getName(), field.getType(), groovy.getProperty(field.getName()));  

答案 1 :(得分:0)

所以我认为它超越了。基本上需要发生的是我需要创建两个迭代器:一个用于groovy对象,一个用于属性字符串,因此最终目标看起来像

groovyObject.iterate().next().getProperty(string.iterate().next());

或类似的东西,当我想出来时,我会更新它。!

一旦我这样做,我就可以回过头来考虑提高效率

相关问题