package com.jenjinstudios.io.serialization;

import com.google.gson.*;
import com.jenjinstudios.io.Message;
import com.jenjinstudios.io.annotations.MessageAdapter;
import org.reflections.Reflections;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Used to deserialize Message objects with Gson.
 *
 * @author Caleb Brinkman
 */
public class GsonMessageDeserializer implements JsonDeserializer<Message>
{
    private static final Map<String, Class> MESSAGE_CLASSES = new HashMap<>(10);

    static {
        Reflections reflections = new Reflections("");
        final Set<Class<? extends Message>> messageClasses = reflections.getSubTypesOf(Message.class);
        for (Class messageClass : messageClasses) {
            Annotation annotation = messageClass.getAnnotation(MessageAdapter.class);
            if (annotation != null) {
                String adaptFromClass = ((MessageAdapter) annotation).adaptFrom().getName();
                MESSAGE_CLASSES.put(adaptFromClass, messageClass);
            } else {
                MESSAGE_CLASSES.put(messageClass.getName(), messageClass);
            }
        }
    }

    @Override
    public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException
    {
        JsonObject jsonObject = json.getAsJsonObject();
        JsonElement classElement = jsonObject.get("class");
        if (classElement == null) {
            throw new JsonParseException("Message must specify class to be deserialized into");
        }
        String className = classElement.getAsString();
        Class<Message> lookupClass = MESSAGE_CLASSES.get(className);
        JsonElement fieldsElement = jsonObject.get("fields");
        return (fieldsElement != null)
              ? context.deserialize(fieldsElement, lookupClass)
              : context.deserialize(new JsonObject(), lookupClass);
    }
}

@@ -192,16 +192,73 @@
 ections;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -556,16 +556,113 @@
 sage%3E%0A%7B%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(GsonMessageDeserializer.class);%0A
     priv
@@ -748,16 +748,70 @@
 tatic %7B%0A
+        LOGGER.debug(%22Scanning for message classes%22);%0A
         
@@ -1008,24 +1008,106 @@
 eClasses) %7B%0A
+            LOGGER.debug(%22Registering message class: %22 + messageClass.getName());%0A
             
@@ -1331,79 +1331,69 @@
     
-MESSAGE_CLASSES.put(adaptFromClass, messageClass);%0A            %7D else %7B
+LOGGER.debug(%22Registering adapter class: %22 + adaptFromClass);
 %0A   
@@ -1425,38 +1425,30 @@
 SES.put(
-messageClass.getName()
+adaptFromClass
 , messag
@@ -1476,24 +1476,97 @@
 %7D%0A        %7D%0A
+        LOGGER.debug(%22Registered Message Classes: %7B%7D%22, MESSAGE_CLASSES);%0A
     %7D%0A%0A    @
@@ -2066,42 +2066,423 @@
 lass
- = MESSAGE_CLASSES.get(className);
+;%0A        if (MESSAGE_CLASSES.containsKey(className)) %7B%0A            lookupClass = MESSAGE_CLASSES.get(className);%0A        %7D else %7B%0A            try %7B%0A                lookupClass = (Class%3CMessage%3E) Class.forName(className);%0A            %7D catch (ClassNotFoundException %7C ClassCastException e) %7B%0A                throw new JsonParseException(%22Message implementation not found or incorrect: %22, e);%0A            %7D%0A        %7D
 %0A   

Revert part of last commit