I am trying to use a tasty inspector to convert method params to case classes but I get a classcast exception at runtime.
My code:
import dotty.tools.dotc.ast.Trees.{PackageDef, Template}
import scala.quoted.*
import scala.tasty.inspector.*
class MyInspector extends Inspector:
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
for tasty <- tastys do
import tasty.quotes.reflect.*
tasty.ast match {
case PackageDef(pid, stats) =>
stats.collect { case TypeDef(typeName, Template(constr, parentsOrDerived, self, preBody: List[_])) =>
preBody.collect { case DefDef(name, paramss: List[List[_]] @unchecked, tpt, preRhs) =>
val params = paramss.flatten.map { case ValDef(name, tpt, preRhs) =>
s"$name : ${tpt.show}"
}
println(s"""
|case class ${typeName}_${name}_ccIn(${params.mkString(", ")})
|""".stripMargin)
println("------------------------")
}
}
}
@main def tryit() =
val tastyFiles = List("../example-commands/classpath-1/target/scala-3.2.1/classes/cp1/Cp1Exports.tasty")
TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)
I run this against this class (after I compile it and the compiler creates a .tasty file):
package cp1
import java.time.LocalDate
trait Cp1Exports:
def add(a: Int, b: Int): Int
def subtract(a: Int, b: Int): Int
def friends(p: Person, from: LocalDate): Seq[Person]
case class Person(id: Int, name: String)
But I get this exception:
Exception in thread "main" java.lang.ClassCastException: class dotty.tools.dotc.ast.Trees$Import cannot be cast to class dotty.tools.dotc.ast.Trees$TypeDef (dotty.tools.dotc.ast.Trees$Import and dotty.tools.dotc.ast.Trees$TypeDef are in unnamed module of loader 'app')
at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeDef$.unapply(QuotesImpl.scala:339)
at console.macros.MyInspector$$anon$1.applyOrElse(MyInspector.scala:15)
The line causing the issue is this:
stats.collect { case TypeDef(typeName, Template(constr, parentsOrDerived, self, preBody: List[_])) =>
But it shouldn't because this is a collect. The error is caused because there is an import in Cp1Exports. If I remove the import, it works.
Also any advice to simplify the code would be appreciated.
I am using scala 3.2.1 (incl scala-compiler with that version)
EDIT:
Ok after following the advice from below, I ended up with this code which works (but seems rather complicated):
import dotty.tools.dotc.ast.Trees.*
import scala.quoted.*
import scala.tasty.inspector.*
class MyInspector extends Inspector:
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
for tasty <- tastys do
given dotty.tools.dotc.core.Contexts.Context = scala.quoted.quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
tasty.ast match {
case PackageDef(pid, stats) =>
stats.collect { case TypeDef(typeName, Template(constr, parentsOrDerived, self, preBody: List[_])) =>
preBody.collect { case DefDef(name, paramss: List[List[_]] @unchecked, tpt, preRhs) =>
val params = paramss.flatten.map { case ValDef(name, tpt, preRhs) =>
s"$name : ${tpt.show}"
}
println(s"""
|case class ${typeName}_${name}_ccIn(${params.mkString(", ")})
|""".stripMargin)
println("------------------------")
}
}
}
@main def tryit() =
val tastyFiles = List("../example-commands/classpath-1/target/scala-3.2.1/classes/cp1/Cp1Exports.tasty")
TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)
Thanks