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..