Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8296878

Document Filter attached to JPasswordField and setText("") is not cleared instead inserted characters replaced with unicode null characters

XMLWordPrintable

    • b01
    • b26
    • generic
    • generic

        A DESCRIPTION OF THE PROBLEM :
        To clear a JPasswordField you're supposed to do setText(""). In versions before 11.0.11 this would work if that field had a document filter attached to it, but since 11.0.11 this does not work anymore, and instead the currently inserted characters will be replaced with unicode null characters instead.
        Visually and functionally this means the passwordfield is not empty.

        Tested versions:
        11.0.11+9 (does no work)
        11.0.10+9 (does work)

        REGRESSION : Last worked in version 11.0.10

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create a JPasswordField
        2. Get the document as PlainDocument from the field
        3. Set a documentFilter
        4. Input text in the JPasswordField (either through the UI or with setTexT(String)
        5. Clear the text with setText("") (ie. with a button)

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The field is empty
        ACTUAL -
        The originally set characters get replaced with unicode null characters

        ---------- BEGIN SOURCE ----------
        public class PasswordTest {

            public static void main(String... args) {

                JPasswordField test = new JPasswordField();
                PlainDocument document = (PlainDocument) test.getDocument();
                document.setDocumentFilter(new DocumentFilter() {
                    @Override
                    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                        String string = fb.getDocument().getText(0, fb.getDocument().getLength()) + text;

                        if (string.length() <= 6 && string.matches("[0-9]+")) {
                            super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
                        }
                    }
                });
                test.setText("123456");
                test.setText("");
                System.out.println(Arrays.equals(test.getPassword(), new char[0]));
            }

        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Remove the document filter.

        FREQUENCY : always


              serb Sergey Bylokhov
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Created:
                Updated:
                Resolved: