Serializers { @SuppressWarnings({"unchecked"}) public static <S extends Transformer<?, ?>> S instantiate(final Class<S> serializerClass, TypeToken<?> sourceType) { if (!getSourceType(serializerClass).isSupertypeOf(sourceType)) { throw new IllegalArgumentException(String.format( "Can not instantiate %s, the serializer source %s is not assignable from %s", serializerClass, getSourceType(serializerClass), sourceType)); } for (SerializerConstructor constructor : SerializerConstructor.values()) { try { return constructor.construct(serializerClass, sourceType.getType()); } catch (NoSuchMethodException e) { continue; } catch (Exception e) { String errorMessage = String.format( "Failed to instantiate custom serializer constructor %s%s with source type: %s", serializerClass.getName(), constructor, sourceType); throw new IllegalStateException(errorMessage, e); } } String message = String.format( "Failed to instantiate custom serializer %s, constructors not found: %s", serializerClass.getName(), Arrays.toString(SerializerConstructor.values())); throw new IllegalStateException(message); } private  Serializers(); @SuppressWarnings({"unchecked"}) static S instantiate(final Class<S> serializerClass,
      TypeToken<?> sourceType); static List<Class<? extends Transformer<?, ?>>> getSerializerClasses(
      @Nullable Type type, @Nullable final ApiSerializationConfig config); static List<Class<? extends Transformer<?, ?>>> getSerializerClasses(
      TypeToken<?> type, @Nullable final ApiSerializationConfig config); @Nullable static TypeToken<?> getSourceType(Class<? extends Transformer<?, ?>> clazz); @Nullable static TypeToken<?> getTargetType(Class<? extends Transformer<?, ?>> clazz);  }
@Test public void testInstantiate_invalid() { try { Serializers.instantiate(PrivateConstructorSerializer.class, TypeToken.of(String.class)); fail("Instatiation should fail for non-serializers."); } catch (RuntimeException expected) { } }
@Test public void testInstantiate_typeConstructor() { Transformer<List<?>, Type> serializer; serializer = Serializers.instantiate(ListToTypeSerializer.class, TypeToken.of(ImmutableList.class)); assertEquals(ImmutableList.class, serializer.transformTo(ImmutableList.of())); Type typeWithGeneric = new TypeToken<List<?>>() {}.getType(); serializer = Serializers.instantiate(ListToTypeSerializer.class, TypeToken.of(typeWithGeneric)); assertEquals(typeWithGeneric, serializer.transformTo(ImmutableList.of())); assertEquals(ImmutableList.of(typeWithGeneric), serializer.transformFrom(List.class)); try { Serializers.instantiate(ListToTypeSerializer.class, TypeToken.of(Collection.class)); fail("Shouldn't be able to instantiate with Collection"); } catch (IllegalArgumentException e) { } }
@Test public void testInstantiate_classConstructor() { Transformer<Set<?>, Type> serializer; serializer = Serializers.instantiate(SetToTypeSerializer.class, TypeToken.of(ImmutableSet.class)); TypeToken<?> typeWithGeneric = new TypeToken<TreeSet<?>>() {}; serializer = Serializers.instantiate(SetToTypeSerializer.class, typeWithGeneric); assertEquals(TreeSet.class, serializer.transformTo(ImmutableSet.of())); try { Serializers.instantiate(SetToTypeSerializer.class, TypeToken.of(Collection.class)); fail("Shouldn't be able to instantiate with Collection"); } catch (IllegalArgumentException e) { } }
@Test public void testInstantiate_noConstructor() { try { Serializers.instantiate(NoValidConstructorSerializer.class, TypeToken.of(String.class)); fail("Shouldn't be able to instantiate with Collection"); } catch (IllegalStateException e) { String expected = "Failed to instantiate custom serializer " + NoValidConstructorSerializer.class.getName() + ", constructors not found: " + "[(interface java.lang.reflect.Type), (class java.lang.Class), ()]"; assertEquals(expected, e.getMessage()); } }