@Test public void shouldFailToInstantiateNoDefaultConstructor() { assertThatThrownBy( () -> ClassUtils.instantiateClassDefConstructor(NoDefaultConstructor.class)) .isInstanceOf(InstantiationException.class); }
public static <T> T instantiateClassDefConstructor(Class<T> clazz) { Objects.requireNonNull(clazz, "class to instantiate should not be null"); if (clazz.getConstructors().length > 0 && Arrays.stream(clazz.getConstructors()).noneMatch(c -> c.getParameterCount() == 0)) { throw new InstantiationException( "Default constructor is required to create instance of public class: " + clazz .getName()); } try { return clazz.getConstructor().newInstance(); } catch (Exception e) { throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e); } }
ClassUtils { public static <T> T instantiateClassDefConstructor(Class<T> clazz) { Objects.requireNonNull(clazz, "class to instantiate should not be null"); if (clazz.getConstructors().length > 0 && Arrays.stream(clazz.getConstructors()).noneMatch(c -> c.getParameterCount() == 0)) { throw new InstantiationException( "Default constructor is required to create instance of public class: " + clazz .getName()); } try { return clazz.getConstructor().newInstance(); } catch (Exception e) { throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e); } } }
ClassUtils { public static <T> T instantiateClassDefConstructor(Class<T> clazz) { Objects.requireNonNull(clazz, "class to instantiate should not be null"); if (clazz.getConstructors().length > 0 && Arrays.stream(clazz.getConstructors()).noneMatch(c -> c.getParameterCount() == 0)) { throw new InstantiationException( "Default constructor is required to create instance of public class: " + clazz .getName()); } try { return clazz.getConstructor().newInstance(); } catch (Exception e) { throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e); } } private  ClassUtils(); }
ClassUtils { public static <T> T instantiateClassDefConstructor(Class<T> clazz) { Objects.requireNonNull(clazz, "class to instantiate should not be null"); if (clazz.getConstructors().length > 0 && Arrays.stream(clazz.getConstructors()).noneMatch(c -> c.getParameterCount() == 0)) { throw new InstantiationException( "Default constructor is required to create instance of public class: " + clazz .getName()); } try { return clazz.getConstructor().newInstance(); } catch (Exception e) { throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e); } } private  ClassUtils(); static Predicate<T> instantiatePredicateClass(Class<? extends Predicate<T>> clazz); static T instantiateClassDefConstructor(Class<T> clazz); }
ClassUtils { public static <T> T instantiateClassDefConstructor(Class<T> clazz) { Objects.requireNonNull(clazz, "class to instantiate should not be null"); if (clazz.getConstructors().length > 0 && Arrays.stream(clazz.getConstructors()).noneMatch(c -> c.getParameterCount() == 0)) { throw new InstantiationException( "Default constructor is required to create instance of public class: " + clazz .getName()); } try { return clazz.getConstructor().newInstance(); } catch (Exception e) { throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e); } } private  ClassUtils(); static Predicate<T> instantiatePredicateClass(Class<? extends Predicate<T>> clazz); static T instantiateClassDefConstructor(Class<T> clazz);  }