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

generics: recursive instantiation of generic class gets stack overflow

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      javac -source 1.5 Recurse.java
      java Recurse 10000
      This may be a case of user shooting himself inthe foot, but compiler allows unlimited instances of class. Maybe compiler can issues a warning.

      class Recurse<T>
      {
          static int count = 0;

          public Recurse() {
            count++;
            System.out.print(count + " ");
            System.out.println(this.toString());
          }
        public Object nest(int n)
        {
          if (n == 0){
                return this;
          }
          else {
            return new Recurse<Recurse<T>>().nest(n - 1);
          }

        }
        public static void main(String[] args)
        {
         int c = 250;
         if (args.length > 0)
          c = new Integer(args[0]).intValue();

           Recurse<String> cs = new Recurse<String>();
           cs.nest(c);
        }
      }

      /*

      ...
      8566 Recurse@145f572
      8567 Recurse@1f530d8
      8568 Recurse@59e205
      8569 Exception in thread "main" java.lang.StackOverflowError
              at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:95)
              at sun.nio.cs.SingleByteEncoder.encodeLoop(SingleByteEncoder.java:134)
              at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:536)
              at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(StreamEncoder.java:384)
              at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:136)
              at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
              at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
              at java.io.PrintStream.write(PrintStream.java:304)
              at java.io.PrintStream.print(PrintStream.java:448)
              at java.io.PrintStream.println(PrintStream.java:585)
              at Recurse.<init>(Recurse.java:8)

        Attachments

          Activity

            People

            • Assignee:
              gafter Neal Gafter
              Reporter:
              ssides Steve Sides
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: