public Module bindingModule() { return new KeyedManifest.Impl(method) { @Override public void configure() { final Binder binder = binder().withSource(method); if(!hasReturnValue()) { binder.addError("Cannot bind this method as a provider because it does not return a value"); return; } install(InjectableMethod.this); final ScopedBindingBuilder builder = binder.bind(providedKey).toProvider(asProvider()); if(scope != null) builder.in(scope); } }; }
@Override public ScopedBindingBuilder toProvider(Provider<? extends T> provider) { return new ScopedBindingBuilderImpl( delegate.toProvider(new Supplier<T>() { @Override public T get() { return provider.get(); } @Inject public void init(Injector injector) { injector.injectMembers(provider); } })); }
void bindFutureProvider(PrivateBinder binder) { binder = binder.withSource(source); ScopedBindingBuilder scoper = binder.bind(bindingKey).toProvider(this); if (isVoid(method)) { scoper.asEagerSingleton(); } else { if (scopeAnnotation != null) { scoper.in(scopeAnnotation); } if (exposedBinding) { binder.expose(bindingKey); } } }
protected void applyScoping(Binding<?> binding, final ScopedBindingBuilder scopedBindingBuilder) { binding.acceptScopingVisitor(new BindingScopingVisitor<Void>() { public Void visitEagerSingleton() { if (scopedBindingBuilder != null) { scopedBindingBuilder.asEagerSingleton(); } return null; } public Void visitScope(Scope scope) { scopedBindingBuilder.in(scope); return null; } public Void visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { scopedBindingBuilder.in(scopeAnnotation); return null; } public Void visitNoScoping() { // do nothing return null; } }); }
void configure(Binder binder) { binder = binder.withSource(method); SecondaryBinder<?, ?> sbinder = ThrowingProviderBinder.create(binder).bind(checkedProvider, key.getTypeLiteral()); if (key.getAnnotation() != null) { sbinder = sbinder.annotatedWith(key.getAnnotation()); } else if (key.getAnnotationType() != null) { sbinder = sbinder.annotatedWith(key.getAnnotationType()); } sbinder.scopeExceptions(scopeExceptions); ScopedBindingBuilder sbbuilder = sbinder.toProviderMethod(this); if (scopeAnnotation != null) { sbbuilder.in(scopeAnnotation); } if (exposed) { // the cast is safe 'cause the only binder we have implements PrivateBinder. If there's a // misplaced @Exposed, calling this will add an error to the binder's error queue ((PrivateBinder) binder).expose(sbinder.getKey()); } CheckedProvideUtils.validateExceptions( binder, exceptionTypes, sbinder.getExceptionTypes(), checkedProvider); }
void configure(Binder binder) { binder = binder.withSource(method); SecondaryBinder<?, ?> sbinder = ThrowingProviderBinder.create(binder) .bind(checkedProvider, key.getTypeLiteral()); if(key.getAnnotation() != null) { sbinder = sbinder.annotatedWith(key.getAnnotation()); } else if(key.getAnnotationType() != null) { sbinder = sbinder.annotatedWith(key.getAnnotationType()); } ScopedBindingBuilder sbbuilder = sbinder.toProviderMethod(this); if(scopeAnnotation != null) { sbbuilder.in(scopeAnnotation); } if (exposed) { // the cast is safe 'cause the only binder we have implements PrivateBinder. If there's a // misplaced @Exposed, calling this will add an error to the binder's error queue ((PrivateBinder) binder).expose(sbinder.getKey()); } CheckedProvideUtils.validateExceptions( binder, exceptionTypes, sbinder.getExceptionTypes(), checkedProvider); }
public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) { if (scopingAnnotation == Singleton.class || scopingAnnotation == javax.inject.Singleton.class) { return SINGLETON_ANNOTATION; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(scopingAnnotation); } @Override public Class<? extends Annotation> getScopeAnnotation() { return scopingAnnotation; } @Override public String toString() { return scopingAnnotation.getName(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scopingAnnotation); } }; }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
@Override public void configure(final Env env, final Config conf, final Binder binder) throws Exception { boolean dev = env.name().equals("dev"); ScopedBindingBuilder provider = binder.bind(M) .toProvider(APIProvider.class); if (!dev) { provider.asEagerSingleton(); } Path dir = Optional.ofNullable(basedir).orElse(Paths.get(conf.getString("user.dir"))); ApiParser parser = new ApiParser(dir, filter); customizer.forEach(parser::modify); binder.bind(ApiParser.class).toInstance(parser); String contextPath = conf.getString("application.path"); if (swaggerOptions != null) { swagger(contextPath, env.router(), swaggerOptions, swagger); } if (ramlOptions != null) { raml(contextPath, env.router(), ramlOptions, raml); } }
public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) { if (scopingAnnotation == Singleton.class || scopingAnnotation == javax.inject.Singleton.class) { return SINGLETON_ANNOTATION; } return new Scoping() { public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(scopingAnnotation); } @Override public Class<? extends Annotation> getScopeAnnotation() { return scopingAnnotation; } @Override public String toString() { return scopingAnnotation.getName(); } public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scopingAnnotation); } }; }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } return new Scoping() { public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
private void bindProvidersInScope(PrivateBinder privateBinder) { ScopedBindingBuilder scoper = privateBinder.bind(providersClass); if (scopeAnnotation != null) { scoper.in(scopeAnnotation); } for (EventualProvider<?> p : providers) { p.bindFutureProvider(privateBinder); } }
@Override public <T> Void visit(Binding<T> binding) { final Key<T> key = binding.getKey(); if (!keysToIntercept.contains(key)) { return super.visit(binding); } binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() { @Override public Void visit(UntargettedBinding<? extends T> untargettedBinding) { binder.addError("Cannot intercept bare binding of %s. " + "You may only intercept bindings that bind a class to something.", key); return null; } }); Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey))); ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey); // we scope the user's provider, not the interceptor. This is dangerous, // but convenient. It means that although the user's provider will live // in its proper scope, the intereptor gets invoked without a scope applyScoping(binding, scopedBindingBuilder); keysIntercepted.add(key); return null; }
@Override public <T> Void visit(Binding<T> binding) { final Key<T> key = binding.getKey(); if (!keysToIntercept.contains(key)) { return super.visit(binding); } binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() { @Override public Void visit(UntargettedBinding<? extends T> untargettedBinding) { binder.addError("Cannot intercept bare binding of %s. " + "You may only intercept bindings that bind a class to something.", key); return null; } }); Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey))); ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey); // we scope the user's provider, not the interceptor. This is dangerous, // but convenient. It means that although the user's provider will live // in its proper scope, the interceptor gets invoked without a scope applyScoping(binding, scopedBindingBuilder); keysIntercepted.add(key); return null; }
/** * Bind also the concrete model class, this binding is necesary to add the scope also to the concrete class. * @param description */ private void bindModelClass(StatisticDescriptor description) { Class<?> statisticModelClass = description.getModelClass(); ScopedBindingBuilder binderBuilder = bind(statisticModelClass); applyScope(description, binderBuilder); }
ScopedBindingBuilder toProviderMethod(CheckedProviderMethod<?> target) { Key<CheckedProviderMethod<?>> targetKey = Key.get(CHECKED_PROVIDER_METHOD_TYPE, UniqueAnnotations.create()); binder.bind(targetKey).toInstance(target); return toInternal(targetKey); }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } else if (scope == Scopes.NO_SCOPE) { return EXPLICITLY_UNSCOPED; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
ScopedBindingBuilder toProviderMethod(CheckedProviderMethod<?> target) { Key<CheckedProviderMethod> targetKey = Key.get(CheckedProviderMethod.class, UniqueAnnotations.create()); binder.bind(targetKey).toInstance(target); return toInternal(targetKey); }
public ScopedBindingBuilder toProvider( Key<? extends javax.inject.Provider<? extends T>> providerKey) { delegate.toProvider(providerKey); annotation.targetType = TargetType.PROVIDER_KEY; annotation.target = providerKey; return this; }
public <S extends T> ScopedBindingBuilder toConstructor( Constructor<S> constructor, TypeLiteral<? extends S> type) { delegate.toConstructor(constructor, type); annotation.targetType = TargetType.CONSTRUCTOR; annotation.target = InjectionPoint.forConstructor(constructor, type); return this; }
@Override public ScopedBindingBuilder to(final Class<? extends T> arg0) {// NOPMD return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder to(final TypeLiteral<? extends T> arg0) {// NOPMD return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder to(final Key<? extends T> arg0) {// NOPMD return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder toProvider(final Provider<? extends T> arg0) { return null; }
@Override public ScopedBindingBuilder toProvider(TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType) { return new NullScopedBindingBuilder(); }
@Override public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor) { return new NullScopedBindingBuilder(); }
@Override public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor, TypeLiteral<? extends S> type) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder toProvider(Class<? extends javax.inject.Provider<? extends T>> providerType) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder toProvider(Key<? extends javax.inject.Provider<? extends T>> providerKey) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder to(Class<? extends T> arg0) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder to(TypeLiteral<? extends T> arg0) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder to(Key<? extends T> arg0) { return new NullScopedBindingBuilder(); }
@Override public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> arg0) { return new NullScopedBindingBuilder(); }
@Override public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> arg0, TypeLiteral<? extends S> arg1) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder toProvider(Provider<? extends T> arg0) { return new NullScopedBindingBuilder(); }
@Override public ScopedBindingBuilder toProvider(Class<? extends javax.inject.Provider<? extends T>> arg0) { return new NullScopedBindingBuilder(); }