use of com.google.inject.spi.Dependency in project guice by google.
the class ProviderToInternalFactoryAdapter method get.
@Override
public T get() {
final Errors errors = new Errors();
try {
T t = injector.callInContext(new ContextualCallable<T>() {
@Override
public T call(InternalContext context) throws ErrorsException {
Dependency dependency = context.getDependency();
// binding, we'll fail properly elsewhere in the chain.
return internalFactory.get(errors, context, dependency, true);
}
});
errors.throwIfNewErrors(0);
return t;
} catch (ErrorsException e) {
throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
}
}
use of com.google.inject.spi.Dependency in project guice by google.
the class InjectorImpl method getProviderOrThrow.
<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors) throws ErrorsException {
Key<T> key = dependency.getKey();
BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
final InternalFactory<? extends T> internalFactory = binding.getInternalFactory();
final Object source = binding.getSource();
return new Provider<T>() {
@Override
public T get() {
final Errors errors = new Errors(dependency);
try {
T t = callInContext(new ContextualCallable<T>() {
@Override
public T call(InternalContext context) throws ErrorsException {
Dependency previous = context.pushDependency(dependency, source);
try {
return internalFactory.get(errors, context, dependency, false);
} finally {
context.popStateAndSetDependency(previous);
}
}
});
errors.throwIfNewErrors(0);
return t;
} catch (ErrorsException e) {
throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
}
}
@Override
public String toString() {
return internalFactory.toString();
}
};
}
use of com.google.inject.spi.Dependency in project guice by google.
the class SingleFieldInjector method inject.
@Override
public void inject(Errors errors, InternalContext context, Object o) {
errors = errors.withSource(dependency);
Dependency previous = context.pushDependency(dependency, binding.getSource());
try {
Object value = binding.getInternalFactory().get(errors, context, dependency, false);
field.set(o, value);
} catch (ErrorsException e) {
errors.withSource(injectionPoint).merge(e.getErrors());
} catch (IllegalAccessException e) {
// a security manager is blocking us, we're hosed
throw new AssertionError(e);
} finally {
context.popStateAndSetDependency(previous);
}
}
use of com.google.inject.spi.Dependency in project guice by google.
the class SingleParameterInjector method inject.
T inject(Errors errors, InternalContext context) throws ErrorsException {
Dependency<T> localDependency = dependency;
Dependency previous = context.pushDependency(localDependency, source);
try {
return factory.get(errors.withSource(localDependency), context, localDependency, false);
} finally {
context.popStateAndSetDependency(previous);
}
}
use of com.google.inject.spi.Dependency in project guice by google.
the class Jsr330Test method testGuicifyWithDependencies.
public void testGuicifyWithDependencies() {
Provider<String> jsr330Provider = new Provider<String>() {
@Inject
double d;
int i;
@Inject
void injectMe(int i) {
this.i = i;
}
@Override
public String get() {
return d + "-" + i;
}
};
final com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider);
assertTrue(guicified instanceof HasDependencies);
Set<Dependency<?>> actual = ((HasDependencies) guicified).getDependencies();
validateDependencies(actual, jsr330Provider.getClass());
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toProvider(guicified);
bind(int.class).toInstance(1);
bind(double.class).toInstance(2.0d);
}
});
Binding<String> binding = injector.getBinding(String.class);
assertEquals("2.0-1", binding.getProvider().get());
validateDependencies(actual, jsr330Provider.getClass());
}
Aggregations