@Test(expected = RuntimeException.class) public void badConvert( ) { ArrayList<Object> al = new ArrayList<Object>( ); for (int i = 0; i < 5; i++) { al.add(Integer.valueOf(i)); } List<String> asIntArray = GenericUtils.convert(al, String.class); System.out.println(asIntArray); }
@SuppressWarnings("unchecked") public static <T> java.util.List<T> convert(java.util.List<?> list, Class<T> clzz) { for (Object o : list) { if (!clzz.isAssignableFrom(o.getClass()) ) { throw new RuntimeException("invalid list conversion, " + clzz.getName() + " list contains " + o.getClass()); } } return (List<T>) list; }
GenericUtils { @SuppressWarnings("unchecked") public static <T> java.util.List<T> convert(java.util.List<?> list, Class<T> clzz) { for (Object o : list) { if (!clzz.isAssignableFrom(o.getClass()) ) { throw new RuntimeException("invalid list conversion, " + clzz.getName() + " list contains " + o.getClass()); } } return (List<T>) list; } }
GenericUtils { @SuppressWarnings("unchecked") public static <T> java.util.List<T> convert(java.util.List<?> list, Class<T> clzz) { for (Object o : list) { if (!clzz.isAssignableFrom(o.getClass()) ) { throw new RuntimeException("invalid list conversion, " + clzz.getName() + " list contains " + o.getClass()); } } return (List<T>) list; }  }
GenericUtils { @SuppressWarnings("unchecked") public static <T> java.util.List<T> convert(java.util.List<?> list, Class<T> clzz) { for (Object o : list) { if (!clzz.isAssignableFrom(o.getClass()) ) { throw new RuntimeException("invalid list conversion, " + clzz.getName() + " list contains " + o.getClass()); } } return (List<T>) list; }  @SuppressWarnings("unchecked") static java.util.List<T> convert(java.util.List<?> list, Class<T> clzz); }
GenericUtils { @SuppressWarnings("unchecked") public static <T> java.util.List<T> convert(java.util.List<?> list, Class<T> clzz) { for (Object o : list) { if (!clzz.isAssignableFrom(o.getClass()) ) { throw new RuntimeException("invalid list conversion, " + clzz.getName() + " list contains " + o.getClass()); } } return (List<T>) list; }  @SuppressWarnings("unchecked") static java.util.List<T> convert(java.util.List<?> list, Class<T> clzz);  }