@Test void shouldThrowForUnmatchedType() { Set<Character> set = new HashSet<>(Arrays.asList('a', 'b', 'c', 'd')); Class<?> rawType = Map.class; assertThrows(InjectorException.class, () -> ReflectionUtils.toSuitableCollectionType(rawType, set)); }
public static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result) { if (rawType.isArray()) { @SuppressWarnings("unchecked") Class<T[]> arrayClass = (Class<T[]>) rawType; return Arrays.copyOf(result.toArray(), result.size(), arrayClass); } else if (rawType.isAssignableFrom(Set.class)) { return result; } else if (rawType.isAssignableFrom(List.class)) { return new ArrayList<>(result); } throw new InjectorException("Cannot convert @AllTypes result to '" + rawType + "'. " + "Supported: Set, List, or any supertype thereof, and array"); }
ReflectionUtils { public static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result) { if (rawType.isArray()) { @SuppressWarnings("unchecked") Class<T[]> arrayClass = (Class<T[]>) rawType; return Arrays.copyOf(result.toArray(), result.size(), arrayClass); } else if (rawType.isAssignableFrom(Set.class)) { return result; } else if (rawType.isAssignableFrom(List.class)) { return new ArrayList<>(result); } throw new InjectorException("Cannot convert @AllTypes result to '" + rawType + "'. " + "Supported: Set, List, or any supertype thereof, and array"); } }
ReflectionUtils { public static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result) { if (rawType.isArray()) { @SuppressWarnings("unchecked") Class<T[]> arrayClass = (Class<T[]>) rawType; return Arrays.copyOf(result.toArray(), result.size(), arrayClass); } else if (rawType.isAssignableFrom(Set.class)) { return result; } else if (rawType.isAssignableFrom(List.class)) { return new ArrayList<>(result); } throw new InjectorException("Cannot convert @AllTypes result to '" + rawType + "'. " + "Supported: Set, List, or any supertype thereof, and array"); } private  ReflectionUtils(); }
ReflectionUtils { public static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result) { if (rawType.isArray()) { @SuppressWarnings("unchecked") Class<T[]> arrayClass = (Class<T[]>) rawType; return Arrays.copyOf(result.toArray(), result.size(), arrayClass); } else if (rawType.isAssignableFrom(Set.class)) { return result; } else if (rawType.isAssignableFrom(List.class)) { return new ArrayList<>(result); } throw new InjectorException("Cannot convert @AllTypes result to '" + rawType + "'. " + "Supported: Set, List, or any supertype thereof, and array"); } private  ReflectionUtils(); static Object getFieldValue(Field field, Object instance); static void setField(Field field, Object instance, Object value); static Object invokeMethod(Method method, Object instance, Object... parameters); static T newInstance(Constructor<T> constructor, Object... parameters); @Nullable static Class<?> getGenericType(@Nullable Type genericType); @Nullable static Class<?> getCollectionType(Class<?> mainType, @Nullable Type genericType); static Object toSuitableCollectionType(Class<?> rawType, Set<T> result); static Method[] safeGetDeclaredMethods(Class<?> clazz); static Field[] safeGetDeclaredFields(Class<?> clazz); }
ReflectionUtils { public static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result) { if (rawType.isArray()) { @SuppressWarnings("unchecked") Class<T[]> arrayClass = (Class<T[]>) rawType; return Arrays.copyOf(result.toArray(), result.size(), arrayClass); } else if (rawType.isAssignableFrom(Set.class)) { return result; } else if (rawType.isAssignableFrom(List.class)) { return new ArrayList<>(result); } throw new InjectorException("Cannot convert @AllTypes result to '" + rawType + "'. " + "Supported: Set, List, or any supertype thereof, and array"); } private  ReflectionUtils(); static Object getFieldValue(Field field, Object instance); static void setField(Field field, Object instance, Object value); static Object invokeMethod(Method method, Object instance, Object... parameters); static T newInstance(Constructor<T> constructor, Object... parameters); @Nullable static Class<?> getGenericType(@Nullable Type genericType); @Nullable static Class<?> getCollectionType(Class<?> mainType, @Nullable Type genericType); static Object toSuitableCollectionType(Class<?> rawType, Set<T> result); static Method[] safeGetDeclaredMethods(Class<?> clazz); static Field[] safeGetDeclaredFields(Class<?> clazz);  }