[infer][java] add working example with closeQuietly in the tests

Summary:
@public
Using the Closeable as resource approach allows to deal with the case of user defined varations of closeQuietly

Test Plan: Infer CI.
master
jrm 10 years ago
parent 11712caea9
commit f0026006e7

@ -8,6 +8,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.StringReader;
public class CloseableAsResourceExample {
class LocalException extends IOException {
@ -94,4 +95,24 @@ public class CloseableAsResourceExample {
ByteArrayInputStream stream = new ByteArrayInputStream(array);
}
void closingWithCloseQuietly() {
SomeResource r = null;
try {
r = new SomeResource();
r.doSomething();
} catch (IOException e) {
} finally {
Utils.closeQuietly(r);
}
}
void failToCloseWithCloseQuietly() {
try {
SomeResource r = new SomeResource();
r.doSomething();
Utils.closeQuietly(r);
} catch (IOException e) {
}
}
}

@ -0,0 +1,18 @@
// Copyright (c) 2015-Present Facebook. All rights reserved.
package codetoanalyze.java.infer;
import java.io.Closeable;
public class Utils {
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Exception ex) {
}
}
}

@ -34,6 +34,7 @@ public class CloseableAsResourceTest {
"withException",
"notClosingCloseable",
"notClosingWrapper",
"failToCloseWithCloseQuietly",
};
assertThat(
"Results should not contain resource leak errors",

Loading…
Cancel
Save