4

Is there any helper method in the JDK or common libraries that does this:

if (resource instanceof AutoCloseable) {
    ((AutoCloseable) resource).close();
}

Just a one-liner to call an object's close() if applicable.

I know about try-with-resources, that's not applicable to this situation. And I know that not all classes that have a close() method implement AutoCloseable. But, I seem to write the above over and over..

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
tariksbl
  • 1,069
  • 6
  • 20
  • 1
    That looks like a XY problem. Can you tell more about the context? You should not have to use `instanceof`. – fge Mar 23 '14 at 20:07
  • fge: yes a clarification is in order: I have a ref to an interface, at compile time I can't know if the impl also implements AutoCloseable in addition to the interface. – tariksbl Mar 23 '14 at 20:38
  • Uhm, yes you can: `public void doSomethingWith(T victim)` – fge Mar 23 '14 at 20:40
  • That would restrict to AutoCloseable impls; I'm looking to ".. call an object's close() *if applicable* ". – tariksbl Mar 23 '14 at 20:53
  • 1
    That really doesn't look right at all. There is something fundamentally wrong with this approach, you should explain your problem more. – fge Mar 23 '14 at 20:56
  • I don't get it. You shouldn't have code that closes resources it didn't allocate. Resource-allocating code should use an ARM block. – erickson Mar 23 '14 at 21:41

3 Answers3

0

There's something Apache Commons offers:

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.Closeable)

Haris Osmanagić
  • 1,249
  • 12
  • 28
  • 4
    Doesn't help. For one, there is no requirement that an `AutoCloseable` also implements `Closeable`. And second, this utility pales in comparison to Guava's `Closer`. – fge Mar 23 '14 at 20:08
0

Here is apache commons closeQuietly adapted for AutoCloseable:

  static void closeQuietly(AutoCloseable closeable) {
    try {
      if (closeable != null) {
        closeable.close();
      }
    }
    catch (Exception swallowed) {
    }
  }

since google sent me here for that case :)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
-1

Edit:

Check this:

class CloserHelper
{
    public static void close(Object object)
    {
        if (object instanceof AutoCloseable)
        {
            try
            {
                ((AutoCloseable) object).close();
            }
            catch (Exception ignored) { }
        }
    }
}

I can think to something like this

class CloserHelper
{
    public static void close(AutoCloseable obj) throws Exception
    {
        obj.close();
    }
}

Then

CloserHelper.close(resource);

If the object is not a AutoCloseable you cannot just call it


If you want to ignore exceptions

class CloserHelper
{
    public static void close(AutoCloseable obj)
    {
        try
        {
            obj.close();
        }
        catch (Exception e) { }
    }
}
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • Marco: I have a ref to an interface, at compile time I can't know if the impl also implements AutoCloseable in addition to the interface. – tariksbl Mar 23 '14 at 20:22
  • `catch (Exception e)` <-- bad. You also catch all unchecked exceptions with that. And that includes `NullPointerEception`. – fge Mar 23 '14 at 20:43
  • @fge Yes, my code want to ignore all exceptions. Anyway instanceof will avoid null so no NullPointerException – Marco Acierno Mar 23 '14 at 20:44