CreateInstanceByDefaultConstructor.java

  1. package org.jbehave.core.steps.needle.configuration;

  2. /**
  3.  * Instantiates new java object by default constructor
  4.  * @author Jan Galinski, Holisticon AG (jan.galinski@holisticon.de)
  5.  * @author Simon Zambrovski, Holisticon AG (simon.zambrovski@holisticon.de)
  6.  */
  7. public enum CreateInstanceByDefaultConstructor {

  8.     /**
  9.      * Singleton
  10.      */
  11.     INSTANCE;

  12.     public final <T> T apply(final Class<T> type) {
  13.         try {
  14.             return type.getConstructor().newInstance();
  15.         } catch (final Exception e) {
  16.             throw new IllegalStateException(
  17.                     String.format("Can not instantiate instance of %s by default constructor.", type.getSimpleName()),
  18.                     e);
  19.         }
  20.     }

  21. }