use of javax.inject.Provider 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());
}
use of javax.inject.Provider in project EventHub by Codecademy.
the class Module method getEventHubHandler.
@Provides
private EventHubHandler getEventHubHandler(Injector injector, EventHub eventHub) throws ClassNotFoundException {
Map<String, Provider<Command>> commandsMap = Maps.newHashMap();
Reflections reflections = new Reflections(PACKAGE_NAME);
Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class);
for (Class<? extends Command> commandClass : commandClasses) {
String path = commandClass.getAnnotation(Path.class).value();
// noinspection unchecked
commandsMap.put(path, (Provider<Command>) injector.getProvider(commandClass));
}
return new EventHubHandler(eventHub, commandsMap);
}
use of javax.inject.Provider in project auto by google.
the class FactoryWriter method addConstructorAndProviderFields.
private void addConstructorAndProviderFields(TypeSpec.Builder factory, FactoryDescriptor descriptor) {
MethodSpec.Builder constructor = constructorBuilder().addAnnotation(Inject.class);
if (descriptor.publicType()) {
constructor.addModifiers(PUBLIC);
}
Iterator<ProviderField> providerFields = descriptor.providers().values().iterator();
for (int argumentIndex = 1; providerFields.hasNext(); argumentIndex++) {
ProviderField provider = providerFields.next();
TypeName typeName = resolveTypeName(provider.key().type().get()).box();
TypeName providerType = ParameterizedTypeName.get(ClassName.get(Provider.class), typeName);
factory.addField(providerType, provider.name(), PRIVATE, FINAL);
if (provider.key().qualifier().isPresent()) {
// only qualify the constructor parameter
providerType = providerType.annotated(AnnotationSpec.get(provider.key().qualifier().get()));
}
constructor.addParameter(providerType, provider.name());
constructor.addStatement("this.$1L = checkNotNull($1L, $2L)", provider.name(), argumentIndex);
}
factory.addMethod(constructor.build());
}
use of javax.inject.Provider in project tomee by apache.
the class CdiPlugin method getSessionBeanProxy.
@Override
public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) {
Object instance = cacheProxies.get(inBean);
if (instance != null) {
return instance;
}
synchronized (inBean) {
// singleton for the app so safe to sync on it
instance = cacheProxies.get(inBean);
if (instance != null) {
return instance;
}
final Class<? extends Annotation> scopeClass = inBean.getScope();
final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean;
final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext;
if (scopeClass == null || Dependent.class == scopeClass) {
// no need to add any layer, null = @New
return cdiEjbBean.createEjb(cc);
}
// only stateful normally
final InstanceBean<Object> bean = new InstanceBean<>(cdiEjbBean);
if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) {
final BeanContext beanContext = cdiEjbBean.getBeanContext();
final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean);
if (!beanContext.isLocalbean()) {
final List<Class> interfaces = new ArrayList<>();
final InterfaceType type = beanContext.getInterfaceType(interfce);
if (type != null) {
interfaces.addAll(beanContext.getInterfaces(type));
} else {
// can happen when looked up from impl instead of API in OWB -> default to business local
interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL));
}
interfaces.add(Serializable.class);
interfaces.add(IntraVmProxy.class);
if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) {
interfaces.add(BeanContext.Removable.class);
}
try {
instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
try {
return method.invoke(provider.get(), args);
} catch (final InvocationTargetException ite) {
throw ite.getCause();
}
}
});
} catch (final IllegalAccessException e) {
throw new OpenEJBRuntimeException(e);
}
} else {
final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass());
instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider);
}
cacheProxies.put(inBean, instance);
} else {
final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass);
instance = context.get(bean, cc);
}
bean.setOwbProxy(instance);
return instance;
}
}
use of javax.inject.Provider in project graylog2-server by Graylog2.
the class ElasticsearchBackendTest method setup.
@BeforeClass
public static void setup() {
Map<String, Provider<ESSearchTypeHandler<? extends SearchType>>> handlers = Maps.newHashMap();
handlers.put(MessageList.NAME, () -> new ESMessageList(new QueryStringDecorators.Fake()));
final FieldTypesLookup fieldTypesLookup = mock(FieldTypesLookup.class);
backend = new ElasticsearchBackend(handlers, null, mock(IndexLookup.class), new QueryStringDecorators.Fake(), (elasticsearchBackend, ssb, job, query) -> new ESGeneratedQueryContext(elasticsearchBackend, ssb, job, query, fieldTypesLookup), false);
}
Aggregations