Skip to content

Instantly share code, notes, and snippets.

@DmytroMitin
Last active Aug 24, 2021
Embed
What would you like to do?
import Macros.moduleClassOf
object App {
object Foo
@MyAnnotation(Array(moduleClassOf[Foo.type]))
class SomeClass
}
lazy val commonSettings = Seq(
scalaVersion := "2.12.4"
)
lazy val core = project
.dependsOn(macros)
.settings(
commonSettings,
scalacOptions ++= Seq(
"-Ymacro-debug-lite"
)
)
lazy val macros = project
.settings(
commonSettings,
libraryDependencies ++= Seq(
scalaOrganization.value % "scala-reflect" % scalaVersion.value,
)
)
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
object Macros {
def moduleClassOf[T <: Singleton]: Class[T] = macro impl[T]
def impl[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe._
Literal(Constant(weakTypeOf[T]))
}
// def impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[Class[T]] = {
// import c.universe._
// c.Expr[Class[T]](Literal(Constant(weakTypeOf[T])))
// }
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
Class<?>[] value();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment