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

org.hibernate.validator.internal.util.privilegedactions.ConstructorInstance Maven / Gradle / Ivy

There is a newer version: 8.0.1.Final
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */


package org.hibernate.validator.internal.util.privilegedactions;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.PrivilegedAction;

import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
 * Execute instance creation as privileged action.
 *
 * @author Emmanuel Bernard
 * @author Hardy Ferentschik
 */
public final class ConstructorInstance implements PrivilegedAction {

	private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );

	private final Constructor constructor;
	private final Object[] initArgs;

	public static  ConstructorInstance action(Constructor constructor, Object... initArgs) {
		return new ConstructorInstance<>( constructor, initArgs );
	}

	private ConstructorInstance(Constructor constructor, Object... initArgs) {
		this.constructor = constructor;
		this.initArgs = initArgs;
	}

	@Override
	public T run() {
		try {
			return constructor.newInstance( initArgs );
		}
		catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw LOG.getUnableToInstantiateException( constructor.getDeclaringClass(), e );
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy