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

java.awt.FontMetrics class has mutual recursive methods.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • client-libs
    • sparc
    • solaris_2.5



      Name: saC57035 Date: 02/13/97


      This bug was found by Dmitri A.Silaev of St.Petersburg Java SQE team.

      The java.awt.FontMetrics class has mutual recursive methods which
      cause any simple ancestor of this class to hang.

      Though documentation notes that "the implementations of these
      methods are inefficient, they are usually overridden with more
      efficient toolkit specific implementations", but even inefficient
      code should work properly.

      The methods FontMetrics.getWidth() and FontMetrics.getWidths() are
      mutual recursive. The same applies to methods
      FontMetrics.stringWidth(java.lang.String) and FontMetrics.charsWidth(char).

      Here is the example demonstrating the bug:

      ---- Test.java ----------------------
      import java.awt.*;

      class ExFontMetrics extends FontMetrics{
          ExFontMetrics(Font f){
      super(f);
          }
      }

      public class Test{
          public static void main(String[] args){
      Font f = new Font("TimesRoman",0,12);
      ExFontMetrics fm = new ExFontMetrics(f);
      try {
      int[] l = fm.getWidths();
      } catch (Throwable e) {
      System.out.println("Trowable " + e);
      }
      try {
      int l = fm.stringWidth("DAS");
      } catch (Throwable e) {
      System.out.println("Throwable " + e);
      }
          }
      }


      -- The output ----------------
      Throwable java.lang.StackOverflowError
      Throwable java.lang.StackOverflowError
      ------------------------

      Here is the fragment of source code:

          public int[] getWidths() {
      int widths[] = new int[256];
      for (char ch = 0 ; ch < 256 ; ch++) {
      widths[ch] = charWidth(ch); !!!
      }
      return widths;
          }
          public int charWidth(char ch) {
      if (ch < 256) {
      return getWidths()[ch]; !!!
      }
      char data[] = {ch};
      return charsWidth(data, 0, 1);
          }

          public int stringWidth(String str) {
      int len = str.length();
      char data[] = new char[len];
      str.getChars(0, len, data, 0);
      return charsWidth(data, 0, len); !!!
          }
          public int charsWidth(char data[], int off, int len) {
      return stringWidth(new String(data, off, len)); !!!
          }


      ======================================================================

            flar Jim Graham
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: