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

org.hibernate.validator.internal.util.privilegedactions.NewProxyInstance 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.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.security.PrivilegedAction;

/**
 * Execute proxy creation as privileged action.
 *
 * @author Guillaume Smet
 */
public final class NewProxyInstance implements PrivilegedAction {

	private final ClassLoader classLoader;
	private final Class[] interfaces;
	private final InvocationHandler invocationHandler;

	public static  NewProxyInstance action(ClassLoader classLoader, Class interfaze, InvocationHandler invocationHandler) {
		return new NewProxyInstance( classLoader, interfaze, invocationHandler );
	}

	public static NewProxyInstance action(ClassLoader classLoader, Class[] interfaces, InvocationHandler invocationHandler) {
		return new NewProxyInstance( classLoader, interfaces, invocationHandler );
	}

	private NewProxyInstance(ClassLoader classLoader, Class[] interfaces, InvocationHandler invocationHandler) {
		this.classLoader = classLoader;
		this.interfaces = interfaces;
		this.invocationHandler = invocationHandler;
	}

	private NewProxyInstance(ClassLoader classLoader, Class interfaze, InvocationHandler invocationHandler) {
		this.classLoader = classLoader;
		this.interfaces = new Class[] { interfaze };
		this.invocationHandler = invocationHandler;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T run() {
		return (T) Proxy.newProxyInstance( classLoader, interfaces, invocationHandler );
	}
}