2014-03-27 51 views
0

我大部分已經解決了這個問題,但最終無法弄清楚錯誤追蹤 - 這可能是一些微妙的事情,我可能會犯錯。將這些字段值排序爲一個地圖

我正在爲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()),只有當我訪問的這部分該程序 - 因此它有一些問題呢?

+0

如果您發佈了可編譯代碼,這將有所幫助。 –

+0

你是什麼意思兼容的代碼? – fiz

+0

基本上將這些字符串改爲字段名是我想要的 – fiz

回答

2

A 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功能來代替:using Gson library in GWT client code

您可以在服務器端使用GSON,如果不創建Map是你可以在客戶端使用,你可以寫一個自定義的串行(com.google.gson.JsonSerializer)及解串器(com.google.gson.JsonDeserializer)構建Map s,可在客戶端使用。