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

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

Go to download

JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle

There is a newer version: 5.1.0
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.Method;
import java.security.PrivilegedAction;

/**
 * Returns the declared method with the specified name and parameter types or {@code null} if it does not exist.
 *
 * @author Kevin Pollet <[email protected]> (C) 2011 SERLI
 */
public final class GetDeclaredMethod implements PrivilegedAction {
	private final Class clazz;
	private final String methodName;
	private final Class[] parameterTypes;
	private final boolean makeAccessible;

	public static GetDeclaredMethod action(Class clazz, String methodName, Class... parameterTypes) {
		return new GetDeclaredMethod( clazz, methodName, false, parameterTypes );
	}

	/**
	 * Before using this method, you need to check the {@code HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS}
	 * permission against the security manager.
	 */
	public static GetDeclaredMethod andMakeAccessible(Class clazz, String methodName, Class... parameterTypes) {
		return new GetDeclaredMethod( clazz, methodName, true, parameterTypes );
	}

	private GetDeclaredMethod(Class clazz, String methodName, boolean makeAccessible, Class... parameterTypes) {
		this.clazz = clazz;
		this.methodName = methodName;
		this.parameterTypes = parameterTypes;
		this.makeAccessible = makeAccessible;
	}

	@Override
	public Method run() {
		try {
			Method method = clazz.getDeclaredMethod( methodName, parameterTypes );

			if ( makeAccessible ) {
				method.setAccessible( true );
			}

			return method;
		}
		catch (NoSuchMethodException e) {
			return null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy