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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:47:26+00:00 2026-05-13T09:47:26+00:00

To make debug-time introspection into classes easy, I’d like to make a generic toString

  • 0

To make debug-time introspection into classes easy, I’d like to make a generic toString method in the base class for the objects in question. As it’s not performance critical code, I’d like to use Reflection to print out field name/value pairs (“x=1, y=2” etc).

Is there an easy way to do this? I tried several potential solutions, and ran up against security access issues, etc.

To be clear, the toString() method in the base class should reflectively iterate over public vals in any classes that inherit from it, as well as any traits that are mixed 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-13T09:47:27+00:00Added an answer on May 13, 2026 at 9:47 am
    import util._                 // For Scala 2.8.x NameTransformer
    import scala.tools.nsc.util._ // For Scala 2.7.x NameTransformer
    
    /**
     * Repeatedly run `f` until it returns None, and assemble results in a Stream.
     */
    def unfold[A](a: A, f: A => Option[A]): Stream[A] = {
      Stream.cons(a, f(a).map(unfold(_, f)).getOrElse(Stream.empty))
    }
    
    def get[T](f: java.lang.reflect.Field, a: AnyRef): T = {
      f.setAccessible(true)
      f.get(a).asInstanceOf[T]
    }
    
    /**
     * @return None if t is null, Some(t) otherwise.
     */
    def optNull[T <: AnyRef](t: T): Option[T] = if (t eq null) None else Some(t)
    
    /**
     * @return a Stream starting with the class c and continuing with its superclasses.
     */
    def classAndSuperClasses(c: Class[_]): Stream[Class[_]] = unfold[Class[_]](c, (c) => optNull(c.getSuperclass))
    
    def showReflect(a: AnyRef): String = {
      val fields = classAndSuperClasses(a.getClass).flatMap(_.getDeclaredFields).filter(!_.isSynthetic)
      fields.map((f) => NameTransformer.decode(f.getName) + "=" + get(f, a)).mkString(",")
    }
    
    // TEST
    trait T {
      val t1 = "t1"
    }
    
    class Base(val foo: String, val ?? : Int) {
    }
    
    class Derived(val d: Int) extends Base("foo", 1) with T
    
    assert(showReflect(new Derived(1)) == "t1=t1,d=1,??=1,foo=foo")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To make a JavaScript class with a public method I'd do something like: function
I'd like to make a debug logging function with the same parameters as printf
last time when i debug our application who is written in mvc i make
I make a lot of web applications and from time to time I need
To make click-able divs, I do: <div class=clickable url=http://google.com> blah blah </div> and then
to make debugging easier I coded an authentication hard in my application, like: User
Just had my mind going today. I spent some time in IE debug mode,
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf /usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/cppapplication_1 mkdir -p build/Debug/GNU-MacOSX rm -f
Are this is a bug why they occured every time in debug. i am
Do you have any idea of how can I make this function more time-efficient?

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.