• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  *
3  */
4 package org.junit.experimental.theories;
5 
6 import java.lang.annotation.Annotation;
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.Method;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.List;
12 
13 public class ParameterSignature {
signatures(Method method)14 	public static ArrayList<ParameterSignature> signatures(Method method) {
15 		return signatures(method.getParameterTypes(), method
16 				.getParameterAnnotations());
17 	}
18 
signatures(Constructor<?> constructor)19 	public static List<ParameterSignature> signatures(Constructor<?> constructor) {
20 		return signatures(constructor.getParameterTypes(), constructor
21 				.getParameterAnnotations());
22 	}
23 
signatures( Class<?>[] parameterTypes, Annotation[][] parameterAnnotations)24 	private static ArrayList<ParameterSignature> signatures(
25 			Class<?>[] parameterTypes, Annotation[][] parameterAnnotations) {
26 		ArrayList<ParameterSignature> sigs= new ArrayList<ParameterSignature>();
27 		for (int i= 0; i < parameterTypes.length; i++) {
28 			sigs.add(new ParameterSignature(parameterTypes[i],
29 					parameterAnnotations[i]));
30 		}
31 		return sigs;
32 	}
33 
34 	private final Class<?> type;
35 
36 	private final Annotation[] annotations;
37 
ParameterSignature(Class<?> type, Annotation[] annotations)38 	private ParameterSignature(Class<?> type, Annotation[] annotations) {
39 		this.type= type;
40 		this.annotations= annotations;
41 	}
42 
canAcceptType(Class<?> candidate)43 	public boolean canAcceptType(Class<?> candidate) {
44 		return type.isAssignableFrom(candidate);
45 	}
46 
getType()47 	public Class<?> getType() {
48 		return type;
49 	}
50 
getAnnotations()51 	public List<Annotation> getAnnotations() {
52 		return Arrays.asList(annotations);
53 	}
54 
canAcceptArrayType(Class<?> type)55 	public boolean canAcceptArrayType(Class<?> type) {
56 		return type.isArray() && canAcceptType(type.getComponentType());
57 	}
58 
hasAnnotation(Class<? extends Annotation> type)59 	public boolean hasAnnotation(Class<? extends Annotation> type) {
60 		return getAnnotation(type) != null;
61 	}
62 
findDeepAnnotation(Class<T> annotationType)63 	public <T extends Annotation> T findDeepAnnotation(Class<T> annotationType) {
64 		Annotation[] annotations2= annotations;
65 		return findDeepAnnotation(annotations2, annotationType, 3);
66 	}
67 
findDeepAnnotation( Annotation[] annotations, Class<T> annotationType, int depth)68 	private <T extends Annotation> T findDeepAnnotation(
69 			Annotation[] annotations, Class<T> annotationType, int depth) {
70 		if (depth == 0)
71 			return null;
72 		for (Annotation each : annotations) {
73 			if (annotationType.isInstance(each))
74 				return annotationType.cast(each);
75 			Annotation candidate= findDeepAnnotation(each.annotationType()
76 					.getAnnotations(), annotationType, depth - 1);
77 			if (candidate != null)
78 				return annotationType.cast(candidate);
79 		}
80 
81 		return null;
82 	}
83 
getAnnotation(Class<T> annotationType)84 	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
85 		for (Annotation each : getAnnotations())
86 			if (annotationType.isInstance(each))
87 				return annotationType.cast(each);
88 		return null;
89 	}
90 }