Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:01:17+00:00 2026-05-26T02:01:17+00:00

We can use reflection to get method names as follows: object Foo { def

  • 0

We can use reflection to get method names as follows:

object Foo { def bar(name:String, age:Int) = {} } 
val foo = Foo.getClass
val methods = foo.getMethods.filter(_.getName.startsWith("b"))
methods.foreach(m => println(m.getName))

I now need to get the parameter types and names.

  • Are the parameter names stored in the byte-code? If answer is yes, how to access them?
  • If answer above is no, can we store the names somehow using annotations?
  • Can someone given an example to read the types, and how to use them. I am interested only in functions having String and/or Array[String] type parameters.

[EDIT:] Java version of the solution also ok.

[EDIT:] Annotations seems to be one way to do it. However, Scala annotation support is not that good. Related SO question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:01:17+00:00Added an answer on May 26, 2026 at 2:01 am

    If debugging info is present in the classes, it can be done as follows.

    I am basically using Adam Paynter’s answer and copy-pasting the code from here after slight edit to get it to work in Scala.

    package test
    import java.io.InputStream
    import java.util.ArrayList
    import scala.collection.JavaConversions._
    import org.objectweb.asm.ClassReader
    import org.objectweb.asm.Type
    import org.objectweb.asm.tree.ClassNode
    import org.objectweb.asm.tree.LocalVariableNode
    import org.objectweb.asm.tree.MethodNode
    
    object Util {
      case class Param(paraName:String, paraType:Type)
      case class ScalaMethod(name:String, returnType:Type, params:List[Param])
    
      def main(args:Array[String]):Unit = {
        getMethods(scala.util.Random.getClass).foreach(printMethod _ )
        def printMethod(m:ScalaMethod) = {
          println (m.name+" => "+m.returnType.getClassName)
          m.params.foreach(p =>
            println (" "+ p.paraName+":"+p.paraType.getClassName))
        }
      }
    
      /**
       * extracts the names, parameter names and parameter types of all methods of c
       */
      def getMethods(c:Class[_]):List[ScalaMethod] = {
        val cl:ClassLoader = c.getClassLoader();
        val t:Type = Type.getType(c);
        val url:String = t.getInternalName() + ".class";
        val is:InputStream = cl.getResourceAsStream(url);
        if (is == null)
          throw new IllegalArgumentException("""The class loader cannot
                                             find the bytecode that defined the
                                             class (URL: " + url + ")""");
        val cn = new ClassNode();
        val cr = new ClassReader(is);
        cr.accept(cn, 0);
        is.close();
        val methods = cn.methods.asInstanceOf[java.util.List[MethodNode]];
        var mList:List[ScalaMethod] = Nil
        if (methods.size > 0) for (i <- 1 to methods.size) {
          val m:MethodNode = methods.get(i-1)
          val argTypes:Array[Type] = Type.getArgumentTypes(m.desc);
          val paraNames = new java.util.ArrayList[String](argTypes.length)
          val vars = m.localVariables.asInstanceOf[java.util.List[LocalVariableNode]];
          var pList:List[Param] = Nil
          if (argTypes.length > 0) for (i <- 0 to argTypes.length) {
              // The first local variable actually represents the "this" object
              paraNames.add(vars.get(i).name);
              pList = Param(paraNames.get(i-1), argTypes(i-1)) :: pList
          }
          mList = ScalaMethod(m.name, Type.getReturnType(m.desc), pList) :: mList
        }
        mList
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can use reflection in Java to get the name of
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
I'm using reflection to get some member names as strings. I'm using a method
How can I get the name of the object that was passed byref into
How can I use reflection to create a generic List with a custom class
You can use a standard dot notation or a method call in Objective-C to
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
Using Java Reflection, is it possible to get the name of a local variable?
I want to use PHP's reflection features to retrieve a list of parameter names
I know you can use a .net reflector to view code created with .net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.