我必须查看宏中的特定声明并单独检查它们。 尝试检查akka流声明时,发生了许多此类错误。
def getType(symbol: Symbol): Type = {
symbol.typeSignature
}
[error] error while loading SmallSortedMap$Entry, class file '/Users/xxx/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: K)
这样,似乎在访问特定符号的typeSygnature时发生错误。
我想忽略它,将其隐藏并成功编译
def getType(symbol: Symbol): Option[Type] = {
Try {
symbol.typeSignature
}.getOrElse(None) // Can not capture
}
但是,“尝试”似乎无法捕获“类已损坏”。 例如,带有此符号的软件包被排除在黑名单之外。结果,每次添加依赖项时,都可能发生复杂的维护。
if (symbol.isNotBroken) {
symbol.typeSignature
}
有没有办法解决这种问题?
我尝试过typeCheck。
implicit class RichVectorSymbol(value: Vector[Symbol]) {
def accessible: Vector[Symbol] = {
value.flatMap { x =>
scala.util.Try {
print(s"typecheck ${x.fullName} ")
c.typecheck(q"${c.parse(x.fullName)}", silent = true)
} match {
case Success(r) if r.nonEmpty =>
println("Success")
Some(r.symbol)
case Failure(e) =>
println("Fail")
c.warning(c.enclosingPosition, e.getMessage)
None
case _ =>
println("Empty")
None
}
}
}
}
因此。
// Success case
typecheck akka.event.jul.Logger Success
typecheck akka.io.dns.CachePolicy Success
typecheck akka.io.dns.DnsSettings Success
// Fail case
typecheck com.fasterxml.jackson.databind.ObjectMapper$2 [error] error while loading ObjectMapper$2, class file '/Users/xxxxx/.ivy2/cache/com.fasterxml.jackson.core/jackson-databind/bundles/jackson-databind-2.9.8.jar(com/fasterxml/jackson/databind/ObjectMapper$2.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: T)
Empty
typecheck akka.protobuf.SmallSortedMap$Entry [error] error while loading SmallSortedMap$Entry, class file '/Users/xxxxx/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: K)
Empty
像这样,我被迫编译错误。 示例在这里。 https://github.com/giiita/scaladia/blob/master/scaladia-macro/src/main/scala/com/phylage/scaladia/internal/AutoDIExtractor.scala
在阅读了scala源代码之后,我在内部抛出了一个清晰的IOException,但是我无法捕获它,因此我报告了该问题,以防万一 https://github.com/scala/bug/issues/11611
答案 0 :(得分:1)
在宏中,您可以尝试
c.typecheck(q"${... some tree ...}", silent = true)
如果树没有类型检查,则返回空树。
事情似乎以akka.protobuf.SmallSortedMap$Entry
中的美元符号表示。如果我们将$
替换为#
或.
,则错误class file is broken
变为class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
,这是因为SmallSortedMap
has包-私有(Java默认)访问权限。
当我将类型作为字符串参数输入宏并使用#
或.
而不是$
时,我设法捕获了错误。
def foo[T]: Unit = macro fooImpl[T]
def fooImpl[T: c.WeakTypeTag](c: blackbox.Context): c.Tree = {
import c.universe._
try {
println(weakTypeOf[T].typeSymbol.typeSignature)
} catch {
case ex: Throwable => println(ex)
}
q"()"
}
def foo1(tpe: String): Unit = macro foo1Impl
def foo1Impl(c: blackbox.Context)(tpe: c.Tree): c.Tree = {
import c.universe._
val q"${tpeStr: String}" = tpe
try {
println(c.typecheck(c.parse(s"val ${c.freshName()}: $tpeStr = ???")))
// println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode))
// println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).symbol.companion.typeSignature)
// println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).tpe.typeSymbol.typeSignature)
// println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).tpe)
} catch {
case ex: Throwable => println(ex)
}
q"()"
}
foo[Int]
// foo[akka.protobuf.SmallSortedMap$Entry]//Error:scalac: error while loading SmallSortedMap$Entry, class file '.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken(class java.util.NoSuchElementException/key not found: K)
// foo[akka.protobuf.SmallSortedMap#Entry]//Error: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
// foo[akka.protobuf.SmallSortedMap.Entry]//Error:class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
// foo1("Int")
// foo1("akka.protobuf.SmallSortedMap$Entry")//Error:scalac: error while loading SmallSortedMap$Entry, class file '.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken (class java.util.NoSuchElementException/key not found: K)
foo1("akka.protobuf.SmallSortedMap#Entry")//Warning:scalac: scala.reflect.macros.TypecheckException: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
foo1("akka.protobuf.SmallSortedMap.Entry")//Warning:scalac: scala.reflect.macros.TypecheckException: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
否则,当您调用foo[... SomeType ...]
时,将在展开宏SomeType
之前对foo
进行类型检查。
https://stackoverflow.com/a/56754290/5249621
http://www.scala-archive.org/Expand-macros-before-typechecking-its-arguments-trees-td4641188.html