Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind java.lang.AutoCloseable as IDisposable #484

Open
jonpryor opened this issue Aug 23, 2019 · 0 comments
Open

Bind java.lang.AutoCloseable as IDisposable #484

jonpryor opened this issue Aug 23, 2019 · 0 comments
Labels
enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.)

Comments

@jonpryor
Copy link
Member

Context: dotnet/android@d3e5974

Background

Java 8 introduced a try-with-resources statement, which is conceptually similar to the C# using block:

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
    String line = br.readLine();
}

The Java compiler expands this into a try-finally block:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
    String line = br.readLine();
}
finally {
    if (br != null) br.close();
}

try-with-resources can be used with any type which implements java.lang.AutoCloseable.

Additionally, java.io.Closeable now implements java.lang.AutoCloseable, so all java.io constructs support try-with-resources syntax.

Binding

generator should natively support this feature: if a type implements Java.Lang.IAutoCloseable, then generation for the type should override Java.Lang.Object.Dispose(bool) to have it call Close().

For example, Java.IO.FileOutputStream implements Java.IO.ICloseable, and thus it implements Java.Lang.IAutoCloseable. generator should thus emit:

partial class FileOutputStream {
    protected override void Dispose (bool disposing)
    {
        if (disposing && PeerReference.IsValid) {
            Close ();
        }
        base.Dispose (disposing);
    }
}

By doing so, C# using blocks work as expected:

using (var f = new Java.IO.FileOutputStream("path")) {
    byte[] data = Encoding.UTF8.GetBytes("Test string...");
    stream.Write(data, 0, data.Length);
}
// f.Close() is called at end-of-block, writing data to disk

Open Questions

Do we need to worry about calling Close() multiple times? FileOutputStream inherits from OutputStream, both of which implement ICloseable. Will it be a problem when both OutputStream and FileOutputStream override Object.Dispose(bool) to call Close()?

  • I believe this won't be a problem, as the Closeable.close() docs state:

    If the stream is already closed then invoking this method has no effect.

@jpobst jpobst added enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.) labels Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Proposed change to current functionality generator Issues binding a Java library (generator, class-parse, etc.)
Projects
None yet
Development

No branches or pull requests

2 participants