Save method parameter names in scala macro

I have an interface:

trait MyInterface {
  def doSomething(usefulName : Int) : Unit
}

I have a macro that iterates through interface methods and does everything with the names and parameters of a method. I am accessing method names by doing something like this:

val tpe = typeOf[MyInterface]

// Get lists of parameter names for each method
val listOfParamLists = tpe.decls
  .filter(_.isMethod)
  .map(_.asMethod.paramLists.head.map(sym => sym.asTerm.name))

If I print out the names for the parameters doSomething, usefulNamebecome x$1. Why is this happening, and is there a way to keep the original parameter names?

I am using scala version 2.11.8, macros paradise version 2.1.0 and the blackbox context.

The interface is actually the source of java in a separate sbt project that I control. I tried to compile with:

javacOptions in (Compile, compile) ++= Seq("-target", "1.8", "-source", "1.8", "-parameters")

The parameter flag should keep the names, but I still get the same result as before.

+4
1

, Scala . , Java 8 Scala 2.11 , , .

, Scala (duh!). , Java, .

, , -parameters Java 8, Scala 2.11. , Scala 2.11, , ...

  • MyInterface.java, javac -parameters MyInterface.java

    public interface MyInterface {
      public int doSomething(int bar);
    }
    
  • MyTrait.scala, scalac MyTrait.scala

    class MyTrait {
      def doSomething(bar: Int): Int
    }
    

MethodParameterSpy, , Java 8 -parameter. Java, ( )

public abstract int MyInterface.doSomething(int)

: bar

Scala

public abstract int MyTrait.doSomething(int)

: arg0

, Scala . , Scala Java 8 - . , Java. x$1, x$2,... , , Java 8 arg0, arg1,... Scala, ( -parameters, MyInterface.java.)

( 2.11), , Java, - Java Scala. -

$ javac -parameters MyInterface.java
$ jar -cf MyInterface.jar MyInterface.class
$ scala -cp MyInterface.jar
scala> :pa
// Entering paste mode (ctrl-D to finish)

import java.lang.reflect._

Class.forName("MyInterface")
  .getDeclaredMethods
  .map(_.getParameters.map(_.getName))

// Exiting paste mode, now interpreting.

res: Array[Array[String]] = Array(Array(bar))

, , -parameter ( arg0).

, , , , Java Scala, .isJava (: typeOf[MyInterface].decls.filter(_.isMethod).head.isJava), , .

Future

, Scala 2.12. , , 2.12 Java, -parameter, Java- Scala.

, ?

+4

Source: https://habr.com/ru/post/1648905/


All Articles