[英]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.