use of com.google.inject.Provider in project roboguice by roboguice.
the class ElementsTest method testBindToProvider.
public void testBindToProvider() {
final Provider<String> aProvider = new Provider<String>() {
public String get() {
return "A";
}
};
final javax.inject.Provider<Integer> intJavaxProvider = new javax.inject.Provider<Integer>() {
public Integer get() {
return 42;
}
};
final javax.inject.Provider<Double> doubleJavaxProvider = new javax.inject.Provider<Double>() {
@javax.inject.Inject
String string;
public Double get() {
return 42.42;
}
};
checkModule(new AbstractModule() {
protected void configure() {
bind(String.class).toProvider(aProvider);
bind(Integer.class).toProvider(intJavaxProvider);
bind(Double.class).toProvider(doubleJavaxProvider);
bind(List.class).toProvider(ListProvider.class);
bind(Collection.class).toProvider(Key.get(ListProvider.class));
bind(Iterable.class).toProvider(new TypeLiteral<TProvider<List>>() {
});
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderInstanceBinding);
assertEquals(Key.get(String.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderInstanceBinding<? extends T> binding) {
assertSame(aProvider, binding.getUserSuppliedProvider());
assertSame(aProvider, binding.getProviderInstance());
return null;
}
});
return null;
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderInstanceBinding);
assertEquals(Key.get(Integer.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderInstanceBinding<? extends T> binding) {
assertSame(intJavaxProvider, binding.getUserSuppliedProvider());
assertEquals(42, binding.getProviderInstance().get());
// we don't wrap this w/ dependencies if there were none.
assertFalse(binding.getProviderInstance() instanceof HasDependencies);
return null;
}
});
return null;
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderInstanceBinding);
assertEquals(Key.get(Double.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderInstanceBinding<? extends T> binding) {
assertSame(doubleJavaxProvider, binding.getUserSuppliedProvider());
assertEquals(42.42, binding.getProviderInstance().get());
// we do wrap it with dependencies if there were some.
assertTrue(binding.getProviderInstance() instanceof HasDependencies);
Set<Dependency<?>> deps = ((HasDependencies) binding.getProviderInstance()).getDependencies();
assertEquals(1, deps.size());
assertEquals(String.class, deps.iterator().next().getKey().getTypeLiteral().getRawType());
return null;
}
});
return null;
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderKeyBinding);
assertEquals(Key.get(List.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderKeyBinding<? extends T> binding) {
assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
return null;
}
});
return null;
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderKeyBinding);
assertEquals(Key.get(Collection.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderKeyBinding<? extends T> binding) {
assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
return null;
}
});
return null;
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertTrue(command instanceof ProviderKeyBinding);
assertEquals(Key.get(Iterable.class), command.getKey());
command.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ProviderKeyBinding<? extends T> binding) {
assertEquals(new Key<TProvider<List>>() {
}, binding.getProviderKey());
return null;
}
});
return null;
}
});
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class BinderTest method testBindingToProvider.
public void testBindingToProvider() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Provider<String>>() {
}).toInstance(Providers.of("A"));
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class Providers method guicify.
/**
* Returns a Guice-friendly {@code com.google.inject.Provider} for the given
* JSR-330 {@code javax.inject.Provider}. The converse method is unnecessary,
* since Guice providers directly implement the JSR-330 interface.
*
* @since 3.0
*/
public static <T> Provider<T> guicify(javax.inject.Provider<T> provider) {
if (provider instanceof Provider) {
return (Provider<T>) provider;
}
final javax.inject.Provider<T> delegate = checkNotNull(provider, "provider");
// Ensure that we inject all injection points from the delegate provider.
Set<InjectionPoint> injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass());
if (injectionPoints.isEmpty()) {
return new GuicifiedProvider<T>(delegate);
} else {
Set<Dependency<?>> mutableDeps = Sets.newHashSet();
for (InjectionPoint ip : injectionPoints) {
mutableDeps.addAll(ip.getDependencies());
}
final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
return new GuicifiedProviderWithDependencies<T>(dependencies, delegate);
}
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class BoundFieldModuleTest method testExplicitlyBoundRawProviderCanBeBound.
public void testExplicitlyBoundRawProviderCanBeBound() {
final Integer testValue = 1024;
Object instance = new Object() {
@Bind(to = Integer.class)
private Provider anIntProvider = new Provider() {
@Override
public Object get() {
return testValue;
}
};
};
BoundFieldModule module = BoundFieldModule.of(instance);
Injector injector = Guice.createInjector(module);
assertEquals(testValue, injector.getInstance(Integer.class));
}
use of com.google.inject.Provider in project roboguice by roboguice.
the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.
public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
bindScope(BadScope.class, new Scope() {
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure");
}
};
}
});
}
});
try {
injector.getInstance(Key.get(remoteProviderOfFoo)).get();
fail();
} catch (ProvisionException pe) {
assertEquals(2, pe.getErrorMessages().size());
List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
}
}
Aggregations