title: Kotlin crashes in PasswordTransformationMethod while Java works fine
body: <p>The code below crashes in Kotlin/Android with the stack trace provided on the bottom. It was converted from Java/Android, which didn't have such a problem. Original Java code is provided as well. The crash happens when I try to add a character to a password field. Editing existing characters works well.</p>
  
  <p>I've two questions about it:</p>
  
  <ol>
  <li>What was Kotlin's motivation to replace java.lang.CharSequence with a Kotlin's CharSequence? These two are quite different, and I suspect that it causes the crash.</li>
  <li>Is there any way around to make it working in Kotlin?</li>
  </ol>
  
  <p><strong>Kotlin code that crashes Android's Paint</strong></p>
  
  <pre><code>        mPwd!!.transformationMethod = object : PasswordTransformationMethod() {
          override fun getTransformation(source: CharSequence, view: View): CharSequence {
              return PasswordCharSequence(source)
          }
          internal inner class PasswordCharSequence(private val source: CharSequence)// Store char sequence
              : CharSequence {
  
              val mSource = source
              public override val length = mSource.length
  
              init {
                  App.d(TAG, "SOURCE " + mSource + " " + length)
              }
              override fun get(index: Int): Char {
                  App.d(TAG, "SOURCE IND " + index + " " + mSource.length)
                  return if (mChkUnmask!!.isChecked) mSource.get(index) else '*'
              }
              override fun subSequence(start: Int, end: Int): CharSequence {
                  App.d(TAG, "SOURCE SUB " + start + " " + end)
                  return mSource.subSequence(start, end) // Return default
              }
          }
      }
  </code></pre>
  
  <p><strong>Original Java code that works perfectly well</strong></p>
  
  <pre><code>        mPwd.setTransformationMethod(new PasswordTransformationMethod() {
  
          @Override
          public CharSequence getTransformation(CharSequence source, View view) {
              return new PasswordCharSequence(source);
          }
  
          class PasswordCharSequence implements CharSequence {
              private CharSequence mSource;
              public PasswordCharSequence(CharSequence source) {
                  mSource = source; // Store char sequence
              }
              public char charAt(int index) {
                  return mChkUnmask.isChecked()?mSource.charAt(index):'*';
              }
              public int length() {
                  return mSource.length(); // Return default
              }
              public CharSequence subSequence(int start, int end) {
                  return mSource.subSequence(start, end); // Return default
              }
          }
      });
  </code></pre>
  
  <p><strong>Stack Trace from Logcat</strong></p>
  
  <pre><code>06-03 00:35:08.143 16694 16694 E AndroidRuntime: java.lang.IndexOutOfBoundsException
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.graphics.Paint.getRunAdvance(Paint.java:2986)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.TextLine.handleText(TextLine.java:719)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.TextLine.handleRun(TextLine.java:873)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.TextLine.measureRun(TextLine.java:387)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.TextLine.measure(TextLine.java:277)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.TextLine.metrics(TextLine.java:251)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.Layout.getLineExtent(Layout.java:1072)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.text.Layout.getLineWidth(Layout.java:1038)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.TextView.desired(TextView.java:8142)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.TextView.onMeasure(TextView.java:8208)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.View.measure(View.java:21051)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.View.measure(View.java:21051)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.View.measure(View.java:21051)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.View.measure(View.java:21051)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
  06-03 00:35:08.143 16694 16694 E AndroidRuntime:        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
  </code></pre>
  
  <p><strong>Program output from Log.d(...)</strong></p>
  
  <p>10 is a CharSequence length here. First digit - current index. Crash happens when I was trying to add a char with index 9 from a keyboard. It was never added and didn't show up in the output below </p>
  
  <pre><code>06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 810
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 710
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 610
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 510
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 410
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 310
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 210
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 110
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 010
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 010
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 110
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 210
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 310
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 410
  06-03 00:35:08.121 16694 16694 D GAC-STORE: SOURCE IND 510
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 610
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 710
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 810
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 010
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 110
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 210
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 310
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 410
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 510
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 610
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 710
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 810
  06-03 00:35:08.122 16694 16694 D GAC-STORE: SOURCE IND 810
  </code></pre>
  
  <p><strong>Layout for mPwd</strong></p>
  
  <pre><code>&lt;EditText
      android:id="@+id/txtDlgPwd"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/chkStorePwd"
      android:hint ="Ecnryption Password"
      android:maxLines="1"
      android:singleLine="true"
      android:inputType="textPassword|textNoSuggestions"
      android:imeOptions="actionDone"
      android:layout_marginLeft="10dp"
      android:layout_marginTop="20dp"
      android:textSize="15sp"/&gt;
  </code></pre>