|
| 1 | +/* |
| 2 | + * Copyright 2010-2014 Benjamin Lings |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.codingwell.scalaguice |
| 17 | + |
| 18 | +import com.google.inject.Binder |
| 19 | +import com.google.inject.binder._ |
| 20 | +import com.google.inject.name.Names |
| 21 | +import java.lang.annotation.{Annotation => JAnnotation} |
| 22 | +import javax.inject.Provider |
| 23 | +import scala.reflect.ClassTag |
| 24 | + |
| 25 | +/** |
| 26 | + * Extensions for Guice's binding DSL. |
| 27 | + * These allow using a type parameter instead of `classOf[Foo]` |
| 28 | + * or `new TypeLiteral[Bar[Foo]] {}`. The extra methods are |
| 29 | + * named as those in the normal binding DSL suffixed with `Type`. |
| 30 | + * |
| 31 | + * For example, instead of |
| 32 | + * {{{ |
| 33 | + * binder.bind(new TypeLiteral[Bar[Foo]]{}).to(classOf[FooBarImpl]) |
| 34 | + * }}} |
| 35 | + * use |
| 36 | + * {{{ |
| 37 | + * import BindingExtensions._ |
| 38 | + * binder.bindType[Bar[Foo]].toType[FooImpl] |
| 39 | + * }}} |
| 40 | + * |
| 41 | + * '''Note''' This syntax allows binding to and from generic types. |
| 42 | + * It doesn't currently allow bindings between wildcard types because the |
| 43 | + * manifests for wildcard types don't provide access to type bounds. |
| 44 | + */ |
| 45 | +object BindingExtensions { |
| 46 | + |
| 47 | + implicit class ScalaBinder(val self: Binder) extends AnyVal { |
| 48 | + inline def bindType[T]: AnnotatedBindingBuilder[T] = self.bind(typeLiteral[T]) |
| 49 | + } |
| 50 | + |
| 51 | + implicit class ScalaScopedBindingBuilder(val self: ScopedBindingBuilder) extends AnyVal { |
| 52 | + def inType[TAnn <: JAnnotation : ClassTag](): Unit = self.in(cls[TAnn]) |
| 53 | + } |
| 54 | + |
| 55 | + implicit class ScalaLinkedBindingBuilder[T](val self: LinkedBindingBuilder[T]) extends AnyVal { |
| 56 | + inline def toType[TImpl <: T]: ScopedBindingBuilder = self.to(typeLiteral[TImpl]) |
| 57 | + |
| 58 | + def toProviderType[TProvider <: Provider[_ <: T] : ClassTag]: ScopedBindingBuilder = self.toProvider(cls[TProvider]) |
| 59 | + } |
| 60 | + |
| 61 | + implicit class ScalaAnnotatedBindingBuilder[T](val self: AnnotatedBindingBuilder[T]) extends AnyVal { |
| 62 | + def annotatedWithType[TAnn <: JAnnotation : ClassTag]: LinkedBindingBuilder[T] = self.annotatedWith(cls[TAnn]) |
| 63 | + } |
| 64 | + |
| 65 | + implicit class ScalaAnnotatedConstantBindingBuilder(val self: AnnotatedConstantBindingBuilder) extends AnyVal { |
| 66 | + def annotatedWithType[TAnn <: JAnnotation : ClassTag]: ConstantBindingBuilder = self.annotatedWith(cls[TAnn]) |
| 67 | + def annotatedWithName(name: String): ConstantBindingBuilder = self.annotatedWith(Names.named(name)) |
| 68 | + } |
| 69 | + |
| 70 | + implicit class ScalaConstantBindingBuilder(val self: ConstantBindingBuilder) extends AnyVal { |
| 71 | + def to[T: ClassTag](): Unit = self.to(cls[T]) |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments