Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Needle] Move ObjectFactory to io.cucumber.needle
- Loading branch information
1 parent
1659ef8
commit 4f2ded5
Showing
39 changed files
with
238 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 4 additions & 15 deletions
19
needle/src/main/java/cucumber/api/needle/InjectionProviderInstancesSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,11 @@ | ||
package cucumber.api.needle; | ||
|
||
import cucumber.runtime.java.needle.NeedleFactory; | ||
import de.akquinet.jbosscc.needle.NeedleTestcase; | ||
import de.akquinet.jbosscc.needle.injection.InjectionProvider; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* <a href="http://javadocs.techempower.com/jdk18/api/java/util/function/Supplier.html">Supplies</a> a Set of | ||
* InjectionProvider instances that are created outside the {@link NeedleFactory} lifecycle. | ||
* InjectionProvider instances that are created outside the {@link io.cucumber.needle.NeedleFactory} lifecycle. | ||
* @deprecated use {@code io.cucumber.needle.api.InjectionProviderInstancesSupplier} instead | ||
*/ | ||
public interface InjectionProviderInstancesSupplier { | ||
@Deprecated | ||
public interface InjectionProviderInstancesSupplier extends io.cucumber.needle.InjectionProviderInstancesSupplier { | ||
|
||
/** | ||
* <a href="http://javadocs.techempower.com/jdk18/api/java/util/function/Supplier.html">Supplies</a> a Set of | ||
* InjectionProvider instances that are created outside the {@link NeedleFactory} lifecycle. | ||
* | ||
* @return InjectionProviders that can be added to {@link NeedleTestcase} | ||
*/ | ||
Set<InjectionProvider<?>> get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
.../java/cucumber/runtime/java/needle/config/CollectInjectionProvidersFromStepsInstance.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
needle/src/main/java/io/cucumber/needle/CollectInjectionProvidersFromStepsInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.cucumber.needle; | ||
|
||
import de.akquinet.jbosscc.needle.injection.InjectionProvider; | ||
import de.akquinet.jbosscc.needle.reflection.ReflectionUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Collects {@link InjectionProvider} instances. | ||
*/ | ||
enum CollectInjectionProvidersFromStepsInstance { | ||
/** | ||
* stateless Singleton | ||
*/ | ||
INSTANCE; | ||
|
||
/** | ||
* Logger for the factory. | ||
*/ | ||
private final Logger logger = LoggerFactory.getLogger(NeedleFactory.class); | ||
|
||
/** | ||
* Collect providers direct in the step definition. | ||
* | ||
* @param instance step definition instance | ||
* @return collected injection providers. | ||
*/ | ||
final <T> InjectionProvider<?>[] apply(final T instance) { | ||
final Set<InjectionProvider<?>> allProviders = new LinkedHashSet<>(); | ||
for (final Field field : ReflectionUtil.getAllFieldsWithAnnotation(instance, NeedleInjectionProvider.class)) { | ||
final Set<InjectionProvider<?>> providers = getInjectionProviders(field, instance); | ||
allProviders.addAll(providers); | ||
} | ||
for (final Field field : ReflectionUtil.getAllFieldsWithAnnotation(instance, cucumber.api.needle.NeedleInjectionProvider.class)) { | ||
final Set<InjectionProvider<?>> providers = getInjectionProviders(field, instance); | ||
allProviders.addAll(providers); | ||
} | ||
if (logger.isTraceEnabled()) { | ||
logger.trace("Adding {} InjectionProvider instances.", allProviders.size()); | ||
} | ||
|
||
return allProviders.toArray(new InjectionProvider<?>[0]); | ||
} | ||
|
||
private <T> Set<InjectionProvider<?>> getInjectionProviders(Field field, T instance) { | ||
final Set<InjectionProvider<?>> providers = new LinkedHashSet<>(); | ||
field.setAccessible(true); | ||
try { | ||
final Object value = field.get(instance); | ||
if (value instanceof InjectionProvider<?>[]) { | ||
providers.addAll(Arrays.asList((InjectionProvider<?>[]) value)); | ||
} else if (value instanceof InjectionProvider) { | ||
providers.add((InjectionProvider<?>) value); | ||
} else if (value instanceof InjectionProviderInstancesSupplier) { | ||
providers.addAll(((InjectionProviderInstancesSupplier) value).get()); | ||
} else { | ||
throw new IllegalStateException("Fields annotated with NeedleInjectionProviders must be of type " | ||
+ "InjectionProviderInstancesSupplier, InjectionProvider " + "or InjectionProvider[]"); | ||
} | ||
} catch (final Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
return providers; | ||
} | ||
|
||
} |
8 changes: 4 additions & 4 deletions
8
...g/CreateInstanceByDefaultConstructor.java → ...e/CreateInstanceByDefaultConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
needle/src/main/java/io/cucumber/needle/InjectionProviderInstancesSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.cucumber.needle; | ||
|
||
import de.akquinet.jbosscc.needle.NeedleTestcase; | ||
import de.akquinet.jbosscc.needle.injection.InjectionProvider; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* <a href="http://javadocs.techempower.com/jdk18/api/java/util/function/Supplier.html">Supplies</a> a Set of | ||
* InjectionProvider instances that are created outside the {@link NeedleFactory} lifecycle. | ||
*/ | ||
public interface InjectionProviderInstancesSupplier { | ||
|
||
/** | ||
* <a href="http://javadocs.techempower.com/jdk18/api/java/util/function/Supplier.html">Supplies</a> a Set of | ||
* InjectionProvider instances that are created outside the {@link NeedleFactory} lifecycle. | ||
* | ||
* @return InjectionProviders that can be added to {@link NeedleTestcase} | ||
*/ | ||
Set<InjectionProvider<?>> get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.