本文整理汇总了Scala中scala.annotation.compileTimeOnly类的典型用法代码示例。如果您正苦于以下问题:Scala compileTimeOnly类的具体用法?Scala compileTimeOnly怎么用?Scala compileTimeOnly使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了compileTimeOnly类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CoreMacros
//设置package包名称以及导入依赖的类
package com.outr.arango
import scala.annotation.compileTimeOnly
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
@compileTimeOnly("Enable macro paradise to expand compile-time macros")
object CoreMacros {
def updateIfModifiable[T](c: blackbox.Context)(value: c.Expr[T])(implicit t: c.WeakTypeTag[T]): c.Expr[T] = {
import c.universe._
if (t.tpe <:< typeOf[Modifiable]) {
c.Expr[T](q"$value.copy(modified = System.currentTimeMillis())")
} else {
c.Expr[T](q"$value")
}
}
}
示例2: Fields
//设置package包名称以及导入依赖的类
package com.scalagen.macros
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.meta._
@compileTimeOnly("@fields not expanded")
class Fields extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case q"..$mods class SourceContainer (...$paramss) extends $template" =>
template match {
case template"{ ..$stats } with ..$ctorcalls { $param => ..$body }" =>
val newBody = body :+ q"""def fields = this.length """
val newTemplate = template"{ ..$stats } with ..$ctorcalls { $param => ..$newBody }"
q"..$mods class SourceContainer (...$paramss) extends $newTemplate"
}
case _ => throw new Exception("Thing happened that not work")
}
}
}
示例3: Macros
//设置package包名称以及导入依赖的类
package com.outr.uberterm
import scala.annotation.compileTimeOnly
import scala.reflect.macros.blackbox
@compileTimeOnly("Enable Macros for expansion")
object Macros {
def module[M <: UberTermModule](context: blackbox.Context)
(implicit m: context.WeakTypeTag[M]): context.Expr[M] = {
import context.universe._
val typeString = m.tpe.toString
val (preType, postType) = if (typeString.indexOf('.') != -1) {
val index = typeString.lastIndexOf('.')
typeString.substring(0, index + 1) -> typeString.substring(index + 1)
} else {
"" -> typeString
}
val clientTypeString = s"${preType}Client$postType"
val serverTypeString = s"${preType}Server$postType"
val clientType = try {
Some(context.universe.rootMirror.staticClass(clientTypeString))
} catch {
case _: ScalaReflectionException => None
}
val serverType = try {
Some(context.universe.rootMirror.staticClass(serverTypeString))
} catch {
case exc: ScalaReflectionException => None
}
val communicationType = serverType match {
case Some(t) => t
case None => clientType match {
case Some(t) => t
case None => context.abort(context.enclosingPosition, s"Unable to find implementation trait $clientTypeString or $serverTypeString for $typeString.")
}
}
val application = context.prefix.tree
context.Expr[M](
q"""
val module = new $m()
module.init()
module
""")
}
def help(context: blackbox.Context)(): context.Expr[ModuleHelp] = {
import context.universe._
val test = context.prefix.tree
scribe.info(s"Module: ${test.tpe.members}")
// module.symbol
context.abort(context.enclosingPosition, "WIP")
}
}
示例4: Benchmark
//设置package包名称以及导入依赖的类
package io.flatmap.ml.macros
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.meta._
@compileTimeOnly("@Benchmark annotation not expanded")
class Benchmark extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case q"..$mods def $name[..$tparams](...$argss): $tpeopt = $expr" =>
println("Parameters: " + argss)
q"""
..$mods def $name[..$tparams](...$argss): $tpeopt = {
val start = System.nanoTime()
val result = $expr
val end = System.nanoTime()
println(${name.toString} + " elapsed time: " + (end - start) + "ns")
result
}
"""
case _ => abort("@Benchmark annotation works only on methods")
}
}
}
示例5: make
//设置package包名称以及导入依赖的类
package example
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
import scala.annotation.{StaticAnnotation, compileTimeOnly}
trait Goodbye {
def make(): Unit
}
class GoodbyeTranslator[C <: Context](val context: C) {
import context.universe._
def translate(tree: Tree): Tree = {
q"""
new example.Goodbye {
def make() {
$tree
System.exit(0)
}
}
"""
}
}
object GoodbyeTranslator {
def apply(c: Context): GoodbyeTranslator[c.type] = new GoodbyeTranslator(c)
}
object GoodbyeMacro {
def impl(c: Context)(arg: c.Expr[Unit]): c.Expr[Goodbye] = {
val translator = GoodbyeTranslator(c)
val tree = translator.translate(arg.tree)
c.Expr[Goodbye](tree)
}
}
@compileTimeOnly("only for compile time expansion")
object goodbye {
def apply(arg: Unit): Goodbye = macro GoodbyeMacro.impl
}
示例6: HelloTranslator
//设置package包名称以及导入依赖的类
package example
import scala.reflect.macros.whitebox.Context
import scala.language.experimental.macros
import scala.annotation.{StaticAnnotation, compileTimeOnly}
class HelloTranslator[C <: Context](val context: C) {
import context.universe._
case class ObjectPattern(name: TermName, parents: List[Tree], body: List[Tree])
object ObjectPattern {
def unapply(tree: Tree): Option[ObjectPattern] = tree match {
case q"object $name extends ..$parents { ..$body }" => Some(ObjectPattern(name, parents, body))
case _ => None
}
}
def translate(tree: Tree): Tree = tree match {
case ObjectPattern(pattern) => translate(pattern)
case _ => context.abort(context.enclosingPosition, "Cannot match object pattern")
}
def translate(pattern: ObjectPattern): Tree = {
import pattern._
q"""
object $name extends ..$parents {
def hello: ${typeOf[String]} = "hello"
..$body
}
"""
}
}
object HelloTranslator {
def apply(c: Context): HelloTranslator[c.type] = new HelloTranslator(c)
}
object HelloMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val translator = HelloTranslator(c)
val trees = annottees.map(_.tree).toList
val translated = translator.translate(trees.head) :: trees.tail
c.Expr[Any](q"..$translated")
}
}
@compileTimeOnly("only for compile time expansion")
class hello extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro HelloMacro.impl
}
示例7: as
//设置package包名称以及导入依赖的类
package troy.dsl
import scala.annotation.compileTimeOnly
trait ParsingOps {
type ParseAs[R]
// $COVERAGE-OFF$
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[R](mapper: () => R): ParseAs[R] = ???
// (1 to 22).map(1 to _).map(_.map(i => s"T$i").mkString(", ")).map(tstr => s"def as[$tstr, R](mapper: ($tstr) => R): MacroDSL[M[R]]").foreach(println)
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, R](mapper: (T1) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, R](mapper: (T1, T2) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, R](mapper: (T1, T2, T3) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, R](mapper: (T1, T2, T3, T4) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, R](mapper: (T1, T2, T3, T4, T5) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, R](mapper: (T1, T2, T3, T4, T5, T6) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, R](mapper: (T1, T2, T3, T4, T5, T6, T7) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) => R): ParseAs[R] = ???
// $COVERAGE-ON$
}
示例8: as
//设置package包名称以及导入依赖的类
package troy.meta
import scala.annotation.compileTimeOnly
trait ParsingOps {
type ParseAs[R]
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[R](mapper: () => R): ParseAs[R] = ???
// (1 to 22).map(1 to _).map(_.map(i => s"T$i").mkString(", ")).map(tstr => s"def as[$tstr, R](mapper: ($tstr) => R): MacroDSL[M[R]]").foreach(println)
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, R](mapper: (T1) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, R](mapper: (T1, T2) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, R](mapper: (T1, T2, T3) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, R](mapper: (T1, T2, T3, T4) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, R](mapper: (T1, T2, T3, T4, T5) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, R](mapper: (T1, T2, T3, T4, T5, T6) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, R](mapper: (T1, T2, T3, T4, T5, T6, T7) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) => R): ParseAs[R] = ???
@compileTimeOnly("as can be called only inside troy.dsl.withSchema block")
def as[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](mapper: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) => R): ParseAs[R] = ???
}
示例9: RichStringContext
//设置package包名称以及导入依赖的类
package troy
import com.datastax.driver.core.{ Row, ResultSet, Statement }
import scala.annotation.compileTimeOnly
import scala.concurrent.Future
package object meta {
implicit class RichStringContext(val context: StringContext) extends AnyVal {
@compileTimeOnly("cql Strings can be used only inside troy.dsl.withSchema block")
def cql(args: Any*): MacroDSL.TroyCql = ???
}
implicit class MacroDsl_RichStatement(val statement: Statement) extends ParsingOps {
type ParseAs[R] = Future[Seq[R]]
}
implicit class MacroDsl_RichFutureBoundStatement(val xxx: Future[Statement]) extends ParsingOps {
type ParseAs[R] = Future[Seq[R]]
}
implicit class MacroDsl_RichResultSet(val xxx: ResultSet) extends ParsingOps {
type ParseAs[R] = Seq[R]
}
implicit class MacroDsl_RichFutureOfResultSet(val xxx: Future[ResultSet]) extends ParsingOps {
type ParseAs[R] = Future[Seq[R]]
}
implicit class MacroDsl_RichFutureOfSeqOfRow(val xxx: Future[Seq[Row]]) extends ParsingOps {
type ParseAs[R] = Future[Seq[R]]
}
implicit class MacroDsl_RichFutureOfOptionOfRow(val xxx: Future[Option[Row]]) extends ParsingOps {
type ParseAs[R] = Future[Option[R]]
}
implicit class MacroDsl_RichSeqOfRow(val xxx: Seq[Row]) extends ParsingOps {
type ParseAs[R] = Seq[R]
}
implicit class MacroDsl_RichJavaListOfRow(val xxx: java.util.List[Row]) extends ParsingOps {
type ParseAs[R] = Seq[R]
}
implicit class MacroDsl_RichOptionOfRow(val xxx: Option[Row]) extends ParsingOps {
type ParseAs[R] = Option[R]
}
}
示例10: Macros
//设置package包名称以及导入依赖的类
package com.qvantel.jsonapi
import scala.annotation.compileTimeOnly
import scala.reflect.macros.blackbox.Context
import com.qvantel.jsonapi.macrosupport.{JsonApiReaders, JsonApiWriters}
@compileTimeOnly("Macros can only be used at compile-time")
final class Macros(val c: Context) extends JsonApiWriters with JsonApiReaders {
import c.universe._
private[this] def createFormat(t: c.Type): c.Tree = {
val rootParamName = TermName(c.freshName("root"))
val includedParamName = TermName(c.freshName("included"))
val includePaths = TermName(c.freshName("includePaths"))
val includePath = TermName(c.freshName("includePath"))
q"""new _root_.com.qvantel.jsonapi.JsonApiFormat[$t] with _root_.spray.json.RootJsonFormat[$t] {
import _root_.com.qvantel.jsonapi.PathJsonFormat
override final def write($rootParamName: $t): _root_.spray.json.JsValue = ${primaryDataWriter(t,
rootParamName)}
override final def included($rootParamName: $t): _root_.scala.collection.immutable.Set[_root_.spray.json.JsObject] = ${includedWriter(
t,
rootParamName)}
override final def read(
$rootParamName: _root_.spray.json.JsValue,
$includedParamName: _root_.scala.collection.immutable.Map[(String, String), _root_.spray.json.JsObject],
$includePaths: _root_.scala.collection.immutable.Set[String],
$includePath: String
): $t = ${reader(t, rootParamName, includedParamName, includePaths, includePath)}
}"""
}
def createIncludes[A: c.WeakTypeTag]: c.Tree = {
val t = weakTypeOf[A]
val includeParamName = TermName(c.freshName())
val includeMapParamName = TermName(c.freshName())
q"""
new _root_.com.qvantel.jsonapi.Includes[$t] {
private[this] final lazy val $includeMapParamName: Map[String, _root_.com.qvantel.jsonapi.Includes[_]] = ${includesMap(
t)}
override def includeAllowed($includeParamName: String): Boolean = ${includeAllowed(t,
includeParamName,
includeMapParamName)}
}
"""
}
def jsonApiWriterWithNoNameManglingImpl[A: c.WeakTypeTag]: c.Tree = createWriter(weakTypeOf[A])
def jsonApiFormatWithNoNameManglingImpl[A: c.WeakTypeTag]: c.Tree = createFormat(weakTypeOf[A])
}
示例11: inl
//设置package包名称以及导入依赖的类
package com.qvantel.jsonapi.macrosupport
import scala.annotation.compileTimeOnly
import scala.reflect.macros.blackbox.Context
import shapeless.{CNil, Coproduct}
@compileTimeOnly("Macros can only be used at compile-time")
trait Tools {
val c: Context
import c.universe._
private[this] val coproductType = typeOf[Coproduct]
private[this] val coproductNil = typeOf[CNil]
private[this] def inl(name: TermName): c.Tree = {
val bind = pq"$name"
q"_root_.shapeless.Inl($bind)"
}
private[this] def inr(tree: c.Tree): c.Tree = q"_root_.shapeless.Inr($tree)"
def coproductTypes(t: c.Type): List[c.Type] = {
def go(ta: List[c.Type], acc: List[c.Type]): List[c.Type] =
ta match {
case Nil => acc
case l :: r :: Nil if r =:= coproductNil => acc :+ l
case l :: r :: Nil => go(r.dealias.typeArgs, acc :+ l)
}
if (t <:< coproductType) {
go(t.dealias.typeArgs, Nil)
} else {
c.abort(c.enclosingPosition, s"The type $t must be a shapeless.Coproduct")
}
}
def coproductPattern(n: Int, name: TermName): c.Tree =
(1 until n).foldLeft(inl(name))((tree, _) => inr(tree))
def coproductPatterns(nTypes: Int, name: TermName): Seq[c.Tree] =
(1 to nTypes).map(coproductPattern(_, name))
}
示例12: BaseMacros
//设置package包名称以及导入依赖的类
package org.hyperscala
import com.outr.reactify.Channel
import scala.annotation.compileTimeOnly
import scala.reflect.macros.blackbox
@compileTimeOnly("Enable macro paradise to expand macro annotations")
object BaseMacros {
def appPickler[T](c: blackbox.Context)(implicit t: c.WeakTypeTag[T]): c.Expr[Channel[T]] = {
import c.universe._
val app = q"${c.prefix.tree}"
c.Expr[Channel[T]](
q"""
val pickler = new org.hyperscala.Pickler[$t]($app) {
override def read(json: String): $t = upickle.default.read[$t](json)
override def write(t: $t): String = upickle.default.write[$t](t)
}
pickler.channel
"""
)
}
def screenPickler[T](c: blackbox.Context)(implicit t: c.WeakTypeTag[T]): c.Expr[Channel[T]] = {
import c.universe._
val app = q"${c.prefix.tree}.app"
c.Expr[Channel[T]](
q"""
val pickler = new org.hyperscala.Pickler[$t]($app) {
override def read(json: String): $t = upickle.default.read[$t](json)
override def write(t: $t): String = upickle.default.write[$t](t)
}
pickler.channel
"""
)
}
def communicationPickler[T](c: blackbox.Context)(implicit t: c.WeakTypeTag[T]): c.Expr[Channel[T]] = {
import c.universe._
val communication = q"${c.prefix.tree}"
c.Expr[Channel[T]](
q"""
val pickler = new org.hyperscala.Pickler[$t]($communication) {
override def read(json: String): $t = upickle.default.read[$t](json)
override def write(t: $t): String = upickle.default.write[$t](t)
}
pickler.channel
"""
)
}
}
示例13: helloMacro
//设置package包名称以及导入依赖的类
package codes.bytes.macros_intro.macros
import scala.reflect.macros.whitebox.Context
import scala.language.experimental.macros
import scala.annotation.{compileTimeOnly, StaticAnnotation}
object helloMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val result = {
annottees.map(_.tree).toList match {
case q"object $name extends ..$parents { ..$body }" :: Nil =>
q"""
object $name extends ..$parents {
def hello: ${typeOf[String]} = "hello"
..$body
}
"""
}
}
c.Expr[Any](result)
}
}
@compileTimeOnly("enable macro paradise to expand macro annotations")
class hello extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro helloMacro.impl
}
// vim: set ts=2 sw=2 sts=2 et:
示例14: Bootstrap
//设置package包名称以及导入依赖的类
package ng.macros
import be.doeraene.sjsreflect.Reflect
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.meta._
@compileTimeOnly("@Bootstrap not expanded")
class Bootstrap extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
val q"object $tname extends ..$parents { ..$stats }" = defn
// should evaluate something like:
// app.AppModule.annotations = app.AppModule_().annotations
// for every class that extends NGAnnotations
val propertyApplication =
q"""
be.doeraene.sjsreflect.Reflect
.enumerateClasses
// gets the fully qualified class-name
.map(_.getName)
.withFilter(_ != "ng.macros.NGAnnotation")
.foreach(className => {
val objName = "annots." + className.split('.').last + "_()"
val expr =
s"$$className.annotations = $$objName.annotations; $$className.parameters = $$objName.parameters"
scalajs.js.eval(expr)
})
"""
q"object $tname extends ..$parents { $propertyApplication; ..$stats }"
}
}
示例15: before
//设置package包名称以及导入依赖的类
package nz.daved.elysium.core
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import nz.daved.elysium.manipulate.DefManipulation._
import scala.meta.Defn.Def
import scala.meta._
@compileTimeOnly("@before not expanded")
class before(fun: () => Unit) extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
val q"new $_(${apply: Term.Apply})" = this
defn.asInstanceOf[Def].prependStat(apply)
}
}
@compileTimeOnly("@after not expanded")
class after(fun: () => Unit) extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
val q"new $_(${apply: Term.Apply})" = this
defn.asInstanceOf[Def].appendStat(apply)
}
}