From 9c90d403307c2bc3ce410a98b3f408cda885fcd5 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 3 Dec 2009 15:31:34 +0100 Subject: Cleaned up TODO.txt. At this point it's more documentation of failed experiments than a todo list, so renamed it too. --- doc/experiences.txt | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 doc/experiences.txt (limited to 'doc/experiences.txt') diff --git a/doc/experiences.txt b/doc/experiences.txt new file mode 100644 index 00000000..90fd0c2d --- /dev/null +++ b/doc/experiences.txt @@ -0,0 +1,43 @@ +## Fix HandleCleanup + +Consider requiring an initializer and warn when the varname gets reassigned, if the declaration wasn't already final. Think about this more. + +Right now exceptions thrown by the cleanup method will mask any exceptions thrown in the main body, which is not wanted. This does not appear to be doable without java 7 features or very extensive additions to the lombok framework. + +A lot has been tried: + + Tried tactics, and why they won't work: + + - Replace every 'return', 'break', and 'continue' statement (for the latter 2, only if they break/continue out of the try block) with a block that first sets a uniquely named flag before doing the operation. + Then, check that flag in the finally block to see if the cleanup call should be guarded by a try/catchThrowable. This doesn't work, because its not possible to instrument the 4th way out of a try block: + just running to the end of it. Putting a 'flag = true;' at the end isn't safe, because that might be unreachable code. + + - Put catch blocks in for all relevant exceptions (RuntimeException, Error, all exceptions declared as thrown by the method, and all types of exceptions of the catch blocks of encapsulating try blocks. + This doesn't work, partly because it'll fail for sneakily thrown exceptions, but mostly because you can't just catch an exception listed in the 'throws' clause of the method body; catching an exception + that no statement in the try block can throw is a compile time error, but it is perfectly allright to declare these as 'thrown'. + + - Put in a blanket catch Throwable to set the flag. Two problems with this: + First, sneaky throw can't be done. Thread.stop invokes a security manager and triggers a warning, Calling a sneakyThrow method creates a runtime dependency on lombok, constructing a sneakyThrow in-class + requires either lots of "$1" classes that clog up permgen, or are slow and require reflection tricks to load live off of a byte array literal. + Secondly, this would mean that any statements in the try body that throw an exception aren't flagged to the user as needing to be handled. Unacceptable. + + The Cleanup annotation now also calls the cleanup method for you, and will call it at the END of the current scope. The following plans have been tried and abandoned: + + - Cleanup right after the final mention. This doesn't work, because the final mention may not be the final use-place. Example: + @Cleanup InputStream in = new FileInputStream(someFile); + InputStreamReader reader = new InputStreamReader(in); + reader.read(); //oops - in is already closed by now. + + - Require an explicit var.cleanup() call and consider that the cue to close off the try block. + This doesn't work either, because now variables set in between the @Cleanup declaration and the var.cleanup() call become invisible to following statements. Example: + @Cleanup InputStream in = new FileInputStream(someFile); + int i = in.read(); + in.close(); + System.out.println(i); //fail - i is inside the generated try block but this isn't, so 'i' is not visible from here. + + By running to the end of visible scope, all these problems are avoided. This does remove the flexibility of declaring where you want a close call to be executed, but there are two mitigating factors available: + + 1) Create an explicit scope block. You can just stick { code } in any place where you can legally write a statement, in java. This is relatively unknown, so I expect most users will go for: + + 2) Just call close explicitly. I've yet to see a cleanup method which isn't idempotent anyway (calling it multiple times is no different than calling it once). + \ No newline at end of file -- cgit