All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refutils.util.ConstructorHelper Maven / Gradle / Ivy

The newest version!
package refutils.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Utility class to handle constructors.
 *
 * @param  The type to instantiate
 * @author exbjek
 */
public final class ConstructorHelper {
  private final Class clazz;

  public ConstructorHelper(final Class clazz) {
    this.clazz = clazz;
  }

  /**
   * Instantiate private empty constructor.
   *
   * @return the instance
   * @throws NoSuchMethodException if a matching method is not found.
   * @throws IllegalAccessException should never be thrown since the constructor is made accessible
   * @throws InvocationTargetException if the underlying constructor throws an exception.
   * @throws InstantiationException if the class that declares the underlying constructor represents
   *     an abstract class.
   */
  public T instantiatePrivate()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
          InstantiationException {
    Constructor constructor = clazz.getDeclaredConstructor();
    constructor.setAccessible(true);
    return constructor.newInstance();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy