简体   繁体   中英

Why does only one field behave like this?

My case is very very simple... :

I have this User class:

package com.myapp.domain.model.user

class User private[user](private val _id: String,
                         private var _password: String = null,
                         private var _androidToken: String = null,
                         private var _iosDeviceToken: String = null) extends NodeIdentity with DomainEntityId[String] {

          private[model] def this() {
              this(null, null, null, null) //ugly but needed for Spring-Data
          }            
          //.........
}

and its corresponding companion:

object User {

  def create(.....): ValidationNel[IllegalUserFailure, User] = {
    new User("123", "456", "789", "111")
  }

}

The weird thing is while debugging in IntelliJ, I found that:

_id = "123"         
_password = "456"            
_androidToken = "789"              
com$myapp$domain$model$user$User$_iosDeviceToken = "111"     

What might be the reason explaining why only _iosDeviceToken appears like this? Like..unreachable directly.
Actually, I merely simplified the example, but really it is almost as simple as this.

Don't figure it out..

It means you're accessing the member in a way that requires wider access than private at the platform level, so for Java, it is renamed to avoid name collisions. I see you are using private[user] and so on.

Probably there is a duplicate answer somewhere.

Here's an example where I had to be aware of the name mangling, with a little explanation.

Here's another example of how it can happen unexpectedly, because of the particular encoding:

package mangled.user

class User private[user](private val _id: String,
                         private var _aToken: String = null,
                         private var _bToken: String = null) {
  private[mangled] def this() = this(null, null, null)

  def f = (1 to 3) map (_ => _bToken)
}

object User {
  def create = new User("x","y","z")
}

The anonfun gets access to the member:

scala> :javap -pv -fun mangled.user.User
[snip]
  public final java.lang.String apply(int);
    flags: ACC_PUBLIC, ACC_FINAL
    Code:
      stack=1, locals=2, args_size=2
         0: aload_0       
         1: getfield      #19                 // Field $outer:Lmangled/user/User;
         4: invokevirtual #23                 // Method mangled/user/User.mangled$user$User$$_bToken:()Ljava/lang/String;
         7: areturn   

And the members themselves:

scala> :javap -p mangled.user.User
Compiled from "mangled.scala"
public class mangled.user.User {
  private final java.lang.String _id;
  private java.lang.String _aToken;
  private java.lang.String mangled$user$User$$_bToken;
  public static java.lang.String $lessinit$greater$default$3();
  public static java.lang.String $lessinit$greater$default$2();
  public static mangled.user.User create();
  private java.lang.String _id();
  private java.lang.String _aToken();
  private void _aToken_$eq(java.lang.String);
  public java.lang.String mangled$user$User$$_bToken();
  private void mangled$user$User$$_bToken_$eq(java.lang.String);
  public scala.collection.immutable.IndexedSeq<java.lang.String> f();
  public mangled.user.User(java.lang.String, java.lang.String, java.lang.String);
  public mangled.user.User();
}

The accessor to _bToken has been made public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM