-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c00ecb
commit ca3927a
Showing
5 changed files
with
131 additions
and
22 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
shared/src/main/scala/magnolify/shared/AnnotationType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2021 Spotify AB. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package magnolify.shared | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros._ | ||
|
||
sealed case class AnnotationType[T](annotations: List[Any]) | ||
|
||
object AnnotationType { | ||
implicit def apply[T]: AnnotationType[T] = macro applyImpl[T] | ||
|
||
def applyImpl[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { | ||
import c.universe._ | ||
val wtt = weakTypeTag[T] | ||
val pre = wtt.tpe.asInstanceOf[TypeRef].pre | ||
|
||
// Scala 2.12 & 2.13 macros seem to handle annotations differently | ||
// Scala annotation works in both but Java annotations only works in 2.13 | ||
val saType = typeOf[scala.annotation.StaticAnnotation] | ||
val jaType = typeOf[java.lang.annotation.Annotation] | ||
// Annotation for Scala enumerations are on the outer object | ||
val annotated = if (pre <:< typeOf[scala.Enumeration]) pre else wtt.tpe | ||
val trees = annotated.typeSymbol.annotations.collect { | ||
case t if t.tree.tpe <:< saType && !(t.tree.tpe <:< jaType) => | ||
// FIXME `t.tree` should work but somehow crashes the compiler | ||
val q"new $n(..$args)" = t.tree | ||
q"new $n(..$args)" | ||
} | ||
|
||
// Get Java annotations via reflection | ||
val j = q"classOf[${annotated.typeSymbol.asClass}].getAnnotations.toList" | ||
val annotations = q"_root_.scala.List(..$trees) ++ $j" | ||
|
||
q"_root_.magnolify.shared.AnnotationType[$wtt]($annotations)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
shared/src/main/scala/magnolify/shared/ReflectionUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2021 Spotify AB. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package magnolify.shared | ||
|
||
import scala.reflect.ClassTag | ||
|
||
object ReflectionUtils { | ||
def name[T](implicit ct: ClassTag[T]): String = ct.runtimeClass.getSimpleName | ||
|
||
def namespace[T](implicit ct: ClassTag[T]): String = | ||
ct.runtimeClass.getCanonicalName.replaceFirst(s".${name[T]}$$", "") | ||
} |
45 changes: 45 additions & 0 deletions
45
shared/src/test/scala/magnolify/shared/AnnotationTypeSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2021 Spotify AB. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package magnolify.shared | ||
|
||
import magnolify.test._ | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
class AnnotationTypeSuite extends MagnolifySuite { | ||
import AnnotationTypeSuite._ | ||
test("Enumeration") { | ||
assertEquals(AnnotationType[AccountType.Type].annotations, List(Version("1.0"))) | ||
} | ||
|
||
test("Class") { | ||
assertEquals(AnnotationType[Account].annotations, List(Version("2.0"))) | ||
} | ||
} | ||
|
||
object AnnotationTypeSuite { | ||
case class Version(version: String) extends StaticAnnotation | ||
|
||
@Version("1.0") | ||
object AccountType extends Enumeration { | ||
type Type = Value | ||
val Checking, Saving = Value | ||
} | ||
|
||
@Version("2.0") | ||
case class Account(user: String, accountType: AccountType.Type) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters