DeserializationConstructorAndReflectiveTypeAdapterFactory implements TypeAdapterFactory { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } } DeserializationConstructorAndReflectiveTypeAdapterFactory(ConstructorConstructor constructorConstructor,
         FieldNamingStrategy serializationFieldNamingPolicy, Excluder excluder,
         AnnotationConstructorNamingStrategy deserializationFieldNamingPolicy); TypeAdapter<T> create(Gson gson, TypeToken<T> type);  }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* parameter 0 failed to be named by AnnotationBasedNamingStrategy requiring one of javax.inject.Named") public void testSerializedNameRequiredOnAllParameters() { parameterizedCtorFactory .create(gson, TypeToken.get(WithDeserializationConstructorButWithoutSerializedName.class)); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "duplicate key: foo") public void testNoDuplicateSerializedNamesRequiredOnAllParameters() { parameterizedCtorFactory.create(gson, TypeToken.get(DuplicateSerializedNames.class)); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "absent!") public void testValidatedConstructor() throws IOException { Gson gson = new GsonBuilder().registerTypeAdapterFactory(parameterizedCtorFactory) .registerTypeAdapterFactory(new OptionalTypeAdapterFactory()).create(); assertEquals(new ValidatedConstructor(Optional.of(0), 1), gson.fromJson("{\"foo\":0,\"bar\":1}", ValidatedConstructor.class)); gson.fromJson("{\"bar\":1}", ValidatedConstructor.class); }
@Test(expectedExceptions = NullPointerException.class) public void testPartialObjectStillThrows() throws IOException { TypeAdapter<ComposedObjects> adapter = parameterizedCtorFactory .create(gson, TypeToken.get(ComposedObjects.class)); assertNull(adapter.fromJson("{\"x\":{\"foo\":0,\"bar\":1}}")); }