1 | |
package org.jbehave.core.steps; |
2 | |
|
3 | |
import java.lang.annotation.Annotation; |
4 | |
import java.lang.reflect.Method; |
5 | |
import java.lang.reflect.Type; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
|
9 | |
import org.jbehave.core.annotations.AsParameterConverter; |
10 | |
import org.jbehave.core.configuration.Configuration; |
11 | |
import org.jbehave.core.steps.ParameterConverters.MethodReturningConverter; |
12 | |
import org.jbehave.core.steps.ParameterConverters.ParameterConverter; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public abstract class AbstractStepsFactory implements InjectableStepsFactory { |
26 | |
|
27 | |
private final Configuration configuration; |
28 | |
|
29 | 15 | public AbstractStepsFactory(Configuration configuration) { |
30 | 15 | this.configuration = configuration; |
31 | 15 | } |
32 | |
|
33 | |
public List<CandidateSteps> createCandidateSteps() { |
34 | 14 | List<Object> stepsInstances = stepsInstances(); |
35 | 14 | List<CandidateSteps> steps = new ArrayList<CandidateSteps>(); |
36 | 14 | for (Object instance : stepsInstances) { |
37 | 12 | configuration.parameterConverters().addConverters( |
38 | |
methodReturningConverters(instance)); |
39 | 12 | steps.add(new Steps(configuration, instance)); |
40 | |
} |
41 | 14 | return steps; |
42 | |
} |
43 | |
|
44 | |
protected abstract List<Object> stepsInstances(); |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private List<ParameterConverter> methodReturningConverters(Object instance) { |
50 | 12 | List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
51 | |
|
52 | 130 | for (Method method : instance.getClass().getMethods()) { |
53 | 118 | if (method.isAnnotationPresent(AsParameterConverter.class)) { |
54 | 1 | converters.add(new MethodReturningConverter(method, instance)); |
55 | |
} |
56 | |
} |
57 | |
|
58 | 12 | return converters; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected boolean hasAnnotatedMethods(Type type) { |
69 | 2 | if (type instanceof Class<?>) { |
70 | 11 | for (Method method : ((Class<?>) type).getMethods()) { |
71 | 10 | for (Annotation annotation : method.getAnnotations()) { |
72 | 1 | if (annotation.annotationType().getName().startsWith( |
73 | |
"org.jbehave.core.annotations")) { |
74 | 1 | return true; |
75 | |
} |
76 | |
} |
77 | |
} |
78 | |
} |
79 | 1 | return false; |
80 | |
} |
81 | |
|
82 | |
} |