Yes, you can scope to your hearts content with {} blocks. For those not aware of this:
There's a rarely used feature of the java language: separate blocks. You can put { (statements) } anywhere in java code where a statement is legal. Like any other occurrence of {} to delimit code, any variable declarations inside the {} are not visible outside the brackets. So, this: public void test() { int foo = 1; { int bar = foo + 2; } //MARK } is legal, and 'bar' will not be visible at the mark. Because of this feature, @Cleanup is what it is; if you want an explicit close point, then use a block. In practice, a decent amount of resources you may want to @Cleanup require try/catch blocks, which are of course also their own scope: try { @Cleanup InputStream in = new FileInputStream(path); } catch ( IOException e ) { throw new DatabaseAccessException(e); } After all, the act of creating a resource tends to throw the same kinds of exceptions as using the resource. Funny thing, this @Cleanup thing. I never considered this style of ARM until forced into it by lombok's particular limitations (Specifically: it needs code that passes the tokenizer and tree maker before lombok can run its transformations), and then it turns out it's in many ways superior to the existing proposal. I don't think coin is going to go this route though, it's a bit late for such a drastic change. On Sep 1, 4:31 pm, Jess Holle <je...@ptc.com> wrote: > I actually prefer @Cleanup to the JDK 7 proposal in that it presumably > just cleans up at the end of the variable's scope ala C++. > > If you need ordering amongst several @Cleanup variables, then you can > always introduce {...} scoping, right? If so, then I'd rather introduce > such things only as needed rather than the JDK 7 proposal's approach. > > > > Roel Spilker wrote: > > Casper, > > > You are correct. I just wanted the other readers to know that it is > > not limited to "close". It is however limited to no-args methods. > > > Roel > > > On Sep 1, 4:10 pm, Casper Bang <casper.b...@gmail.com> wrote: > > >> I think you misunderstood Roel. I am aware of the method name argument > >> override, I was merely exploring the different implementations and > >> implications of these two ARM blocks. The JDK7 version would have to > >> rely on a new interface Disposable being added to the libraries, as > >> the abomination known as checked exceptions (IOException) once again > >> gets in the way of simply using the existing Closable. Lombok couldn't > >> care less what it's closing and could in fact be used as a C++ > >> deconstructor. Correct? > > >> /Casper > > >> On 1 Sep., 15:47, Roel Spilker <r.spil...@gmail.com> wrote: > > >>> Caster, that is not entirely correct. The "close" methode (actually, > >>> close is just the default, you can specify an other method name if you > >>> want) will be called when the variable goes out of scope. > >>> Unfortunately, java does not allow an annotation on a code block. So > >>> an equivalent programm would be: > > >>> { > >>> @Cleanup InputStream in = new FileInputStream(...); > >>> // Do something else... > > >>> } > > >>> On Sep 1, 3:10 pm, Casper Bang <casper.b...@gmail.com> wrote: > > >>>> The neat thing about this is that we do not have to wait until the > >>>> Java API has been retrofitted with the Disposable interface. However, > >>>> you won't get the scope limitation benefits (the in and out variable > >>>> is scoped the whole method). I kind of wish we could use annotations > >>>> on blocks, so this would be possible instead: > > >>>> @Cleanup{ > >>>> InputStream in = new FileInputStream( ... ); > >>>> // Do something... > > >>>> } > > >>>> @Cleanup{ > >>>> InputStream in = new FileInputStream( ... ); > >>>> // Do something else... > > >>>> } > > >>>> /Casper > > >>>> On 1 Sep., 14:53, Roel Spilker <r.spil...@gmail.com> wrote: > > >>>>> For ARM-blocks you can have a look at the @Cleanup annotation of > >>>>> Lombok and have ARM-blocks for Java right now! > > >>>>> The following code will close both streams correctly after they run > >>>>> out of scope. > > >>>>> import lombok.Cleanup; > >>>>> import java.io.*; > > >>>>> public class CleanupExample { > >>>>> public static void main(String[] args) throws IOException { > >>>>> @Cleanup InputStream in = new FileInputStream(args[0]); > >>>>> @Cleanup OutputStream out = new > >>>>> FileOutputStream(args[1]); > >>>>> byte[] b = new byte[10000]; > >>>>> while (true) { > >>>>> int r = in.read(b); > >>>>> if (r == -1) break; > >>>>> out.write(b, 0, r); > >>>>> } > >>>>> } > > >>>>> } > > >>>>> Seehttp://projectlombok.org/features/Cleanup.htmlformore > >>>>> information > > >>>>> On Sep 1, 2:19 pm, "joel.neely" <joel.ne...@gmail.com> wrote: > > >>>>>> According to coverage > >>>>>> athttp://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/10 > >>>>>> , Snow Leopard, the latest version of Mac OS X, added "blocks" to C. > >>>>>> The article illustrates this new language construct with the by-now > >>>>>> canonical ARM and home-grown-control-structure examples. > > >>>>>> Hey, Java! Closures to the left of me [JRuby, Scala, etc.], blocks to > >>>>>> the right [C on OS X], here I am, stuck in the middle with you! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---