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

generics: parametric exception type versus overriding

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.1
    • 5.0
    • tools
    • hopper
    • generic
    • solaris_7
    • Verified

      frog:~/workspaces/test/Generic9 $ cat -n T.java
           1 import java.io.*;
           2
           3 abstract class AChurchBoolean {
           4 public abstract <Return, Parameter, Throws extends Throwable>
           5 Return accept(IVisitor<Return, Parameter, Throws> visitor, Parameter parameter) throws Throws;
           6
           7 public interface IVisitor<Return, Parameter, Throws extends Throwable> {
           8 public Return caseTrue(Parameter parameter) throws Throws;
           9 public Return caseFalse(Parameter parameter) throws Throws;
          10 }
          11 }
          12
          13 class TrueChurchBoolean extends AChurchBoolean {
          14 private static TrueChurchBoolean instance = new TrueChurchBoolean();
          15 private TrueChurchBoolean() {}
          16 public static TrueChurchBoolean singleton() {
          17 return instance;
          18 }
          19 public <Return, Parameter, Throws extends Throwable>
          20 Return accept(IVisitor<Return, Parameter, Throws> visitor, Parameter parameter) throws Throws {
          21 return visitor.caseTrue(parameter);
          22 }
          23 }
          24
          25 class FalseChurchBoolean extends AChurchBoolean {
          26 private static FalseChurchBoolean instance = new FalseChurchBoolean();
          27 private FalseChurchBoolean() {}
          28 public static FalseChurchBoolean singleton() {
          29 return instance;
          30 }
          31 public <Return, Parameter, Throws extends Throwable>
          32 Return accept(IVisitor<Return, Parameter, Throws> visitor, Parameter parameter) throws Throws {
          33 return visitor.caseFalse(parameter);
          34 }
          35 }
          36
          37 class Pair<T,U> {
          38 private T first;
          39 private U second;
          40 Pair(T first, U second) {
          41 this.first = first;
          42 this.second = second;
          43 }
          44 T getFirst() {
          45 return first;
          46 }
          47 U getSecond() {
          48 return second;
          49 }
          50 }
          51
          52 // Perhaps a bit of a toy example, but relevant nonetheless.
          53 class ChurchBooleanTest {
          54 private AChurchBoolean bool;
          55 public ChurchBooleanTest(AChurchBoolean bool) {
          56 this.bool = bool;
          57 }
          58 public AChurchBoolean readIf(File file, byte[] output) throws IOException {
          59 return bool.accept(new AChurchBoolean.IVisitor<AChurchBoolean, Pair<File, byte[]>, IOException>() {
          60 public AChurchBoolean caseTrue(Pair<File, byte[]> parameter) throws IOException {
          61 FileInputStream input = new FileInputStream(parameter.getFirst()); // throws
          62 input.read(parameter.getSecond()); // throws
          63 input.close(); // throws
          64 return TrueChurchBoolean.singleton();
          65 }
          66 public AChurchBoolean caseFalse(Pair<File, byte[]> parameter) throws IOException {
          67 return FalseChurchBoolean.singleton();
          68 }
          69 }, new Pair<File, byte[]>(file, output));
          70 }
          71 }
      frog:~/workspaces/test/Generic9 $ newjavac -gj T.java
      T.java:20: accept(AChurchBoolean.IVisitor<Return,Parameter,Throws>,Parameter) in TrueChurchBoolean cannot override accept(AChurchBoolean.IVisitor<Return,Parameter,Throws>,Parameter) in AChurchBoolean; overridden method does not throw Throws
          Return accept(IVisitor<Return, Parameter, Throws> visitor, Parameter parameter) throws Throws {
                 ^
      T.java:32: accept(AChurchBoolean.IVisitor<Return,Parameter,Throws>,Parameter) in FalseChurchBoolean cannot override accept(AChurchBoolean.IVisitor<Return,Parameter,Throws>,Parameter) in AChurchBoolean; overridden method does not throw Throws
          Return accept(IVisitor<Return, Parameter, Throws> visitor, Parameter parameter) throws Throws {
                 ^
      2 errors
      frog:~/workspaces/test/Generic9 $

            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: