From 38bc97d74a4d25588af2b295e9ffc642c88fa677 Mon Sep 17 00:00:00 2001 From: Yuri Nesterenko Date: Fri, 30 Jul 2021 14:53:48 +0300 Subject: [PATCH] Backport 97e0e9e73d12c2b8e58ca2540d5153c10984731d --- .../classes/javax/swing/JPasswordField.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/JPasswordField.java b/src/java.desktop/share/classes/javax/swing/JPasswordField.java index 6f37beef4dc..76daa4cfa5f 100644 --- a/src/java.desktop/share/classes/javax/swing/JPasswordField.java +++ b/src/java.desktop/share/classes/javax/swing/JPasswordField.java @@ -489,20 +489,19 @@ private String getEchoString(String str) { * @since 1.6 */ public String getAtIndex(int part, int index) { - String str = null; if (part == AccessibleText.CHARACTER) { - str = super.getAtIndex(part, index); + return getEchoString(super.getAtIndex(part, index)); } else { // Treat the text displayed in the JPasswordField // as one word and sentence. - char[] password = getPassword(); - if (password == null || - index < 0 || index >= password.length) { + int length = getDocument().getLength(); + if (index < 0 || index >= length) { return null; } - str = new String(password); + char[] password = new char[length]; + Arrays.fill(password, getEchoChar()); + return new String(password); } - return getEchoString(str); } /** @@ -523,8 +522,7 @@ public String getAtIndex(int part, int index) { */ public String getAfterIndex(int part, int index) { if (part == AccessibleText.CHARACTER) { - String str = super.getAfterIndex(part, index); - return getEchoString(str); + return getEchoString(super.getAfterIndex(part, index)); } else { // There is no word or sentence after the text // displayed in the JPasswordField. @@ -550,8 +548,7 @@ public String getAfterIndex(int part, int index) { */ public String getBeforeIndex(int part, int index) { if (part == AccessibleText.CHARACTER) { - String str = super.getBeforeIndex(part, index); - return getEchoString(str); + return getEchoString(super.getBeforeIndex(part, index)); } else { // There is no word or sentence before the text // displayed in the JPasswordField. @@ -606,14 +603,14 @@ public AccessibleTextSequence getTextSequenceAt(int part, int index) { } else { // Treat the text displayed in the JPasswordField // as one word, sentence, line and attribute run - char[] password = getPassword(); - if (password == null || - index < 0 || index >= password.length) { + int length = getDocument().getLength(); + if (index < 0 || index >= length) { return null; } + char[] password = new char[length]; + Arrays.fill(password, getEchoChar()); String text = new String(password); - return new AccessibleTextSequence(0, password.length - 1, - getEchoString(text)); + return new AccessibleTextSequence(0, password.length - 1, text); } }