Backstory: I have an external interface system that is based on JSON and GSON. The external interface receives a JSON string that always contains a key
as String type and an dictionary with the data. The associated Message
handler class is found in a lookup table based on the key
. This all works.
Implementation example:
class Message
{
public Class<?> payloadType;
public Message(Class<?> type)
{
this.payloadType = type;
}
public abstract void handle(Gson gson, T json);
}
public class NotifyPlayer extends Message<NotifyPlayer.JSONStructure>
{
public NotifyPlayer ()
{
super(JSONStructure.class);
}
@Override
public void handle(Gson gson, JSONStructure object)
{
//impl
}
static class JSONStructure
{
int user_id;
String message;
String from;
}
}
And the Message handler is registered in a map:
THashMap<String, Class<? extends Message>> messages
messages.add("notify", NotifyPlayer.class);
What I am trying to accomplish is to get all Field
s from the JSONStructure and print the Field
name, type and default value (if any).
What I am having trouble with is getting FieldUtils
to use the JSONStructure class type instead of java.lang.Class
Class<? extends Message> command = Server.getCommand("notify");
Object dummy = command.newInstance();
Object json = command.getField("type").get(dummy);
Class jsonClass = json.getClass();
System.out.println("Json getClass() : " + json.getClass());
Field[] fields = FieldUtils.getAllFields(jsonClass);
jsonClass
ends up being of type Class
instead of NotifyPlayer.JSONStructure
Aucun commentaire:
Enregistrer un commentaire