2017-10-05 52 views
0

我需要記錄每個對象的值。對象的類型可能每次都有所不同,我試圖用反射調用類的getters。但我卡在一個地方,我需要重新調用readData方法,如果類是一個自定義對象。如何讓對象在下面else塊的readData(obj)中傳遞。如何使用反射從下面的片段中檢索值

private static void readData(Object resp) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
    Method[] allMethods = resp.getClass().getDeclaredMethods(); 
    for (Method m : allMethods) { 
     if ("get".equalsIgnoreCase(m.getName().substring(0, 3))) { 
      Class<?> type = m.getReturnType(); 
      if (isWrapperType(type) || type.isPrimitive()) { 
       System.out.println(m.invoke(resp)) ; 
      } 
      else if(Collection.class.isAssignableFrom(type)) { 
       if(m.getGenericReturnType() instanceof ParameterizedType){ 
        ParameterizedType paramType = (ParameterizedType) m.getGenericReturnType(); 
        System.out.println("List is of type "+(Class<?>) paramType.getActualTypeArguments()[0]); 
       } 
       //iterate the object and recall read data with generic type of collection 

      } 
      else{ 
       //Problem : need to pass object from type, how do i get this class object, as it should not be any new instance 
       readData(obj); 
      } 

     } 

    } 

} 
private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes(); 

public static boolean isWrapperType(Class<?> clazz) 
{ 
    return WRAPPER_TYPES.contains(clazz); 
} 

private static Set<Class<?>> getWrapperTypes() 
{ 
    Set<Class<?>> ret = new HashSet<Class<?>>(); 
    ret.add(Boolean.class); 
    ret.add(Character.class); 
    ret.add(Byte.class); 
    ret.add(Short.class); 
    ret.add(Integer.class); 
    ret.add(Long.class); 
    ret.add(Float.class); 
    ret.add(Double.class); 
    ret.add(String.class); 
    ret.add(BigDecimal.class); 
    ret.add(Number.class); 
    return ret; 
} 

這是怎麼BO的樣子

Response.java

public class Response { 
    List<OrderStatusList> orderStatusList; 
    StatusResponse  response; 

    //getter-setter 
} 

StatusResponse.java

public class StatusResponse { 
    protected String type; 
    protected String message; 

    // getter-setter 
} 

OrderStatusList.java

public class OrderStatusList { 
    Header header; 
    // getter - setter 
} 

Header.java

public class Header { 
    protected String orderNumber; 
    protected String orderStatus; 
    protected List<DtOrderStatusResponseList> item; 

    //getter-setter 
} 

DtOrderStatusResponseList.java

public class DtOrderStatusResponseList { 
    protected String orderItemNumber; 
    protected String orderItemMaterialNumber; 
    protected String orderItemRequestedQuantity; 
    protected String orderItemStatus; 

    //getter-setter 
} 
+0

避免記錄[PII](https://en.wikipedia.org/wiki/Personally_identifiable_information),或者至少在真正需要時掩蓋它。 –

+0

爲了理解你的問題,你需要一些解決方案來避免這些多個getter和setter?這是正確的,我明白了嗎? – procrastinator

+0

不,我只需要讀取這些對象中的值,但由於我有大量的類,我將不得不手動對這些對象執行get操作,並且如果它是我需要迭代並執行get操作的列表。我想避免爲每個類做同樣的事情,而是尋找一種解決方案來執行這些獲取方法,而不考慮對象類型 –

回答

0

既然你只需要登錄的價值觀,而不是用它們覆蓋對象#toString方法在所有擁有你想要的信息的類中。 通過這種方法,您可以在一行中有效地獲取每個對象的信息。

例如

public class SOFTest { 
    privat int age, weight, height; 
    private Header header; 

    //Constructor etc. 

    @Overwrite 
    public String toString() { 
     return "SOFTest(" + String.format("%s, %s, %s %s)", age, weight, height, header.toString())); 
    } 
} 
+0

但在這種情況下,我將不得不去每個類並重寫toString(),並且我有更多200個類,每個類擁有大量的數據成員,並擁有一個自定義對象。 我能想到的最佳解決方案是使用反射來編寫一個方法,該方法可以作爲通過任何對象的常見方法 –

+1

在您的代碼片段中,您只有3個類! 查看本教程:https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html – Dinh

+0

感謝本教程,它真的很有幫助 –

0

我需要吸氣劑援引在READDATA()自定義類的對象,所以通過method.invoke(RESP)。它會是這樣的:

private static void readData(Object resp) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
Method[] allMethods = resp.getClass().getDeclaredMethods(); 
for (Method m : allMethods) { 
    if ("get".equalsIgnoreCase(m.getName().substring(0, 3))) { 
     Class<?> type = m.getReturnType(); 
     if (isWrapperType(type) || type.isPrimitive()) { 
      System.out.println(m.invoke(resp)) ; 
     } 
     else if(Collection.class.isAssignableFrom(type)) { 
      if(m.getGenericReturnType() instanceof ParameterizedType){ 
       ParameterizedType paramType = (ParameterizedType) m.getGenericReturnType(); 
       System.out.println("List is of type "+(Class<?>) paramType.getActualTypeArguments()[0]); 
      } 
     } 
     else{ 
      //Solution : need to invoke the getter to get the object and it would work 
      readData(method.invoke(resp)); 
     } 

    } 
    } 
} 
相關問題