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

org.junit.jupiter.params.support.AnnotationConsumerInitializer Maven / Gradle / Ivy

There is a newer version: 5.11.3
Show newest version
/*
 * Copyright 2015-2020 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.params.support;

import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.BOTTOM_UP;
import static org.junit.platform.commons.util.ReflectionUtils.findMethods;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.function.Predicate;

import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.AnnotationUtils;

/**
 * {@code AnnotationConsumerInitializer} is an internal helper class for
 * initializing {@link AnnotationConsumer AnnotationConsumers}.
 *
 * @since 5.0
 */
@API(status = INTERNAL, since = "5.0")
public final class AnnotationConsumerInitializer {

	private AnnotationConsumerInitializer() {
		/* no-op */
	}

	// @formatter:off
	private static final Predicate isAnnotationConsumerAcceptMethod = method ->
			method.getName().equals("accept")
			&& method.getParameterCount() == 1
			&& method.getParameterTypes()[0].isAnnotation();
	// @formatter:on

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  T initialize(AnnotatedElement annotatedElement, T instance) {
		if (instance instanceof AnnotationConsumer) {
			Method method = findMethods(instance.getClass(), isAnnotationConsumerAcceptMethod, BOTTOM_UP).get(0);
			Class annotationType = (Class) method.getParameterTypes()[0];
			Annotation annotation = AnnotationUtils.findAnnotation(annotatedElement, annotationType) //
					.orElseThrow(() -> new JUnitException(instance.getClass().getName()
							+ " must be used with an annotation of type " + annotationType.getName()));
			initializeAnnotationConsumer((AnnotationConsumer) instance, annotation);
		}
		return instance;
	}

	private static  void initializeAnnotationConsumer(AnnotationConsumer instance,
			A annotation) {
		try {
			instance.accept(annotation);
		}
		catch (Exception ex) {
			throw new JUnitException("Failed to initialize AnnotationConsumer: " + instance, ex);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy