[英]Sorting these Field Values into a Map
我已經解決了這個問題,但最終無法弄清楚錯誤的痕跡 - 這可能是一些微妙的事情我可能做錯了。
我正在為GSON實現一個解決方法,它解析嵌套映射時遇到了實際問題 。
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()) - 因此它有一些問題呢?
java.lang.SecurityException
被拋出到客戶端,但它與GWT客戶端不兼容(因為客戶端代碼被交叉編譯為Javascript)。
請參閱此鏈接以獲取可與客戶端代碼一起使用的Java類列表: http : //www.gwtproject.org/doc/latest/RefJreEmulation.html
看起來你正在客戶端調用getField(String)
。 這就是[ERROR] Line 333
正在發生的原因。 如果此代碼位於服務器端,請確保此類的路徑在.gwt.xml
中沒有條目作為源路徑(例如:確保<source path='server' />
不存在)。
而不是做regValues.getClass()
,你可以嘗試RegisterValues.class
? (我不確定這是否有所作為)
另外我不確定你要完成什么,但你不能在GWT客戶端使用Gson。 您可以使用GWT的AutoBean功能: 在GWT客戶端代碼中使用Gson庫
您可以在服務器端使用Gson,如果它不能創建Map
您可以在客戶端使用,您可以編寫自定義Serializer( com.google.gson.JsonSerializer
)和Deserializer( com.google.gson.JsonDeserializer
)構建可在客戶端使用的Map
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.