|
| 1 | +package api.json; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonParseException; |
| 5 | +import com.google.gson.TypeAdapter; |
| 6 | +import com.google.gson.TypeAdapterFactory; |
| 7 | +import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; |
| 8 | +import com.google.gson.reflect.TypeToken; |
| 9 | + |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * A class for handling unexpected fields in GobPie configuration. |
| 16 | + * Code adapted from <a href="https://github.com/google/gson/issues/188">a Gson library issue</a>. |
| 17 | + * |
| 18 | + * @since 0.0.4 |
| 19 | + */ |
| 20 | + |
| 21 | +public class GobPieConfValidatorAdapterFactory implements TypeAdapterFactory { |
| 22 | + |
| 23 | + @Override |
| 24 | + public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { |
| 25 | + // If the type adapter is a reflective type adapter, we want to modify the implementation using reflection. |
| 26 | + // The trick is to replace the Map object used to look up the property name. |
| 27 | + // Instead of returning null if the property is not found, |
| 28 | + // we throw a Json exception to terminate the deserialization. |
| 29 | + TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type); |
| 30 | + |
| 31 | + // Check if the type adapter is a reflective, cause this solution only work for reflection. |
| 32 | + if (delegateAdapter instanceof ReflectiveTypeAdapterFactory.Adapter) { |
| 33 | + |
| 34 | + try { |
| 35 | + // Get reference to the existing boundFields. |
| 36 | + Field f = findBoundField(delegateAdapter.getClass()); |
| 37 | + f.setAccessible(true); |
| 38 | + // Finally, push our custom map back using reflection. |
| 39 | + f.set(delegateAdapter, getBoundFields(f, delegateAdapter)); |
| 40 | + } catch (Exception e) { |
| 41 | + // Should never happen if the implementation doesn't change. |
| 42 | + throw new IllegalStateException(e); |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + return delegateAdapter; |
| 47 | + } |
| 48 | + |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + private static <T> Object getBoundFields(Field f, TypeAdapter<T> delegate) throws IllegalAccessException { |
| 51 | + Object boundFields = f.get(delegate); |
| 52 | + |
| 53 | + // Then replace it with our implementation throwing exception if the value is null. |
| 54 | + boundFields = new LinkedHashMap<>((Map<Object, Object>) boundFields) { |
| 55 | + |
| 56 | + @Override |
| 57 | + public Object get(Object key) { |
| 58 | + Object value = super.get(key); |
| 59 | + if (value == null) { |
| 60 | + throw new JsonParseException(String.valueOf(key)); |
| 61 | + } |
| 62 | + return value; |
| 63 | + } |
| 64 | + |
| 65 | + }; |
| 66 | + return boundFields; |
| 67 | + } |
| 68 | + |
| 69 | + private static Field findBoundField(Class<?> startingClass) throws NoSuchFieldException { |
| 70 | + for (Class<?> c = startingClass; c != null; c = c.getSuperclass()) { |
| 71 | + try { |
| 72 | + return c.getDeclaredField("boundFields"); |
| 73 | + } catch (NoSuchFieldException e) { |
| 74 | + // OK: continue with superclasses |
| 75 | + } |
| 76 | + } |
| 77 | + throw new NoSuchFieldException("boundFields starting from " + (startingClass != null ? startingClass.getName() : null)); |
| 78 | + } |
| 79 | +} |
0 commit comments