JDK-7164542 : try-with-resources: problem with intersection types
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: unknown
  • Submitted: 2012-04-26
  • Updated: 2012-06-12
  • Resolved: 2012-06-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b40Fixed
Related Reports
Relates :  
Description
I am writing to point out an anomaly in the Java compiler (JDK 7u3).
Here is the code (it is a slightly revisited example taken from the book
"Java Generics and Collections" by Naftalin and Wadler).

public class Test {

    public static <S extends AutoCloseable & Readable,
                       T extends AutoCloseable & Appendable>
                       void copy(S s, T t, int size) throws Exception {
        try (S src = s; T trg = t) {
            CharBuffer buf = CharBuffer.allocate(size);
            int i = src.read(buf);
            while (i >= 0) {
                buf.flip(); // prepare buffer for writing
                trg.append(buf);
                buf.clear(); // prepare buffer for reading
                i = src.read(buf);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        int size = 32;
        FileReader r = new FileReader(args[0]);
        FileWriter w = new FileWriter(args[1]);
        copy(r, w, size);
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
        copy(br, bw, size);
    }

}


Problem:
as it is, the program compiles and executes correctly, but if the bounds
AutoCloseable and Readable are swapped (same problem with Appendable),
then the compiler issues the following error:

 /home/davide/Didattica/MGP/11-12/javaCode/lect07/src/Test.java:8: error: cannot find symbol
        try (S src = s; T trg = t) {
        ^
  symbol:   method close()
  location: interface Readable
Fatal Error: Unable to find method close

I guess it has to do with the fact that the lhs bounds are used for erasure
and with the translation of the try-with construct, however the behavior is
anomalous.

Comments
EVALUATION Fix is to add AutoCloseable cast before calling close method on resources.
16-05-2012