5

I need to get a method's parameter name in one of my scala file. I know, by using -parameter compiler option, I can make this work in Java. However, I am not able to do this in scala,as I could not find -parameter option in scalac.

How can I achieve this ? I saw this answer in SO, but it is an old answer. Is it not possible in scala(2.11) as this option has come only in java8? Is there any hack for it ?

EDIT: (adding sample scala code)

class ReflectionTest {
  def method(name: String, id: Long, desc: String) = {
    println("Inside the method");
  }
}

I am trying to read the method parameters of ReflectionTest class's method()

object Test extends App {
  val methods= Class.forName("com.reflection.ReflectionTest").getMethods
  methods filter(_.getName == "method") map { method =>
    val param = method.getParameters
    param.map {p =>
      println("Method : "+method.getName+" , Parameter : "+p.getName)
    }
  }
}

Scala version : 2.11.2

JDK Version : 1.8

SBT Version : 0.13.1

4
  • Are you using SBT? If so, have you tried setting javacOptions? Commented Feb 24, 2015 at 19:27
  • @andersschuller That is for compiling java sources right ? I have scala file, need to get the method parameters. Commented Feb 25, 2015 at 5:07
  • 1
    Sample stackoverflow.com/a/18580363/1296806 except use paramLists for deprecated paramss. Commented Mar 4, 2015 at 15:14
  • 3
    Scalac does this since 2.12.0-M4; see issues.scala-lang.org/browse/SI-9437 Commented Aug 31, 2016 at 17:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.