NullConverter extends AbstractConverter { @SuppressWarnings("unchecked") public static <T> T nullConvert(Class<T> type) { if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) { return (T) Boolean.FALSE; } else if (type.equals(String.class)) { return type.cast(""); } else if (Number.class.isAssignableFrom(type)) { try { return type.getConstructor(String.class).newInstance("0"); } catch (IllegalAccessException | InstantiationException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { return null; } } else if (type.equals(Date.class)) { return type.cast(new Date()); } else if (type.isPrimitive()) { try { String name = type.getSimpleName(); if (name.equals("int")) { name = "Integer"; } return (T) Class.forName("java.lang." + WordUtils.capitalize(name)).getConstructor(String.class).newInstance("0"); } catch (IllegalAccessException | InstantiationException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) { e.printStackTrace(); return null; } } return null; }  @SuppressWarnings("unchecked") static T nullConvert(Class<T> type);  }
@Test public void test() { Integer val = NullConverter.nullConvert(int.class); assertThat("int", val == 0); byte b = NullConverter.nullConvert(byte.class); String nullString = NullConverter.nullConvert(String.class); }