org.hibernate.validation.util.NewInstance Maven / Gradle / Ivy
package org.hibernate.validation.util;
import java.security.PrivilegedAction;
import javax.validation.ValidationException;
/**
* @author Emmanuel Bernard
*/
public class NewInstance implements PrivilegedAction {
private final Class clazz;
private final String message;
public static NewInstance action(Class clazz, String message) {
return new NewInstance( clazz, message );
}
private NewInstance(Class clazz, String message) {
this.clazz = clazz;
this.message = message;
}
public T run() {
try {
return clazz.newInstance();
}
catch ( InstantiationException e ) {
throw new ValidationException( "Unable to instanciate " + message + ": " + clazz, e );
}
catch ( IllegalAccessException e ) {
throw new ValidationException( "Unable to instanciate " + clazz, e );
}
catch ( RuntimeException e ) {
throw new ValidationException( "Unable to instanciate " + clazz, e );
}
}
}