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

Reply via email to