2013-07-19 95 views
3

我試圖獲取一個類(objectfactory.java)的所有數據對象(變量單獨,而不是其功能),並且調用方法本身就是在同一個包中創建其他類的實例,並且以這種方式列出給定包中所有類中的所有對象。 (把事情放在角度看,這些是由JAXB創建的類)。如何使用Java遍歷類的對象及其成員類

也就是說,我基本上想要做的是,通過迭代,使的所有數據對象的列表:

Objectfactory, and then, 
- Person 
- Name 
- Url 
- Link 
- Personnel 

類。

這裏是ObjectFactory類:

@XmlRegistry 
public class ObjectFactory { 

    private final static QName _Given_QNAME = new QName("", "given"); 
    private final static QName _Email_QNAME = new QName("", "email"); 
    private final static QName _Family_QNAME = new QName("", "family"); 

    /** 
    * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: schema 
    * 
    */ 
    public ObjectFactory() { 
    } 
    public Person createPerson() { 
     return new Person(); 
    } 

    public Name createName() { 
     return new Name(); 
    } 

    public Url createUrl() { 
     return new Url(); 
    } 

    public Link createLink() { 
     return new Link(); 
    } 

    public Personnel createPersonnel() { 
     return new Personnel(); 
    } 

    @XmlElementDecl(namespace = "", name = "given") 
    public JAXBElement<String> createGiven(String value) { 
     return new JAXBElement<String>(_Given_QNAME, String.class, null, value); 
    } 
    @XmlElementDecl(namespace = "", name = "email") 
    public JAXBElement<String> createEmail(String value) { 
     return new JAXBElement<String>(_Email_QNAME, String.class, null, value); 
    } 

    @XmlElementDecl(namespace = "", name = "family") 
    public JAXBElement<String> createFamily(String value) { 
     return new JAXBElement<String>(_Family_QNAME, String.class, null, value); 
    } 

} 

我可以去直接只有等到使用Java反思ObjectFactory的字段和方法(getDeclaredFields())等

但是,對於其他類,我只能手動到達他們的對象。 (對於例如,對於班通)

ObjectFactory factory= new ObjectFactory(); 
Field[] fields = factory.createLink().getClass().getDeclaredFields(); 

     Field[] fields1 = factory.createPerson().getClass().getDeclaredFields(); 

     for (Field f1 : fields1) { 
      System.out.println("field name = " + f1.getName()); 
     } 

,但是,我想在ObjectFactory的所有課程做到這一點在運行時,而不是手動撥打電話就像「createPerson()」。

我試着做這樣的事情;

ObjectFactory factory= new ObjectFactory(); 
    Method[] methods = factory.getClass().getDeclaredMethods(); 
    for (Method m : methods) { 
System.out.println("Class name = " + m.getName()); 
     Field[] subfields = m.getClass().getDeclaredFields(); 


     for (Field sf : subfields) { 
      System.out.println("entities = " + sf.getName()); 
     } 
     System.out.println("\n\n"); 

    } 

但這不起作用。

我的預期產出將是這樣的:

Class name = ObjectFactory 
    field name = _Given_QNAME 
    field name = _Email_QNAME 
    field name = _Family_QNAME 

Class name = Person 
    field name = Name 
    field name = Age 
    field name = Sex 

Class name = Personnel 
    field name = address 
... 

等..

我該怎麼辦呢?

回答

3
public void getAllClassAndFields() { 
    ObjectFactory objectFactory = new ObjectFactory(); 
    Method[] methods = objectFactory.getClass().getDeclaredMethods(); 
    for (Method method : methods) { 
     try { 
      // Check if method have XmlElementDecl annotation 
      XmlElementDecl annotation = method.getAnnotation(XmlElementDecl.java); 
      if (annotation == null) { 
       // Invoke method only if it is not annoatated with XmlElementDecl 
       Object object = method.invoke(objectFactory, new Object[] {}); 
       System.out.println("Class Name = " + object.getClass().getName()); 
       printFileds(object); 
      } 

     } catch (Exception e) { 
      // I used Exception to keep it simple, instead use appropriate exception types here 
     } 
    } 
} 

public static void printFileds(Object obj) { 
    Field[] fields = obj.getClass().getFields(); 
    for (Field field : fields) { 
     System.out.println("Field Name = " + field.getName()); 
    } 
} 
+0

感謝您的代碼!它像一個魅力一樣工作! :-) – VictorCreator

+0

@Vijairam:很高興能幫到你! – xyz

0

不知道你爲什麼要這麼做?
但嘗試下面

public class RelectionTestCases { 
public static final String INDENT = " "; 

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 
    ObjectFactory factory = new ObjectFactory(); 

    Field[] fields = ObjectFactory.class.getDeclaredFields(); 

    // Fields 
    System.out.println(ObjectFactory.class.getName()); 
    for (Field field : fields) { 
     System.out.println(INDENT + field.getName()); 
    } 

    // Method 
    Method[] methods = ObjectFactory.class.getDeclaredMethods(); 
    for (Method method : methods) { 

     if (method.getReturnType().equals(JAXBElement.class)) { 
      continue; 
     } 

     Object object = method.invoke(factory, null); 
     System.out.println(object.getClass().getName()); 

     Field[] subFields = object.getClass().getDeclaredFields(); 

     for (Field field : subFields) { 
      System.out.println(INDENT + field.getName()); 
     } 
    } 
} 

}

+0

感謝您的回覆。真的幫助我!但是,我使用了上面的響應,因爲它更適合我的需求! :) – VictorCreator

+0

我的代碼是否工作?我沒有徹底測試 – Seabook