我必须查看宏中的特定声明并单独检查它们。尝试检查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)
以这种方式,似乎在访问特定符号的类型时发生错误。
我想忽略它,隐藏并成功编译
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/main/scala/scala/com/phylage/scaladia/scaladia/internal/autternal/autodiextractor.scala
调试
阅读了Scala源代码后,我在内部抛出了一个清晰的IOException,但我无法抓住它,所以我报告了问题,以防万一https://github.com/scala/bug/issues/11611
在宏中您可以尝试
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
具有软件包 - 私有(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 ...]
时,在扩展Macro foo
之前,请使用SomeType
。
https://stackoverflow.com/a/56754290/5249621
http://www.scala-archive.org/expand-macros-before-typechecking-its-rguments-arguments-trees-td4641188.html