From 4b6d7541647552fdc526e4caa62212b0ed5a34a4 Mon Sep 17 00:00:00 2001 From: jrm Date: Fri, 15 Apr 2016 12:26:15 -0700 Subject: [PATCH] Some cleanup of the models where calls to instanceof are not necessary Summary: Removes some calls to `instanceof` that don't look necessary. Reviewed By: sblackshear Differential Revision: D3183253 fb-gh-sync-id: 2ffd36d fbshipit-source-id: 2ffd36d --- .../java/src/android/database/Cursor.java | 11 ++---- .../infer/models/InferCloseables.java | 35 ++++++------------- .../facebook/infer/models/InferUndefined.java | 2 -- .../fasterxml/jackson/core/JsonParser.java | 7 ++-- .../src/com/google/common/io/Closeables.java | 2 +- .../okhttp/internal/StrictLineReader.java | 33 +---------------- .../java/src/java/io/BufferedReader.java | 7 +++- .../java/src/java/io/FileInputStream.java | 9 +++-- .../java/src/java/io/FilterInputStream.java | 7 ++-- .../java/src/java/io/FilterOutputStream.java | 2 ++ .../models/java/src/java/io/InputStream.java | 10 +++--- infer/models/java/src/java/io/Reader.java | 12 +++---- 12 files changed, 39 insertions(+), 98 deletions(-) diff --git a/infer/models/java/src/android/database/Cursor.java b/infer/models/java/src/android/database/Cursor.java index 487b62f03..43c3452d1 100644 --- a/infer/models/java/src/android/database/Cursor.java +++ b/infer/models/java/src/android/database/Cursor.java @@ -10,20 +10,13 @@ package android.database; -import java.io.Closeable; - import com.facebook.infer.models.InferUndefined; import com.facebook.infer.models.InferBuiltins; -import android.database.sqlite.SQLiteCursor; - - -public class Cursor implements Closeable { +public class Cursor { public void close() { - if (this instanceof SQLiteCursor) { - ((SQLiteCursor) this).close(); - } + InferBuiltins.__set_mem_attribute(this); } public int getInt(int position) { diff --git a/infer/models/java/src/com/facebook/infer/models/InferCloseables.java b/infer/models/java/src/com/facebook/infer/models/InferCloseables.java index a9c6abc1a..dcd8e3df8 100644 --- a/infer/models/java/src/com/facebook/infer/models/InferCloseables.java +++ b/infer/models/java/src/com/facebook/infer/models/InferCloseables.java @@ -10,36 +10,21 @@ package com.facebook.infer.models; -import com.squareup.okhttp.internal.StrictLineReader; - -import java.io.*; +import java.io.Closeable; public final class InferCloseables { - private InferCloseables() { - } + private InferCloseables() { + } - public static void close(Closeable closeable, boolean swallowIOException) - throws IOException { - if (closeable == null) return; - if (closeable instanceof InputStream) { - ((InputStream) closeable).close(); - } else if (closeable instanceof OutputStream) { - ((OutputStream) closeable).close(); - } else if (closeable instanceof Reader) { - ((Reader) closeable).close(); - } else if (closeable instanceof Writer) { - ((Writer) closeable).close(); - } else if (closeable instanceof StrictLineReader) { - ((StrictLineReader) closeable).close(); - } + public static void close(Closeable closeable) { + if (closeable != null) { + InferBuiltins.__set_mem_attribute(closeable); } + } - public static void closeQuietly(Closeable closeable) { - try { - close(closeable, true); - } catch (IOException e) { - } - } + public static void closeQuietly(Closeable closeable) { + close(closeable); + } } diff --git a/infer/models/java/src/com/facebook/infer/models/InferUndefined.java b/infer/models/java/src/com/facebook/infer/models/InferUndefined.java index 9d4dcf3d3..7d3dfcccb 100644 --- a/infer/models/java/src/com/facebook/infer/models/InferUndefined.java +++ b/infer/models/java/src/com/facebook/infer/models/InferUndefined.java @@ -10,8 +10,6 @@ package com.facebook.infer.models; -import com.facebook.infer.models.InferBuiltins; - import java.io.IOException; import java.net.SocketException; import java.sql.SQLException; diff --git a/infer/models/java/src/com/fasterxml/jackson/core/JsonParser.java b/infer/models/java/src/com/fasterxml/jackson/core/JsonParser.java index 541bab09b..c3ecc415d 100644 --- a/infer/models/java/src/com/fasterxml/jackson/core/JsonParser.java +++ b/infer/models/java/src/com/fasterxml/jackson/core/JsonParser.java @@ -9,8 +9,8 @@ package com.fasterxml.jackson.core; +import com.facebook.infer.models.InferBuiltins; import com.facebook.infer.models.InferUndefined; -import com.fasterxml.jackson.core.json.UTF8StreamJsonParser; import java.io.Closeable; import java.io.IOException; @@ -19,9 +19,8 @@ public abstract class JsonParser implements Closeable, Versioned { public void close() throws IOException { - if (this instanceof UTF8StreamJsonParser) { - ((UTF8StreamJsonParser) this).close(); - } + InferBuiltins.__set_mem_attribute(this); + InferUndefined.can_throw_ioexception_void(); } private void throwExceptions() diff --git a/infer/models/java/src/com/google/common/io/Closeables.java b/infer/models/java/src/com/google/common/io/Closeables.java index 0bd53074d..97128c9e5 100644 --- a/infer/models/java/src/com/google/common/io/Closeables.java +++ b/infer/models/java/src/com/google/common/io/Closeables.java @@ -18,7 +18,7 @@ import java.io.IOException; public final class Closeables { public static void close(Closeable closeable, boolean swallowIOException) throws IOException { - InferCloseables.close(closeable, swallowIOException); + InferCloseables.close(closeable); if (!swallowIOException) InferUndefined.can_throw_ioexception_void(); } diff --git a/infer/models/java/src/com/squareup/okhttp/internal/StrictLineReader.java b/infer/models/java/src/com/squareup/okhttp/internal/StrictLineReader.java index 626e72729..ba454410b 100644 --- a/infer/models/java/src/com/squareup/okhttp/internal/StrictLineReader.java +++ b/infer/models/java/src/com/squareup/okhttp/internal/StrictLineReader.java @@ -11,47 +11,16 @@ package com.squareup.okhttp.internal; import com.facebook.infer.models.InferUndefined; -import java.io.Closeable; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; -public class StrictLineReader implements Closeable { - - private InputStream in; - private Charset charset; - private byte[] buf; - - public StrictLineReader(InputStream in, Charset charset) { - this(in, 8192, charset); - } - - public StrictLineReader(InputStream in, int capacity, Charset charset) { - if (in == null) { - throw new NullPointerException(); - } - if (capacity < 0) { - throw new IllegalArgumentException("capacity <= 0"); - } - - this.in = in; - this.charset = charset; - buf = new byte[capacity]; - } - - public void close() throws IOException { - in.close(); - InferUndefined.can_throw_ioexception_void(); - } +public class StrictLineReader { public String readLine() throws IOException { return InferUndefined.can_throw_ioexception_string(); } - public int readInt() throws IOException { return InferUndefined.can_throw_ioexception_int(); } - } diff --git a/infer/models/java/src/java/io/BufferedReader.java b/infer/models/java/src/java/io/BufferedReader.java index 9e61421ec..7dfea765c 100644 --- a/infer/models/java/src/java/io/BufferedReader.java +++ b/infer/models/java/src/java/io/BufferedReader.java @@ -24,8 +24,13 @@ public class BufferedReader extends Reader { } public void close() throws IOException { - if (in instanceof InputStreamReader) + if (in instanceof InputStreamReader) { ((InputStreamReader) in).close(); + } else if (in instanceof BufferedReader) { + ((BufferedReader) in).close(); + } else { + in.close(); + } } public int read() throws IOException { diff --git a/infer/models/java/src/java/io/FileInputStream.java b/infer/models/java/src/java/io/FileInputStream.java index 27af7c4bb..807772101 100644 --- a/infer/models/java/src/java/io/FileInputStream.java +++ b/infer/models/java/src/java/io/FileInputStream.java @@ -45,6 +45,10 @@ public class FileInputStream extends InputStream { init(); } + public void close() throws IOException { + super.close(); + } + public FileChannel getChannel() { channel = new FileChannelImpl(this, fd, InferUndefined.int_undefined()); return channel; @@ -54,11 +58,6 @@ public class FileInputStream extends InputStream { return InferUndefined.can_throw_ioexception_int(); } - public void close() throws IOException { - InferBuiltins.__set_mem_attribute(this); - InferUndefined.can_throw_ioexception_void(); - } - @Override public int read() throws IOException { return InferUndefined.can_throw_ioexception_int(); diff --git a/infer/models/java/src/java/io/FilterInputStream.java b/infer/models/java/src/java/io/FilterInputStream.java index e682588f1..ac246971a 100644 --- a/infer/models/java/src/java/io/FilterInputStream.java +++ b/infer/models/java/src/java/io/FilterInputStream.java @@ -19,10 +19,6 @@ public class FilterInputStream extends InputStream { this.in = in; } - public FilterInputStream() { - super(); - } - public int available() throws IOException { return InferUndefined.can_throw_ioexception_int(); } @@ -31,6 +27,8 @@ public class FilterInputStream extends InputStream { if (in != null) { if (in instanceof FileInputStream) { ((FileInputStream) in).close(); + } else if (in instanceof BufferedInputStream) { + ((FilterInputStream) in).close(); } else { in.close(); } @@ -53,7 +51,6 @@ public class FilterInputStream extends InputStream { InferUndefined.can_throw_ioexception_void(); } - public long skip(long n) throws IOException { return InferUndefined.can_throw_ioexception_long(); } diff --git a/infer/models/java/src/java/io/FilterOutputStream.java b/infer/models/java/src/java/io/FilterOutputStream.java index 10de502d9..62affa949 100644 --- a/infer/models/java/src/java/io/FilterOutputStream.java +++ b/infer/models/java/src/java/io/FilterOutputStream.java @@ -26,6 +26,8 @@ public class FilterOutputStream extends OutputStream { if (out != null) { if (out instanceof FileOutputStream) { ((FileOutputStream) out).close(); + } else { + out.close(); } } } diff --git a/infer/models/java/src/java/io/InputStream.java b/infer/models/java/src/java/io/InputStream.java index 4db5de7cb..b2883bcc5 100644 --- a/infer/models/java/src/java/io/InputStream.java +++ b/infer/models/java/src/java/io/InputStream.java @@ -9,16 +9,14 @@ package java.io; +import com.facebook.infer.models.InferBuiltins; import com.facebook.infer.models.InferUndefined; -public class InputStream implements Closeable { +public class InputStream { public void close() throws IOException { - if (this instanceof FileInputStream) { - ((FileInputStream) this).close(); - } else if (this instanceof FilterInputStream) { - ((FilterInputStream) this).close(); - } + InferBuiltins.__set_mem_attribute(this); + InferUndefined.can_throw_ioexception_void(); } public int read() throws IOException { diff --git a/infer/models/java/src/java/io/Reader.java b/infer/models/java/src/java/io/Reader.java index 47a227221..0717a6bfd 100644 --- a/infer/models/java/src/java/io/Reader.java +++ b/infer/models/java/src/java/io/Reader.java @@ -9,18 +9,14 @@ package java.io; +import com.facebook.infer.models.InferBuiltins; import com.facebook.infer.models.InferUndefined; -public abstract class Reader implements Closeable { +public abstract class Reader { public void close() throws IOException { - if (this instanceof InputStreamReader) { - ((InputStreamReader) this).close(); - } else if (this instanceof BufferedReader) { - ((BufferedReader) this).close(); - } else if (this instanceof FilterReader) { - ((FilterReader) this).close(); - } + InferBuiltins.__set_mem_attribute(this); + InferUndefined.can_throw_ioexception_void(); } public int read() throws IOException {