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

org.jnario.runner.internal.ExtensionClass Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2012 BMW Car IT and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.jnario.runner.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.FrameworkMethod;

/**
 * @author Sebastian Benz - Initial contribution and API
 */
public class ExtensionClass {

	private Map, List> methodsForAnnotations= new HashMap, List>();

	private FrameworkField extensionField;

	public ExtensionClass(FrameworkField extensionField) {
		this.extensionField = extensionField;
		for (Class eachClass : getSuperClasses(extensionField.getField().getType())) {
			for (Method eachMethod : eachClass.getDeclaredMethods()){
				addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
			}
		}
	}

	private  void addToAnnotationLists(FrameworkMethod member, Map, List> map) {
		for (Annotation each : member.getAnnotations()) {
			Class type= each.annotationType();
			List members= getAnnotatedMembers(map, type);
			for (FrameworkMethod otherMethod : members) {
				if(member.isShadowedBy(otherMethod)){
					return;
				}
			}
			if (runsTopToBottom(type))
				members.add(0, member);
			else
				members.add(member);
		}
	}
	

	public List getAnnotatedMethods(
			Class annotationClass) {
		return getAnnotatedMembers(methodsForAnnotations, annotationClass);
	}

	private  List getAnnotatedMembers(Map, List> map,
			Class type) {
		if (!map.containsKey(type))
			map.put(type, new ArrayList());
		return map.get(type);
	}

	private boolean runsTopToBottom(Class annotation) {
		return annotation.equals(Before.class)
				|| annotation.equals(BeforeClass.class);
	}

	private List> getSuperClasses(Class testClass) {
		ArrayList> results= new ArrayList>();
		Class current= testClass;
		while (current != null) {
			results.add(current);
			current= current.getSuperclass();
		}
		return results;
	}

	public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
		return extensionField.get(target);
	}

	public List allMethodsWithAnnotation(
			Class annotationType) {
		List result = methodsForAnnotations.get(annotationType);
		return result == null ? Collections.emptyList() : result;
	}

	public String getName() {
		Field field = extensionField.getField();
		return field.getType().getName() + "#" + field.getName();
	}

	public boolean isStatic() {
		return extensionField.isStatic();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy