From f0026006e7883e8949fda494e0f239b0dd24f7c8 Mon Sep 17 00:00:00 2001 From: jrm Date: Tue, 23 Jun 2015 22:05:03 -0700 Subject: [PATCH] [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. --- .../infer/CloseableAsResourceExample.java | 21 +++++++++++++++++++ .../tests/codetoanalyze/java/infer/Utils.java | 18 ++++++++++++++++ .../java/infer/CloseableAsResourceTest.java | 1 + 3 files changed, 40 insertions(+) create mode 100644 infer/tests/codetoanalyze/java/infer/Utils.java diff --git a/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java b/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java index 43e1edaf3..79ff3a856 100644 --- a/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java +++ b/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java @@ -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) { + } + } + } diff --git a/infer/tests/codetoanalyze/java/infer/Utils.java b/infer/tests/codetoanalyze/java/infer/Utils.java new file mode 100644 index 000000000..023ec3ec2 --- /dev/null +++ b/infer/tests/codetoanalyze/java/infer/Utils.java @@ -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) { + } + } + +} diff --git a/infer/tests/endtoend/java/infer/CloseableAsResourceTest.java b/infer/tests/endtoend/java/infer/CloseableAsResourceTest.java index 8f3c65c55..fd1dfed79 100644 --- a/infer/tests/endtoend/java/infer/CloseableAsResourceTest.java +++ b/infer/tests/endtoend/java/infer/CloseableAsResourceTest.java @@ -34,6 +34,7 @@ public class CloseableAsResourceTest { "withException", "notClosingCloseable", "notClosingWrapper", + "failToCloseWithCloseQuietly", }; assertThat( "Results should not contain resource leak errors",