samedi 16 janvier 2016

Array property version is not printed

I have a class which has various nested fields including a list of Edition objects. I want to collect all the names of the fields including the 'version' field of the Edition object but it is not being printed. All the fields of the Publication object are printed including the array property, but it's nested property version is not printed: Here is the code I have:

def getAllFields(Object someObject){

            if(!someObject.getClass().isArray()){
                 def fields = someObject.getClass().getDeclaredFields();
                 for(Field field in fields){
                     if(!Modifier.isPublic(field.modifiers)){
                         field.setAccessible(true)
                     }
                     println "Name is ${field.name}"
                     Class fieldType=field.getType()
                     Object child=field.get(someObject)
                     println "Class child is ${child.class}"
                     if(child.getClass().isArray()){
                        Class clzz=someObject.getClass().getComponentType()
                        int l=Array.getLength(someObject)
                        def arrField=Array.get(someOject,0)
                         println "Field name is $arrField[0].name}" 
                        getAllFields(Array.get(someOject,0))
                    }
                    // getAllFields(child)
                 }
            }
            else{
                Class clzz=someObject.getClass().getComponentType()
                int l=Array.getLength(someObject)
                def arrField=Array.get(someOject,0)
                 println "Field name is $arrField[0].name}" 
                getAllFields(Array.get(someOject,0))

            }

    }


    class Edition {
        String version

    }

    class Publication{
       List<Edition> editions=[]
       String editor
       String date    

    }
    Edition e=new Edition(version:'1.3')
    def  ll=[]
    ll<<e
    Publication pub=new Publication(editions:ll,editor: 'Sams',date :'04/09/2016')
    getAllFields(pub)

Output is :
Name is editions
Class child is class java.util.ArrayList
Name is editor
Class child is class java.lang.String Name is date
Class child is class java.lang.String
Name is $staticClassInfo
Class child is class org.codehaus.groovy.reflection.ClassInfo
Name is __$stMC
Class child is class java.lang.Boolean
Name is metaClass
Class child is class groovy.lang.MetaClassImpl
Name is $callSiteArray
Class child is class java.lang.ref.SoftReference





Aucun commentaire:

Enregistrer un commentaire