aboutsummaryrefslogtreecommitdiff
path: root/src/org/junit/experimental/theories/ParameterSignature.java
blob: e7150fc24f8ac5db4639b6a0c2025781212bf962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * 
 */
package org.junit.experimental.theories;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ParameterSignature {
	public static ArrayList<ParameterSignature> signatures(Method method) {
		return signatures(method.getParameterTypes(), method
				.getParameterAnnotations());
	}

	public static List<ParameterSignature> signatures(Constructor<?> constructor) {
		return signatures(constructor.getParameterTypes(), constructor
				.getParameterAnnotations());
	}

	private static ArrayList<ParameterSignature> signatures(
			Class<?>[] parameterTypes, Annotation[][] parameterAnnotations) {
		ArrayList<ParameterSignature> sigs= new ArrayList<ParameterSignature>();
		for (int i= 0; i < parameterTypes.length; i++) {
			sigs.add(new ParameterSignature(parameterTypes[i],
					parameterAnnotations[i]));
		}
		return sigs;
	}

	private final Class<?> type;

	private final Annotation[] annotations;

	private ParameterSignature(Class<?> type, Annotation[] annotations) {
		this.type= type;
		this.annotations= annotations;
	}

	public boolean canAcceptType(Class<?> candidate) {
		return type.isAssignableFrom(candidate);
	}

	public Class<?> getType() {
		return type;
	}

	public List<Annotation> getAnnotations() {
		return Arrays.asList(annotations);
	}

	public boolean canAcceptArrayType(Class<?> type) {
		return type.isArray() && canAcceptType(type.getComponentType());
	}

	public boolean hasAnnotation(Class<? extends Annotation> type) {
		return getAnnotation(type) != null;
	}

	public <T extends Annotation> T findDeepAnnotation(Class<T> annotationType) {
		Annotation[] annotations2= annotations;
		return findDeepAnnotation(annotations2, annotationType, 3);
	}

	private <T extends Annotation> T findDeepAnnotation(
			Annotation[] annotations, Class<T> annotationType, int depth) {
		if (depth == 0)
			return null;
		for (Annotation each : annotations) {
			if (annotationType.isInstance(each))
				return annotationType.cast(each);
			Annotation candidate= findDeepAnnotation(each.annotationType()
					.getAnnotations(), annotationType, depth - 1);
			if (candidate != null)
				return annotationType.cast(candidate);
		}

		return null;
	}

	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
		for (Annotation each : getAnnotations())
			if (annotationType.isInstance(each))
				return annotationType.cast(each);
		return null;
	}
}