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

org.hibernate.validator.internal.util.privilegedactions.GetAnnotationAttributes 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 static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.PrivilegedAction;
import java.util.Map;

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

/**
 * @author Guillaume Smet
 */
public final class GetAnnotationAttributes implements PrivilegedAction> {

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

	private final Annotation annotation;

	public static GetAnnotationAttributes action(Annotation annotation) {
		return new GetAnnotationAttributes( annotation );
	}

	private GetAnnotationAttributes(Annotation annotation) {
		this.annotation = annotation;
	}

	@Override
	public Map run() {
		final Method[] declaredMethods = annotation.annotationType().getDeclaredMethods();
		Map attributes = newHashMap( declaredMethods.length );

		for ( Method m : declaredMethods ) {
			// HV-1184 Exclude synthetic methods potentially introduced by jacoco
			if ( m.isSynthetic() ) {
				continue;
			}

			m.setAccessible( true );

			String attributeName = m.getName();

			try {
				attributes.put( m.getName(), m.invoke( annotation ) );
			}
			catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				throw LOG.getUnableToGetAnnotationAttributeException( annotation.getClass(), attributeName, e );
			}
		}
		return CollectionHelper.toImmutableMap( attributes );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy