| 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 | 193 | public AbstractStepsFactory(Configuration configuration) { |
| 30 | 193 | this.configuration = configuration; |
| 31 | 193 | } |
| 32 | |
|
| 33 | |
public List<CandidateSteps> createCandidateSteps() { |
| 34 | 20 | List<Class<?>> types = stepsTypes(); |
| 35 | 20 | List<CandidateSteps> steps = new ArrayList<CandidateSteps>(); |
| 36 | 20 | for (Class<?> type : types) { |
| 37 | 22 | configuration.parameterConverters().addConverters( |
| 38 | |
methodReturningConverters(type)); |
| 39 | 22 | steps.add(new Steps(configuration, type, this)); |
| 40 | 22 | } |
| 41 | 20 | return steps; |
| 42 | |
} |
| 43 | |
|
| 44 | |
protected abstract List<Class<?>> stepsTypes(); |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private List<ParameterConverter> methodReturningConverters(Class<?> type) { |
| 50 | 22 | List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
| 51 | |
|
| 52 | 307 | for (Method method : type.getMethods()) { |
| 53 | 285 | if (method.isAnnotationPresent(AsParameterConverter.class)) { |
| 54 | 1 | converters.add(new MethodReturningConverter(method, type, this)); |
| 55 | |
} |
| 56 | |
} |
| 57 | |
|
| 58 | 22 | 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 | |
@SuppressWarnings("serial") |
| 83 | |
public static class StepsInstanceNotFound extends RuntimeException { |
| 84 | |
|
| 85 | |
public StepsInstanceNotFound(Class<?> type, InjectableStepsFactory stepsFactory) { |
| 86 | 0 | super("Steps instance not found for type "+type+" in factory "+stepsFactory); |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
} |
| 90 | |
} |