TypeProducerFactory { public TypeProducer createTypeProducer(Class<?> aClass, Type genericType) { if (aClass == List.class || aClass == Set.class || aClass == SortedSet.class) { Class<?> actualTypeArgument = null; if (genericType != null) { actualTypeArgument = getActualTypeArgument(genericType); } Method methodValueOf; Constructor<?> constructor; if (actualTypeArgument == String.class || actualTypeArgument == null) { return new CollectionStringProducer(aClass); } else if ((methodValueOf = getStringValueOfMethod(actualTypeArgument)) != null) { return new CollectionStringValueOfProducer(aClass, methodValueOf); } else if ((constructor = getStringConstructor(actualTypeArgument)) != null) { return new CollectionStringConstructorProducer(aClass, constructor); } } else { Method methodValueOf; Constructor<?> constructor; if (aClass.isPrimitive()) { return new PrimitiveTypeProducer(aClass); } else if (aClass == String.class) { return new StringProducer(); } else if ((methodValueOf = getStringValueOfMethod(aClass)) != null) { return new StringValueOfProducer(methodValueOf); } else if ((constructor = getStringConstructor(aClass)) != null) { return new StringConstructorProducer(constructor); } } throw new IllegalArgumentException(String.format("Unsupported type %s", aClass)); }  TypeProducer createTypeProducer(Class<?> aClass, Type genericType);  }
@Test public void createsProducerForListOfString() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(List.class, ParameterizedTypeImpl.newParameterizedType(List.class, String.class)); assertEquals(CollectionStringProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSetOfString() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(Set.class, ParameterizedTypeImpl.newParameterizedType(Set.class, String.class)); assertEquals(CollectionStringProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSortedSetOfString() throws Exception { TypeProducer typeProducer = typeProducerFactory .createTypeProducer(SortedSet.class, ParameterizedTypeImpl.newParameterizedType(SortedSet.class, String.class)); assertEquals(CollectionStringProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForListOfTypeThatHasStringConstructor() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(List.class, ParameterizedTypeImpl.newParameterizedType(List.class, StringConstructorClass.class)); assertEquals(CollectionStringConstructorProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSetOfTypeThatHasStringConstructor() throws Exception { TypeProducer typeProducer = typeProducerFactory .createTypeProducer(Set.class, ParameterizedTypeImpl.newParameterizedType(Set.class, StringConstructorClass.class)); assertEquals(CollectionStringConstructorProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSortedSetOfTypeThatHasStringConstructor() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(SortedSet.class, ParameterizedTypeImpl .newParameterizedType(SortedSet.class, StringConstructorClass.class)); assertEquals(CollectionStringConstructorProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForListOfTypeThatHasStringValueOfMethod() throws Exception { TypeProducer typeProducer = typeProducerFactory .createTypeProducer(List.class, ParameterizedTypeImpl.newParameterizedType(List.class, ValueOfStringClass.class)); assertEquals(CollectionStringValueOfProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSetOfTypeThatHasStringValueOfMethod() throws Exception { TypeProducer typeProducer = typeProducerFactory .createTypeProducer(Set.class, ParameterizedTypeImpl.newParameterizedType(Set.class, ValueOfStringClass.class)); assertEquals(CollectionStringValueOfProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForSortedSetOfTypeThatHasStringValueOfMethod() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(SortedSet.class, ParameterizedTypeImpl.newParameterizedType(SortedSet.class, ValueOfStringClass.class)); assertEquals(CollectionStringValueOfProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForPrimitiveType() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(int.class, null); assertEquals(PrimitiveTypeProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForString() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(String.class, null); assertEquals(StringProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForTypeThatHasStringConstructor() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(StringConstructorClass.class, null); assertEquals(StringConstructorProducer.class, typeProducer.getClass()); }
@Test public void createsProducerForTypeThatHasStringValueOfMethod() throws Exception { TypeProducer typeProducer = typeProducerFactory.createTypeProducer(ValueOfStringClass.class, null); assertEquals(StringValueOfProducer.class, typeProducer.getClass()); }
@Test(expected = IllegalArgumentException.class) public void throwsExceptionWhenNeitherStringConstructorNorStringValueOfMethodAvailable() throws Exception { typeProducerFactory.createTypeProducer(NoStringConstructorNoValueOfClass.class, null); }