我大部分已經解決了這個問題,但最終無法弄清楚錯誤追蹤 - 這可能是一些微妙的事情,我可能會犯錯。將這些字段值排序爲一個地圖
我正在爲GSON實施一種解決方法,該解決方案具有真正的problems parsing nested maps。
public class RegisterValues
{
int Earth;
int Mars;
//etc for 200 planets
public Map returnValues()throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException{
final String [] fieldValues = new String[] {"Earth", "Mars", //etc }
RegisterValues regValues = new RegisterValues();
Map values = new HashMap<String, Integer>();
//values.put("Earth", Earth); manually this works, I'd rather loop through a readymade array of String values.
for (String value : fieldValues){
values.put(field, regValues.getClass().getField(field).getInt(field);) //here it breaks
}
return values;
}
}
的錯誤跟蹤:
Rebinding example.client.Panel // This is the class that is calling the above function
02:24:31.704 [DEBUG] Checking rule <generate-with class='com.google.gwtjsonrpc.rebind.RemoteJsonServiceProxyGenerator'/>
02:24:31.704 [ERROR] Errors in '.../RegisterValues.java'
02:24:31.704 [ERROR] Line 324: No source code is available for type java.lang.SecurityException; did you forget to inherit a required module?
02:24:31.704 [ERROR] Line 333: The method getField(String) is undefined for the type Class<capture#1-of ? extends RegisterValues>
所有的行號指的是在那裏我調用這個類的部分。它在我手動填充地圖時起作用,但在嘗試執行此循環方法時不起作用。
基本上我想知道以上是否正確?
旁註:如果上述看起來是正確的,這是由於這是不可能的,因爲這是即它反射類是動態編譯(GWT.create()),只有當我訪問的這部分該程序 - 因此它有一些問題呢?
如果您發佈了可編譯代碼,這將有所幫助。 –
你是什麼意思兼容的代碼? – fiz
基本上將這些字符串改爲字段名是我想要的 – fiz