Skip to content

Commit e8db9d5

Browse files
committed
Adapt tasty-reflect for the changes in Signature
Further work to present a nicer API for Signature might be needed.
1 parent 878db33 commit e8db9d5

File tree

6 files changed

+50
-41
lines changed

6 files changed

+50
-41
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,13 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
13981398

13991399
type Signature = core.Signature
14001400

1401-
def Signature_paramSigs(self: Signature): List[String] =
1402-
self.paramsSig.map(_.toString)
1401+
def Signature_paramSigs(self: Signature): List[String | Int] =
1402+
self.paramsSig.map {
1403+
case paramSig: core.Names.TypeName =>
1404+
paramSig.toString
1405+
case paramSig: Int =>
1406+
paramSig
1407+
}
14031408

14041409
def Signature_resultSig(self: Signature): String =
14051410
self.resSig.toString

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,10 @@ trait CompilerInterface {
11211121
// SIGNATURES
11221122
//
11231123

1124-
/** JVM signature of a method */
11251124
type Signature <: AnyRef
11261125

1127-
/** The (JVM) erased signatures of the parameters */
1128-
def Signature_paramSigs(self: Signature): List[String]
1126+
def Signature_paramSigs(self: Signature): List[String | Int]
11291127

1130-
/** The (JVM) erased result type */
11311128
def Signature_resultSig(self: Signature): String
11321129

11331130
//

library/src/scala/tasty/reflect/Core.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ trait Core {
400400
/** Untyped identifier */
401401
type Id = internal.Id
402402

403-
/** JVM signature of a method */
403+
/** Signature of a method */
404404
type Signature = internal.Signature
405405

406406
/** Position in a source file */

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ trait Printers
356356

357357
def visitSignature(sig: Signature): Buffer = {
358358
val Signature(params, res) = sig
359-
this += "Signature(" ++= params += ", " += res += ")"
359+
this += "Signature(" ++= params.map(_.toString) += ", " += res += ")"
360360
}
361361

362362
def visitImportSelector(sel: ImportSelector): Buffer = sel match {

library/src/scala/tasty/reflect/SignatureOps.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ package scala.tasty.reflect
22

33
trait SignatureOps extends Core {
44

5-
/** Erased (JVM) signatures. */
5+
/** The signature of a method */
66
object Signature {
7-
/** Matches the erased (JVM) signature and returns its parameters and result type. */
8-
def unapply(sig: Signature) given (ctx: Context): Option[(List[String], String)] =
7+
/** Matches the method signature and returns its parameters and result type. */
8+
def unapply(sig: Signature) given (ctx: Context): Option[(List[String | Int], String)] =
99
Some((sig.paramSigs, sig.resultSig))
1010
}
1111

1212
implicit class SignatureAPI(sig: Signature) {
1313

14-
/** The (JVM) erased signatures of the parameters */
15-
def paramSigs: List[String]= internal.Signature_paramSigs(sig)
14+
/** The signatures of the method parameters.
15+
*
16+
* Each *type parameter section* is represented by a single Int corresponding
17+
* to the number of type parameters in the section.
18+
* Each *term parameter* is represented by a String corresponding to the fully qualified
19+
* name of the parameter type.
20+
*/
21+
def paramSigs: List[String | Int] = internal.Signature_paramSigs(sig)
1622

17-
/** The (JVM) erased result type */
23+
/** The signature of the result type */
1824
def resultSig: String = internal.Signature_resultSig(sig)
1925

2026
}

tests/run-with-compiler-custom-args/tasty-interpreter/interpreter/jvm/JVMReflection.scala

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,35 @@ class JVMReflection[R <: Reflection & Singleton](val reflect: R) {
8181
}
8282

8383
private def paramsSig(sym: Symbol): List[Class[_]] = {
84-
sym.asDefDef.signature.paramSigs.map { param =>
85-
def javaArraySig(name: String): String = {
86-
if (name.endsWith("[]")) "[" + javaArraySig(name.dropRight(2))
87-
else name match {
88-
case "scala.Boolean" => "Z"
89-
case "scala.Byte" => "B"
90-
case "scala.Short" => "S"
91-
case "scala.Int" => "I"
92-
case "scala.Long" => "J"
93-
case "scala.Float" => "F"
94-
case "scala.Double" => "D"
95-
case "scala.Char" => "C"
96-
case paramName => "L" + paramName + ";"
84+
sym.asDefDef.signature.paramSigs.collect {
85+
case param: String =>
86+
def javaArraySig(name: String): String = {
87+
if (name.endsWith("[]")) "[" + javaArraySig(name.dropRight(2))
88+
else name match {
89+
case "scala.Boolean" => "Z"
90+
case "scala.Byte" => "B"
91+
case "scala.Short" => "S"
92+
case "scala.Int" => "I"
93+
case "scala.Long" => "J"
94+
case "scala.Float" => "F"
95+
case "scala.Double" => "D"
96+
case "scala.Char" => "C"
97+
case paramName => "L" + paramName + ";"
98+
}
9799
}
98-
}
99-
100-
def javaSig(name: String): String =
101-
if (name.endsWith("[]")) javaArraySig(name) else name
102-
103-
if (param == "scala.Boolean") classOf[Boolean]
104-
else if (param == "scala.Byte") classOf[Byte]
105-
else if (param == "scala.Char") classOf[Char]
106-
else if (param == "scala.Short") classOf[Short]
107-
else if (param == "scala.Int") classOf[Int]
108-
else if (param == "scala.Long") classOf[Long]
109-
else if (param == "scala.Float") classOf[Float]
110-
else if (param == "scala.Double") classOf[Double]
111-
else java.lang.Class.forName(javaSig(param), false, classLoader)
100+
101+
def javaSig(name: String): String =
102+
if (name.endsWith("[]")) javaArraySig(name) else name
103+
104+
if (param == "scala.Boolean") classOf[Boolean]
105+
else if (param == "scala.Byte") classOf[Byte]
106+
else if (param == "scala.Char") classOf[Char]
107+
else if (param == "scala.Short") classOf[Short]
108+
else if (param == "scala.Int") classOf[Int]
109+
else if (param == "scala.Long") classOf[Long]
110+
else if (param == "scala.Float") classOf[Float]
111+
else if (param == "scala.Double") classOf[Double]
112+
else java.lang.Class.forName(javaSig(param), false, classLoader)
112113
}
113114
}
114115

0 commit comments

Comments
 (0)