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

I write my debug information to a file using a separate thread. During startup,
Question Is it possible to make Log4J display the full path of the file
I have recently started a new C++ project and I intend to make it
#include <QtCore/QCoreApplication> #include <QtGui/QLabel> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QLabel
I have tried to implement a program using sockets in Ubuntu 10.04. Here is
Background I'm trying to get obtain a unique identifier out of a computer and
I am trying to create a DLL to use it with Java JNI. I
I have a FireWorks-generated HTML page and I added some jQuery to it for
I am in the process of developing an application which uses a legacy USB
I have a bizarre need here, and I am unsure of it's feasibility. I

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.