2015-06-19 80 views
2

我已經有了這一點代碼來從動態類中獲取setter方法。但參數可以是java.lang.Stringjava.lang.Long。我如何動態獲取參數類型?獲取參數使用反射的動態方法的類型

public Method getDynMethod(Class aClass, String methodName) { 
    Method m = null; 
    Class[] paramTypes = new Class[1]; 
    paramTypes[0] = String.class; 
    try { 
     m = aClass.getMethod(methodName, paramTypes); 
    } catch (NoSuchMethodException nsme) { 
     nsme.printStackTrace(); 
    } 
    return m; 
} 

這是調用它

Class c = getDynClass(a.getAssetType().getDBTableName()); 
     for (Long l : map.keySet()) { 
      AssetProperties ap = new AssetProperties(); 
      ap.setAssetTypeProperties(em.find(AssetTypeProperty.class, l)); 
      ap.setAssets(a); 
      ap.setValue(map.get(l)); 
      a.getAssetProperties().add(ap); 
      String methodName = "set" + ap.getAssetTypeProperties().getDBColumn(); 
      Method m = getDynMethod(c, methodName); 
      try { 
       String result = (String) m.invoke(c.newInstance(), ap.getValue()); 
       System.out.println(result); 
      } catch (IllegalAccessException iae) { 
       iae.printStackTrace(); 
      } catch (InvocationTargetException ite) { 
       ite.printStackTrace(); 
      } catch (InstantiationException ie) { 
       ie.printStackTrace(); 
      } 

     } 

我另一個參數傳遞給方法,但我仍然不知道什麼參數類型將

+0

你可以把它作爲另一個參數傳遞嗎?'getDynMethod(Class,String,Class [])' –

+0

你能否詳細說明嗎? – geddamsatish

回答

0

你可以得到所有的方法,和過濾的名字,像這樣:

public Method getDynMethod(Class aClass, String methodName) { 
    for (Method m : aClass.getMethods()) { 
     if (methodName.equals(m.getName())) { 
      Class<?>[] params = m.getParameterTypes(); 
      if (params.length == 1 
       && (params[0] == Long.class || params[0] == String.class)) { 
       return m; 
      } 
     } 
    } 

    return null; 
} 
+0

謝謝。這工作。我現在有另一個問題。我在這一行上得到一個空指針 String result =(String)m.invoke(c.newInstance(),ap.getValue()); – Ryan

+0

然後我想是時候使用交互式調試器並放置一個斷點。沒有像調試器這樣的工具來幫助您理解正在發生的事情,尤其是對於空引用。 – jurgenv

2

你可以得到方法的參數代碼method.getParameterTypes();即:

public Class[] methodsParamsTypes(Method method) { 
    return method.getParameterTypes(); 
} 

參見here的完整示例。

編輯 再次讀您的問題我不確定上面的答案是您正在尋找的。

你的意思是你的代碼中有來自地圖的參數,並且你想通過反射調用正確的方法嗎?要麼與LongString?如果是的話這裏有一個例子:

public Method getDynMethod(Class aClass, String methodName, Object...params) { 
    Method m = null; 
    Class[] paramTypes = new Class[params.length]; 
    for (int i = 0; i < paramTypes.length; i++) { 
     //note: if params[i] == null is not possible to retrieve the class type... 
     paramTypes[i] = params[i].getClass(); 
    } 
    try { 
     m = aClass.getMethod(methodName, paramTypes); 
    } catch (NoSuchMethodException nsme) { 
     nsme.printStackTrace(); 
    } 
    return m; 
} 

,並在你的代碼,你可以調用它像:

Method m = getDynMethod(c, methodName, ap.getValue()); 
+0

雖然這個鏈接可能回答這個問題,最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – JNYRanger

+0

好點,我更新了一些代碼片段的答案 – Paizo

0

如果你想要得到你應該調用的方法的參數類型方法稱爲

getParameterTypes() 返回一個Class對象的數組,它是預期的參數。

看一看這裏瞭解更多信息: Method class Documentation

編輯:我得到了ninja'ed: - (