| 1 | |
package org.jbehave.core.steps.guice; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import org.jbehave.core.configuration.Configuration; |
| 7 | |
import org.jbehave.core.steps.AbstractStepsFactory; |
| 8 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
| 9 | |
|
| 10 | |
import com.google.inject.Binding; |
| 11 | |
import com.google.inject.Injector; |
| 12 | |
import com.google.inject.Key; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public class GuiceStepsFactory extends AbstractStepsFactory { |
| 24 | |
|
| 25 | |
private final Injector injector; |
| 26 | |
|
| 27 | |
public GuiceStepsFactory(Configuration configuration, Injector injector) { |
| 28 | 6 | super(configuration); |
| 29 | 6 | this.injector = injector; |
| 30 | 6 | } |
| 31 | |
|
| 32 | |
@Override |
| 33 | |
protected List<Object> stepsInstances() { |
| 34 | 6 | List<Object> steps = new ArrayList<Object>(); |
| 35 | 6 | addInstances(injector, steps); |
| 36 | 6 | return steps; |
| 37 | |
} |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private void addInstances(Injector injector, List<Object> steps) { |
| 46 | 58 | for (Binding<?> binding : injector.getBindings().values()) { |
| 47 | 208 | Key<?> key = binding.getKey(); |
| 48 | 208 | if (hasAnnotatedMethods(key.getTypeLiteral().getType())) { |
| 49 | 4 | steps.add(injector.getInstance(key)); |
| 50 | |
} |
| 51 | 208 | } |
| 52 | 58 | if (injector.getParent() != null) { |
| 53 | 52 | addInstances(injector.getParent(), steps); |
| 54 | |
} |
| 55 | 58 | } |
| 56 | |
} |