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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:06:09+00:00 2026-06-08T01:06:09+00:00

I am playing with reflection to achieve a deep analysis on a trait. One

  • 0

I am playing with reflection to achieve a deep analysis on a trait. One of the things I would like to get is the initial value set to a member field. For example, in the trait:

trait A {
  val x: Int = 3
  val y: String = "y"
}

it would be nice to know 3 and “y”. I have not found anything related to this task in the API, and due to the following output (generated by scalac -Xprint):

abstract trait A extends Object {
  <accessor> def com$hablapps$A$_setter_$x_=(x$1: Int): Unit;
  <accessor> def com$hablapps$A$_setter_$y_=(x$1: String): Unit;
  <stable> <accessor> def x(): Int;
  <stable> <accessor> def y(): String
};
abstract trait A$class extends  {
  def /*A$class*/$init$($this: com.hablapps.A): Unit = {
    $this.com$hablapps$A$_setter_$x_=(3);
    $this.com$hablapps$A$_setter_$y_=("y");
    ()
  }
}

I am afraid it is going to be quite hard to access them, since they are kept in the $init$ method’s body. Is there any (easy) way to get those values with reflection?

  • 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-06-08T01:06:10+00:00Added an answer on June 8, 2026 at 1:06 am

    You have to disassemble the bytecode:

    trait A { val x: Int = 3 }
    
    public abstract class A$class extends java.lang.Object{
    public static void $init$(A);
      Code:
       0:    aload_0
       1:    iconst_3
       2:    invokeinterface #12,  2; //InterfaceMethod A.A$_setter_$x_$eq:(I)V
       7:    return
    

    See line 1–the only place the value exists is in the bytecode for the init method!

    You cannot get to this any other way, since if you have

    trait A { val x: Int = 3 }
    trait B extends A { override val x = 7 }
    class C extends B {}
    

    you find that C extends A$_setter_$x_$eq to do nothing at all–making the A$class.$init$ call a no-op and rendering the value unretrievable.

    Proof:

    public class C extends java.lang.Object implements B,scala.ScalaObject{
    public void A$_setter_$x_$eq(int);
      Code:
       0:    return
    
    public void B$_setter_$x_$eq(int);
      Code:
       0:    aload_0
       1:    iload_1
       2:    putfield #11; //Field x:I
       5:    return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing round with Timers. Context: a winforms with two labels. I would like to
I'm currently playing with reflection and I have problem with my short code: public
Playing with jquery for the first time, and I'm trying to get a simple
I'm playing around with the C# reflection API. I can easily load Type information
I'm playing around with WebMatrix and I'd just like to explore some of the
I was playing around with Java reflection, and I wanted to create a method
Playing with new RTTI module, I couldn't find a way to set an event
I've been playing around with reflection in Java... and I'm a little bit baffled.
I am playing with Java's reflection API, and I'm writing methods that inspect a
I am playing with class generation ( one class for a table - inheritance

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.