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 498021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:50:15+00:00 2026-05-13T05:50:15+00:00

When I research a new library, I sometimes find it hard to locate the

  • 0

When I research a new library, I sometimes find it hard to locate the implementation of a method.

In Java, Metho#getDeclaringClass provides the class that declared a given method. So by iterating over Class#getMethods, I can find for each method, the class that declared it.

In Scala, traits are converted to Java interfaces and a class that extends a trait will implement the methods of the trait by forwarding them to a companion class defining these methods statically. This means, that Method#getDeclaringClass will return the class, not the trait:

scala> trait A { def foo = {println("hi")}}
defined trait A

scala> class B extends A
defined class B

scala> classOf[B].getMethods.find(_.getName() == "foo").get.getDeclaringClass
res3: java.lang.Class[_] = class B

What is the best way to work around this? Meaning, given a class, how can I get a List[(Method, Class)] where each tuple is a method and the trait/class it was declared in?

  • 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-13T05:50:15+00:00Added an answer on May 13, 2026 at 5:50 am

    In Scala 2.8 you can use the ScalaSigParser to parse the scala specific byte code information.

    This will be more stable than the byte code serialization format of scala traits, classes and methods.

    import tools.scalap.scalax.rules.scalasig._
    import scala.runtime._
    
    val scalaSig = ScalaSigParser.parse(classOf[RichDouble]).get
    val richDoubleSymbol = scalaSig.topLevelClasses.head
    val methods = richDoubleSymbol.children filter ( _ match {
        case m : MethodSymbol => true
        case _ => false
    })
    
    methods foreach println
    richDoubleSymbol.isTrait
    ScalaSigParser.parse(classOf[Ordered[Any]]).get.topLevelClasses.head.isTrait
    

    Prints:

    scala> methods foreach println
    MethodSymbol(x, owner=0, flags=20080004, info=23 ,None)
    MethodSymbol(<init>, owner=0, flags=200, info=33 ,None)
    [...]
    MethodSymbol(isPosInfinity, owner=0, flags=200, info=117 ,None)
    MethodSymbol(isNegInfinity, owner=0, flags=200, info=117 ,None)
    
    scala> richDoubleSymbol.isTrait
    res1: Boolean = false
    
    scala> ScalaSigParser.parse(classOf[Ordered[Any]]).get.topLevelClasses.head.isTrait
    res2: Boolean = true
    

    I suppose following this road you can build a reflection API for Scala.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface: I am 100% new to the Flash and ActionScript world, doing research for
I did some research and found that the only way to vertically center a
I've done some research, but I didn't find an answer to my question. I
I have done some research and it really seems that implementing a transaction system
I am trying to decode a colored QR code in Java with ZXing library.
I have this function that creates a unique number for hard-disk and CPU combination.
I'm building my own JSP Tag Library which need to support some features that
I was going through this software library for semantic specs in java. This particular
I'm working with an open source charting library that creates markers on a graph.
Is there any PHP PDF library that can replace placeholder variables in an existing

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.