From 398e97183f7378cfb751cd52c6816cdb5a63ad87 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Wed, 25 Jul 2018 07:49:21 -0700 Subject: [PATCH] [java] record Closeable example from #49 Summary: This previous FP does not occur anymore, make sure it doesn't regress. https://github.com/facebook/infer/issues/49 Reviewed By: dulmarod Differential Revision: D8442842 fbshipit-source-id: b3fde1ad1 --- .../infer/CloseableAsResourceExample.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java b/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java index 779601178..f0c95e25c 100644 --- a/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java +++ b/infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java @@ -10,6 +10,9 @@ package codetoanalyze.java.infer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; +import java.io.File; +import java.io.FileWriter; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringReader; import java.util.Map; @@ -219,4 +222,28 @@ public class CloseableAsResourceExample { map.put(key, res); } + public static void closeCloseable(Closeable closeable) { + try { + if (closeable != null) { + closeable.close(); + } + } catch (Exception ex) {} + } + + public void finallyCloseOk(File file, String fileContent) { + if (!file.exists()) { + FileWriter writer = null; + try { + writer = new FileWriter(file); + writer.write(fileContent); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + closeCloseable(writer); + } + } + } + }