Skip to content

Commit 8ec9406

Browse files
author
Russ Remple
committed
Scala 3
1 parent 8d01257 commit 8ec9406

14 files changed

+2176
-14
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ repo/
1111
*.swp
1212
.idea*
1313
.DS_Store
14-
.bsp/
14+
.bsp/

build.sbt

+21-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description := "Scala syntax for Guice"
44

55
organization := "net.codingwell"
66

7-
version := "5.1.0"
7+
version := "5.2.0"
88
versionScheme := Some("pvp")
99

1010
licenses := Seq("Apache 2" -> new URL("http://www.apache.org/licenses/LICENSE-2.0.txt"))
@@ -13,28 +13,37 @@ homepage := Some(url("https://github.com/codingwell/scala-guice"))
1313

1414
libraryDependencies ++= Seq(
1515
"com.google.inject" % "guice" % "5.1.0",
16-
"org.scala-lang" % "scala-reflect" % scalaVersion.value
16+
"org.scalatest" %% "scalatest" % "3.2.9" % "test",
17+
"com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile"
1718
)
1819

19-
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % "test"
20-
21-
libraryDependencies += "com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile"
22-
23-
libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.1.2"
20+
libraryDependencies ++= {
21+
CrossVersion.partialVersion(scalaVersion.value) match {
22+
case Some((2, n)) => Seq(
23+
"org.scala-lang.modules" %% "scala-collection-compat" % "2.1.2",
24+
"org.scala-lang" % "scala-reflect" % scalaVersion.value
25+
)
26+
case _ => Seq.empty
27+
}
28+
}
2429

2530
autoAPIMappings := true
2631

27-
scalaVersion := "2.13.8"
32+
//scalaVersion := "2.13.8"
2833

29-
crossScalaVersions := Seq("2.11.12", "2.12.15", "2.13.8")
34+
val scala3 = "3.2.2"
35+
scalaVersion := scala3 // for IDE
36+
//crossScalaVersions := Seq(scala3)
37+
crossScalaVersions := Seq("2.11.12", "2.12.15", "2.13.8", scala3)
3038

3139
scalacOptions := Seq("-unchecked", "-deprecation", "-feature")
3240

33-
Compile / unmanagedSourceDirectories += {
41+
Compile / unmanagedSourceDirectories ++= {
3442
val sourceDir = (Compile / sourceDirectory).value
3543
CrossVersion.partialVersion(scalaVersion.value) match {
36-
case Some((2, n)) if n >= 13 => sourceDir / "scala-2.13+"
37-
case _ => sourceDir / "scala-2.12-"
44+
case Some((2, n)) if n >= 13 => Seq(sourceDir / "scala-2.13+")
45+
case Some((3, _)) => Seq(sourceDir / "scala-2.13+")
46+
case _ => Seq(sourceDir / "scala-2.12-")
3847
}
3948
}
4049

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.{Binding, Injector, Key, Provider}
19+
import java.lang.annotation.Annotation
20+
import net.codingwell.scalaguice.KeyExtensions._
21+
import scala.reflect.ClassTag
22+
23+
object InjectorExtensions {
24+
25+
implicit class ScalaInjector(val self: Injector) /*extends AnyVal*/ {
26+
inline def instance[T]: T = self.getInstance(typeLiteral[T].toKey)
27+
inline def instance[T](ann: Annotation): T = self.getInstance(typeLiteral[T].annotatedWith(ann))
28+
inline def instance[T, Ann <: Annotation : ClassTag]: T = self.getInstance(typeLiteral[T].annotatedWith[Ann])
29+
30+
inline def existingBinding[T]: Option[Binding[T]] = existingBinding(typeLiteral[T].toKey)
31+
inline def existingBinding[T](ann: Annotation): Option[Binding[T]] = existingBinding(typeLiteral[T].annotatedWith(ann))
32+
inline def existingBinding[T, Ann <: Annotation : ClassTag]: Option[Binding[T]] = existingBinding(typeLiteral[T].annotatedWith[Ann])
33+
def existingBinding[T](key: Key[T]): Option[Binding[T]] = Option(self.getExistingBinding(key))
34+
35+
inline def provider[T]: Provider[T] = self.getProvider(typeLiteral[T].toKey)
36+
inline def provider[T](ann: Annotation): Provider[T] = self.getProvider(typeLiteral[T].annotatedWith(ann))
37+
inline def provider[T, Ann <: Annotation : ClassTag]: Provider[T] = self.getProvider(typeLiteral[T].annotatedWith[Ann])
38+
}
39+
}

0 commit comments

Comments
 (0)