Make AutoCloseable Reserve
Make a second version of your library using backport instead of the real thing
Either make the user choose which library to use by actually publishing as two artifacts, or create a common facade that loads the correct version using reflection.
For backporting:
AutoClosable can be reused as is.
For each implementation, you will need an adapter for this interface.
Trying with resources A statement might look something like
abstract public class Try { private List<AutoClosable> closables = new ArrayList<>(); public void register(AutoClosable closable){ closables.add(closable); } abstract void execute(); public void do(){ try{ execute() } finally { for (AutoClosable c : closables) c.close()
Usage will look something like this:
new Try(){ File f = new File(); { register(closable(f)) // closabe would return an Adapter from File to AutoClosable } void execute(){ f.doSomeFunStuff() } }.do();
source share