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

javac: compilation success depends on compilation order

XMLWordPrintable

    • 5.0
    • 5.0
    • b55
    • generic
    • generic
    • Verified

      Below are three files: TreeInfo.java, TreeScanner.java and Tree.java.
      If they are compiled with
      javac p/Tree.java p/TreeScanner.java p/TreeInfo.java
      the compilation succeeds.

      If they are compiled with
      javac p/TreeInfo.java p/TreeScanner.java p/Tree.java
      you get the following error:

      p/TreeScanner.java:9: unreported exception java.lang.Throwable; must be caught or declared to be thrown
      if(tree!=null) tree.accept(this);
                                        ^
      1 error



      ------Tree.java--------------
      package p;

      public abstract class Tree {

          /** Visit this tree with a given visitor.
           */
          public abstract <E extends Throwable> void accept(Visitor<E> v) throws E;


          /** A generic visitor class for trees.
           */
          public static abstract class Visitor<E extends Throwable> {
      public void visitTree(Tree that) throws E { assert false; }
          }
      }


      ------TreeInfo.java----------
      package p;
      import p.Tree.*;

      public class TreeInfo {

          public static void declarationFor(final Tree tree) {
      class DeclScanner extends TreeScanner<Error> {
                  public void scan(Tree tree) {
                  }
      }
      DeclScanner s = new DeclScanner();
      tree.accept(s);
          }
      }


      ------TreeScanner.java-------
      package p;
      import p.Tree.*;

      public class TreeScanner<E extends Throwable> extends Visitor<E> {

          /** Visitor method: Scan a single node.
           */
          public void scan(Tree tree) throws E {
      if(tree!=null) tree.accept(this);
          }
      }

            jjg Jonathan Gibbons
            jjg Jonathan Gibbons
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: