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

TextLayout handling of empty strings

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 26
    • client-libs
    • None
    • 2d
    • behavioral
    • low
    • Hide
      Code which is *expecting* that TextLayout will reject empty strings and throw an Exception seems unlikely.

      Any code which might have been relying on the NullPointerException thrown when a null FontRenderContext is provided to the TextLayout constructor will now receive an IllegalArgumentException instead.

      This should be extremely rare, as no exceptions were documented in the constructor JavaDoc, and the NullPointerException was incidental (i.e. caused by a null dereference further down the stack), not explicit (i.e. explicitly thrown by a parameter validation check).
      Show
      Code which is *expecting* that TextLayout will reject empty strings and throw an Exception seems unlikely. Any code which might have been relying on the NullPointerException thrown when a null FontRenderContext is provided to the TextLayout constructor will now receive an IllegalArgumentException instead. This should be extremely rare, as no exceptions were documented in the constructor JavaDoc, and the NullPointerException was incidental (i.e. caused by a null dereference further down the stack), not explicit (i.e. explicitly thrown by a parameter validation check).
    • Java API
    • SE

      Summary

      java.awt.font.TextLayout constructors now allow empty strings, and have updated exception handling specification and implementation.

      Problem

      Clients have to check whether a string is empty or not before passing it to the TextLayout constructors, because the constructors explicitly check for empty strings and throw an IllegalArgumentException if the string is empty. Furthermore, this limitation is not specified. This surprises developers, leads to bugs in applications when developers are not aware of this limitation, and creates unnecessary complexity in applications.

      Additionally, the scenarios which might cause exceptions to be thrown from the TextLayout constructors are not all specified.

      Finally, there is an inconsistency in the parameter validation of the TextLayout constructors: no constructor parameters are allowed to be null, and if they are null then an IllegalArgumentException is thrown -- except if a null FontRenderContext is provided, in which case a NullPointerException is eventually thrown (not explicitly, but because of a null dereference).

      Solution

      1. Allow TextLayout to be constructed with empty strings.
      2. Document the existing exceptions thrown by the TextLayout constructors.
      3. Add explicit checks for null FontRenderContext instances, and throw IllegalArgumentException in such cases (mirroring existing behavior for all other constructor parameters).

      Specification

      public class java.awt.font.TextLayout

      +     * @throws IllegalArgumentException if any of the parameters are null.
            */
           public TextLayout(String string, Font font, FontRenderContext frc)
      +     * @throws IllegalArgumentException if any of the parameters are null.
            */
           public TextLayout(String string, Map<? extends Attribute,?> attributes,
                             FontRenderContext frc)
      +     * @throws IllegalArgumentException if any of the parameters are null.
            */
           public TextLayout(AttributedCharacterIterator text, FontRenderContext frc)

      ```

            dgredler Daniel Gredler
            blichtensunw Brian Lichtenwalter (Inactive)
            Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: