use of com.google.inject.internal.ProviderMethod in project google-gin by gwtplus.
the class ProviderMethodBindingTest method createProviderMethodBinding.
private ProviderMethodBinding createProviderMethodBinding(Object instance, Method method) {
// Ew, but the constructor of ProviderMethod is private, and this is a
// simple way to create a custom one.
IMocksControl control = EasyMock.createControl();
@SuppressWarnings("unchecked") ProviderMethod<Integer> providerMethod = (ProviderMethod<Integer>) control.createMock("providerMethod", ProviderMethod.class);
EasyMock.expect(providerMethod.getInstance()).andStubReturn(instance);
EasyMock.expect(providerMethod.getKey()).andStubReturn(Key.get(Integer.class));
EasyMock.expect(providerMethod.getMethod()).andStubReturn(method);
control.replay();
// Note: guiceUtil and methodCallUtil are used in parts of the binding class
// that we don't test currently, so are set to null. When tests for
// getCreationStatements() and getDependencies() are written, concrete
// values (mocks?) will be required.
ProviderMethodBinding result = new ProviderMethodBinding(errorManager, null, null, providerMethod, Context.forText("dummy context"));
control.verify();
return result;
}
use of com.google.inject.internal.ProviderMethod in project roboguice by roboguice.
the class ShortNameFactoryTest method testGetInstanceName_providerMethod.
public void testGetInstanceName_providerMethod() throws Exception {
final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];
Injector injector = Guice.createInjector(new ProvidingModule());
injector.getBinding(Integer.class).acceptTargetVisitor(new DefaultBindingTargetVisitor<Object, Void>() {
@SuppressWarnings("unchecked")
@Override
public Void visit(ProviderInstanceBinding<?> binding) {
methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
return null;
}
});
assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
use of com.google.inject.internal.ProviderMethod in project guice by google.
the class ShortNameFactoryTest method testGetInstanceName_providerMethod.
public void testGetInstanceName_providerMethod() throws Exception {
final List<ProviderMethod<?>> methodHolder = new ArrayList<>(1);
Injector injector = Guice.createInjector(new ProvidingModule());
injector.getBinding(Integer.class).acceptTargetVisitor(new DefaultBindingTargetVisitor<Object, Void>() {
@Override
public Void visit(ProviderInstanceBinding<?> binding) {
methodHolder.add((ProviderMethod) binding.getUserSuppliedProvider());
return null;
}
});
assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder.get(0)));
}
use of com.google.inject.internal.ProviderMethod in project guice by google.
the class ProviderMethodsTest method testNonModuleProviderMethods.
@Test
public void testNonModuleProviderMethods() {
final Object methodsObject = new Object() {
@Provides
@Named("foo")
String provideFoo() {
return "foo-value";
}
};
Module module = new AbstractModule() {
@Override
protected void configure() {
install(ProviderMethodsModule.forObject(methodsObject));
}
};
Injector injector = Guice.createInjector(module);
Key<String> key = Key.get(String.class, Names.named("foo"));
assertEquals("foo-value", injector.getInstance(key));
// Test the provider method object itself. This makes sure getInstance works, since GIN uses it
List<Element> elements = Elements.getElements(module);
assertEquals(1, elements.size());
Element element = elements.get(0);
assertTrue(element + " instanceof ProviderInstanceBinding", element instanceof ProviderInstanceBinding);
ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
javax.inject.Provider<?> provider = binding.getUserSuppliedProvider();
assertTrue(provider instanceof ProviderMethod);
assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
assertSame(provider, binding.getProviderInstance());
}
use of com.google.inject.internal.ProviderMethod in project ninja by ninjaframework.
the class LifecycleServiceImpl method start.
@Override
public void start() {
startTime = System.currentTimeMillis();
log.info("Starting Ninja application...");
state = State.STARTING;
// until they are instantiated that LifecycleSupport has an opportunity to register them.
for (final Binding binding : injector.getBindings().values()) {
binding.acceptScopingVisitor(new DefaultBindingScopingVisitor() {
@Override
public Object visitEagerSingleton() {
injector.getInstance(binding.getKey());
return null;
}
@Override
public Object visitScope(Scope scope) {
if (scope.equals(Scopes.SINGLETON)) {
Object target = injector.getInstance(binding.getKey());
if (binding instanceof ProviderInstanceBinding) {
Provider providerInstance = ((ProviderInstanceBinding) binding).getProviderInstance();
if (providerInstance instanceof ProviderMethod) {
// @Provides methods don't get picked up by TypeListeners, so we need to manually register them
if (lifecycleSupport.hasLifecycleMethod(target.getClass())) {
lifecycleSupport.registerLifecycle(target);
}
}
}
}
return null;
}
});
}
lifecycleRegister.start();
long time = System.currentTimeMillis() - startTime;
log.info("Ninja application started in {}ms", time);
state = lifecycleRegister.isStarted() ? State.STARTED : State.STOPPED;
}
Aggregations