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

varargs versus anonymous constructors crashes javac

XMLWordPrintable

    • b40
    • generic
    • solaris_8

      The problem seems to be use of a varargs constructor while defining
      an anonymous class. Work around is for caller to manually box the
      (varargs) into an array, bypassing convenience of varargs support.

      ==========$ cat -n Quick.java
           1 class Quick
           2 {
           3 Quick() {
           4 Fox fox1 = new Fox(1) {}; // compiles
           5 Fox fox2 = new Fox(new String[] { "hello" }) {}; // compiles
           6
           7 // NullPointerException not thrown when following 4 lines are
           8 // commented out.
           9
          10 Fox fox3 = new Fox(null) {}; // compiles, but ambiguous?
          11 Fox fox4 = new Fox() {}; // javac NullPointerException
          12 Fox fox5 = new Fox("hello") {}; // javac NullPointerException
          13 Fox fox6 = new Fox("hello", "bye") {}; // javac NullPointerException
          14 }
          15 }
          16
          17 class Fox
          18 {
          19 Fox(int a) {
          20 _keys = new String[0];
          21 }
          22
          23 Fox(String... keys) {
          24 _keys = keys;
          25 }
          26
          27 final String[] _keys;
          28
          29 }
      ==========$ newjavac Quick.java
      + exec /usr/java1.5/bin/javac -J-esa -source 1.5 -target 1.5 Quick.java
      An exception has occurred in the compiler (1.5.0-auto-tiger). Please file a bug at the Java Developer Connection (http://java.sun.com/cgi-bin/bugreport.cgi) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
      java.lang.NullPointerException
              at com.sun.tools.javac.comp.Flow.visitNewClass(Flow.java:1105)
              at com.sun.tools.javac.tree.Tree$NewClass.accept(Tree.java:841)
              at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:34)
              at com.sun.tools.javac.comp.Flow.scanExpr(Flow.java:500)
              at com.sun.tools.javac.comp.Flow.visitVarDef(Flow.java:725)
              at com.sun.tools.javac.tree.Tree$VarDef.accept(Tree.java:511)
              at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:34)
              at com.sun.tools.javac.comp.Flow.scanStat(Flow.java:484)
              at com.sun.tools.javac.comp.Flow.scanStats(Flow.java:492)
              at com.sun.tools.javac.comp.Flow.visitBlock(Flow.java:732)
              at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:540)
              at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:34)
              at com.sun.tools.javac.comp.Flow.scanStat(Flow.java:484)
              at com.sun.tools.javac.comp.Flow.visitMethodDef(Flow.java:684)
              at com.sun.tools.javac.tree.Tree$MethodDef.accept(Tree.java:482)
              at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:34)
              at com.sun.tools.javac.comp.Flow.visitClassDef(Flow.java:624)
              at com.sun.tools.javac.tree.Tree$ClassDef.accept(Tree.java:438)
              at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:34)
              at com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:1223)
              at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:420)
              at com.sun.tools.javac.main.Main.compile(Main.java:584)
              at com.sun.tools.javac.main.Main.compile(Main.java:536)
              at com.sun.tools.javac.Main.compile(Main.java:44)
              at com.sun.tools.javac.Main.main(Main.java:35)
      ==========$

            gafter Neal Gafter (Inactive)
            gafter Neal Gafter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: