-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actually try to inject values when determining if we can resolve a pa…
…rameter. Fix #2
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 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
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 |
---|---|---|
|
@@ -7,10 +7,12 @@ | |
import com.google.common.reflect.TypeToken; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.BindingAnnotation; | ||
import com.google.inject.ConfigurationException; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Key; | ||
import com.google.inject.Module; | ||
import com.google.inject.ProvisionException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.Constructor; | ||
|
@@ -199,7 +201,16 @@ public boolean supportsParameter(ParameterContext parameterContext, | |
extensionContext.getTestClass(), | ||
parameter); | ||
Optional<Injector> optInjector = getInjectorForParameterResolution(extensionContext); | ||
return optInjector.map(injector -> injector.getExistingBinding(key)).isPresent(); | ||
return optInjector.filter(injector -> { | ||
try { | ||
injector.getInstance(key); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JeffFaer
Author
Owner
|
||
return true; | ||
} catch (ConfigurationException | ProvisionException e) { | ||
// If we throw a ParameterResolutionException here instead of returning false, we'll block | ||
// other ParameterResolvers from being able to work. | ||
return false; | ||
} | ||
}).isPresent(); | ||
} | ||
|
||
@Override | ||
|
@@ -250,7 +261,8 @@ private static Key<?> getKey(Optional<Class<?>> containingElement, Parameter par | |
} | ||
|
||
/** | ||
* @throws IllegalArgumentException if the given element has more than one binding annotation. | ||
* @throws IllegalArgumentException if the given element has more than one binding | ||
* annotation. | ||
*/ | ||
private static Optional<? extends Annotation> getOnlyBindingAnnotation(AnnotatedElement element) { | ||
return Optional.ofNullable(Iterables.getOnlyElement(getBindingAnnotations(element), null)); | ||
|
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
Is this really the best way to determine if we can get an instance? If obtaining the instance is a heavy call this can result in overhead (in some cases alot more than you would want). IE. a DB connection or some other heavy instance. Also it isn't really intuitive that a method that is supposed to determine if you can retrieve an instance of something, actually attempts to retrieve an instance.