简体   繁体   中英

Sorting these Field Values into a Map

I have mostly solved the problem but can't figure out the error trace at the end - it'll be something subtle I'm doing wrong probably.

I'm implementing a workaround for GSON, which has real 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;
            }
         }

The error trace:

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>

All the line numbers are referring to the part where I call this class. It works when I fill the map manually, but it doesn't work when I try doing this looping method.

Basically I'm wondering if the above is correct?

Sidenote : If the above looks correct, is it due to this being impossible since this is a reflected class ie it is compiled on the fly (GWT.create()) only when I access this part of the program - therefore it is having some problem with that?

A java.lang.SecurityException is getting thrown to the client side, but it is not compatible with the GWT client side (because client code gets cross-compiled to Javascript).

See this link for a list of Java classes you can use with client-side code: http://www.gwtproject.org/doc/latest/RefJreEmulation.html

It looks like you're calling getField(String) on the client side. That's why [ERROR] Line 333 is happening. If this code is in the server side, make sure the path for this class doesn't have an entry in the .gwt.xml as a source path (eg: make sure <source path='server' /> doesn't exist).

Instead of doing regValues.getClass() , can you try RegisterValues.class ? (I'm not sure whether this would make a difference)

Also I'm not sure what you're trying to accomplish, but you can't use Gson on the GWT client side. You can use GWT's AutoBean feature instead: using Gson library in GWT client code

You can use Gson on the server side, and if it doesn't create Map s you can use on the client side, you can write a custom Serializer( com.google.gson.JsonSerializer ) and Deserializer( com.google.gson.JsonDeserializer ) to build Map s that are usable on the client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM