You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Damjan Jovanovic <da...@apache.org> on 2013/11/03 09:30:04 UTC

Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com> wrote:

> On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <da...@gmail.com> wrote:
> >
> > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
> > <Jo...@scalaris.com> wrote:
> > > Hi Damjan,
> > >
> > > Damjan Jovanovic wrote:
> > >
> > > [snip]
> > >
> > > Thanks for explanation.
> > >
> > >> We would be able to adapt that for Java < 1.7 by swallowing the close
> > >> exception instead of calling addSuppressed() on the primary exception,
> > >> but the show stopper is catching and rethrowing the primary exception
> > >> (Throwable), which javac < 1.7 refuses to compile because it doesn't
> > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
> > >> (
>
> http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
> ).
> > >>
> > >> But this would work and always sets succeeded correctly without
> > >> catch/re-throw:
> > >>
> > >> final InputStream is = factoryMethodThatCanThrow();
> > >> boolean succeeded = false;
> > >> try {
> > >>     try {
> > >>         is.methodThatCanThrow();
> > >>     } finally {
> > >>     }
> > >>     succeeded = true;
> > >> } finally {
> > >>     closeSafely(!succeeded, is);
> > >> }
> > >
> > > I guess the nested try was unintentionally ;-)
> > >
> > > Cheers,
> > > Jörg
> >
> > Well that actually won't work, because the "succeeded = true;" will be
> > skipped if there is a "return;" in the inner try.
> >
> > Other than a custom Java compiler, I guess there's no clean way of
> > doing this in Java < 1.7. There's really only option 2 - with being
> > careful to always set succeeded correctly on all paths out of the try
> > block. Almost like releasing memory in C.
> >
>
> I haven't deeply followed this conversation, but would this be a candidate
> for a [weaver] module?
>
> Matt
>
>

[weaver] I am less sure about, but by playing with Objectweb's ASM I did
manage to compile code with try-with-resources on Java 7, change the class
file version, do some bytecode manipulation to delete calls to
Throwable.addSuppressed(), and get it to work on Java < 7.

Made it into a Maven plugin and it works well :). Also verified other Java
7 language features (binary literals, underscores in numeric literals,
strings in switch, diamonds, exception multi-catch and re-throw) work on
Java < 7 because they're just syntactic sugar.

It's very small and simple - 143 lines in 1 file. Is it worth adding as a
new commons project? Or do we not host (projects that contain) Maven
plugins?

Damjan

Re: Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

Posted by Matt Benson <gu...@gmail.com>.
On Nov 4, 2013 1:12 AM, "Damjan Jovanovic" <da...@apache.org> wrote:
>
> For the record, I would only use this for imaging >= 2.0.
>
> Weaver looked undocumented and undecipherable to me, but let me look at it
> again slowly.

Tell me how you really feel! :-)

More seriously, tell me how to fix these problems. There is an outstanding
request for a basic how-to doc from the user perspective. Most likely there
should be a guide to writing a Weaver module as well.

Matt

>
> Damjan
>
>
> On Sun, Nov 3, 2013 at 9:14 PM, Matt Benson <gu...@gmail.com> wrote:
>
> > I wasn't under the impression this was blocking imaging 1.0. In any
case,
> > framing Damjan's work as a Weaver module would still use the same work
he's
> > done with ASM and would really only utilize a standard mechanism for
> > triggering the process. It would also avoid proliferation of multiple
maven
> > plugins with essentially the same mission: post-processing bytecode.
> >
> > Matt
> > On Nov 3, 2013 10:08 AM, "Gary Gregory" <ga...@gmail.com> wrote:
> >
> > > On Sun, Nov 3, 2013 at 10:56 AM, Matt Benson <gu...@gmail.com>
> > wrote:
> > >
> > >> Sounds like it would be trivial to frame what you've done here into
the
> > >> context of a Weaver module, for which there exist both a maven plugin
> > and
> > >> an Antlib, as well as a need for a near-term release. :-)
> > >>
> > >
> > > and eating our own dog food :)
> > >
> > > We may pushing the limits of Dajman's patience for a 1.0 which is long
> > > overdue.
> > >
> > > I suppose D needs to weigh using ASM or [weaver] vs. adjusting the
> > current
> > > code with Java 5.
> > >
> > > Gary
> > >
> > >>
> > >> Matt
> > >> On Nov 3, 2013 2:30 AM, "Damjan Jovanovic" <da...@apache.org> wrote:
> > >>
> > >> > On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com>
> > >> wrote:
> > >> >
> > >> > > On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <damjan.jov@gmail.com
>
> > >> > wrote:
> > >> > > >
> > >> > > > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
> > >> > > > <Jo...@scalaris.com> wrote:
> > >> > > > > Hi Damjan,
> > >> > > > >
> > >> > > > > Damjan Jovanovic wrote:
> > >> > > > >
> > >> > > > > [snip]
> > >> > > > >
> > >> > > > > Thanks for explanation.
> > >> > > > >
> > >> > > > >> We would be able to adapt that for Java < 1.7 by swallowing
the
> > >> > close
> > >> > > > >> exception instead of calling addSuppressed() on the primary
> > >> > exception,
> > >> > > > >> but the show stopper is catching and rethrowing the primary
> > >> > exception
> > >> > > > >> (Throwable), which javac < 1.7 refuses to compile because it
> > >> doesn't
> > >> > > > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
> > >> > > > >> (
> > >> > >
> > >> > >
> > >> >
> > >>
> >
http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
> > >> > > ).
> > >> > > > >>
> > >> > > > >> But this would work and always sets succeeded correctly
without
> > >> > > > >> catch/re-throw:
> > >> > > > >>
> > >> > > > >> final InputStream is = factoryMethodThatCanThrow();
> > >> > > > >> boolean succeeded = false;
> > >> > > > >> try {
> > >> > > > >>     try {
> > >> > > > >>         is.methodThatCanThrow();
> > >> > > > >>     } finally {
> > >> > > > >>     }
> > >> > > > >>     succeeded = true;
> > >> > > > >> } finally {
> > >> > > > >>     closeSafely(!succeeded, is);
> > >> > > > >> }
> > >> > > > >
> > >> > > > > I guess the nested try was unintentionally ;-)
> > >> > > > >
> > >> > > > > Cheers,
> > >> > > > > Jörg
> > >> > > >
> > >> > > > Well that actually won't work, because the "succeeded = true;"
> > will
> > >> be
> > >> > > > skipped if there is a "return;" in the inner try.
> > >> > > >
> > >> > > > Other than a custom Java compiler, I guess there's no clean
way of
> > >> > > > doing this in Java < 1.7. There's really only option 2 - with
> > being
> > >> > > > careful to always set succeeded correctly on all paths out of
the
> > >> try
> > >> > > > block. Almost like releasing memory in C.
> > >> > > >
> > >> > >
> > >> > > I haven't deeply followed this conversation, but would this be a
> > >> > candidate
> > >> > > for a [weaver] module?
> > >> > >
> > >> > > Matt
> > >> > >
> > >> > >
> > >> >
> > >> > [weaver] I am less sure about, but by playing with Objectweb's ASM
I
> > did
> > >> > manage to compile code with try-with-resources on Java 7, change
the
> > >> class
> > >> > file version, do some bytecode manipulation to delete calls to
> > >> > Throwable.addSuppressed(), and get it to work on Java < 7.
> > >> >
> > >> > Made it into a Maven plugin and it works well :). Also verified
other
> > >> Java
> > >> > 7 language features (binary literals, underscores in numeric
literals,
> > >> > strings in switch, diamonds, exception multi-catch and re-throw)
work
> > on
> > >> > Java < 7 because they're just syntactic sugar.
> > >> >
> > >> > It's very small and simple - 143 lines in 1 file. Is it worth
adding
> > as
> > >> a
> > >> > new commons project? Or do we not host (projects that contain)
Maven
> > >> > plugins?
> > >> >
> > >> > Damjan
> > >> >
> > >>
> > >
> > >
> > >
> > > --
> > > E-Mail: garydgregory@gmail.com | ggregory@apache.org
> > > Java Persistence with Hibernate, Second Edition<
> > http://www.manning.com/bauer3/>
> > > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > > Spring Batch in Action <http://www.manning.com/templier/>
> > > Blog: http://garygregory.wordpress.com
> > > Home: http://garygregory.com/
> > > Tweet! http://twitter.com/GaryGregory
> > >
> >

Re: Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

Posted by Damjan Jovanovic <da...@apache.org>.
For the record, I would only use this for imaging >= 2.0.

Weaver looked undocumented and undecipherable to me, but let me look at it
again slowly.

Damjan


On Sun, Nov 3, 2013 at 9:14 PM, Matt Benson <gu...@gmail.com> wrote:

> I wasn't under the impression this was blocking imaging 1.0. In any case,
> framing Damjan's work as a Weaver module would still use the same work he's
> done with ASM and would really only utilize a standard mechanism for
> triggering the process. It would also avoid proliferation of multiple maven
> plugins with essentially the same mission: post-processing bytecode.
>
> Matt
> On Nov 3, 2013 10:08 AM, "Gary Gregory" <ga...@gmail.com> wrote:
>
> > On Sun, Nov 3, 2013 at 10:56 AM, Matt Benson <gu...@gmail.com>
> wrote:
> >
> >> Sounds like it would be trivial to frame what you've done here into the
> >> context of a Weaver module, for which there exist both a maven plugin
> and
> >> an Antlib, as well as a need for a near-term release. :-)
> >>
> >
> > and eating our own dog food :)
> >
> > We may pushing the limits of Dajman's patience for a 1.0 which is long
> > overdue.
> >
> > I suppose D needs to weigh using ASM or [weaver] vs. adjusting the
> current
> > code with Java 5.
> >
> > Gary
> >
> >>
> >> Matt
> >> On Nov 3, 2013 2:30 AM, "Damjan Jovanovic" <da...@apache.org> wrote:
> >>
> >> > On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com>
> >> wrote:
> >> >
> >> > > On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <da...@gmail.com>
> >> > wrote:
> >> > > >
> >> > > > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
> >> > > > <Jo...@scalaris.com> wrote:
> >> > > > > Hi Damjan,
> >> > > > >
> >> > > > > Damjan Jovanovic wrote:
> >> > > > >
> >> > > > > [snip]
> >> > > > >
> >> > > > > Thanks for explanation.
> >> > > > >
> >> > > > >> We would be able to adapt that for Java < 1.7 by swallowing the
> >> > close
> >> > > > >> exception instead of calling addSuppressed() on the primary
> >> > exception,
> >> > > > >> but the show stopper is catching and rethrowing the primary
> >> > exception
> >> > > > >> (Throwable), which javac < 1.7 refuses to compile because it
> >> doesn't
> >> > > > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
> >> > > > >> (
> >> > >
> >> > >
> >> >
> >>
> http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
> >> > > ).
> >> > > > >>
> >> > > > >> But this would work and always sets succeeded correctly without
> >> > > > >> catch/re-throw:
> >> > > > >>
> >> > > > >> final InputStream is = factoryMethodThatCanThrow();
> >> > > > >> boolean succeeded = false;
> >> > > > >> try {
> >> > > > >>     try {
> >> > > > >>         is.methodThatCanThrow();
> >> > > > >>     } finally {
> >> > > > >>     }
> >> > > > >>     succeeded = true;
> >> > > > >> } finally {
> >> > > > >>     closeSafely(!succeeded, is);
> >> > > > >> }
> >> > > > >
> >> > > > > I guess the nested try was unintentionally ;-)
> >> > > > >
> >> > > > > Cheers,
> >> > > > > Jörg
> >> > > >
> >> > > > Well that actually won't work, because the "succeeded = true;"
> will
> >> be
> >> > > > skipped if there is a "return;" in the inner try.
> >> > > >
> >> > > > Other than a custom Java compiler, I guess there's no clean way of
> >> > > > doing this in Java < 1.7. There's really only option 2 - with
> being
> >> > > > careful to always set succeeded correctly on all paths out of the
> >> try
> >> > > > block. Almost like releasing memory in C.
> >> > > >
> >> > >
> >> > > I haven't deeply followed this conversation, but would this be a
> >> > candidate
> >> > > for a [weaver] module?
> >> > >
> >> > > Matt
> >> > >
> >> > >
> >> >
> >> > [weaver] I am less sure about, but by playing with Objectweb's ASM I
> did
> >> > manage to compile code with try-with-resources on Java 7, change the
> >> class
> >> > file version, do some bytecode manipulation to delete calls to
> >> > Throwable.addSuppressed(), and get it to work on Java < 7.
> >> >
> >> > Made it into a Maven plugin and it works well :). Also verified other
> >> Java
> >> > 7 language features (binary literals, underscores in numeric literals,
> >> > strings in switch, diamonds, exception multi-catch and re-throw) work
> on
> >> > Java < 7 because they're just syntactic sugar.
> >> >
> >> > It's very small and simple - 143 lines in 1 file. Is it worth adding
> as
> >> a
> >> > new commons project? Or do we not host (projects that contain) Maven
> >> > plugins?
> >> >
> >> > Damjan
> >> >
> >>
> >
> >
> >
> > --
> > E-Mail: garydgregory@gmail.com | ggregory@apache.org
> > Java Persistence with Hibernate, Second Edition<
> http://www.manning.com/bauer3/>
> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > Spring Batch in Action <http://www.manning.com/templier/>
> > Blog: http://garygregory.wordpress.com
> > Home: http://garygregory.com/
> > Tweet! http://twitter.com/GaryGregory
> >
>

Re: Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

Posted by Matt Benson <gu...@gmail.com>.
I wasn't under the impression this was blocking imaging 1.0. In any case,
framing Damjan's work as a Weaver module would still use the same work he's
done with ASM and would really only utilize a standard mechanism for
triggering the process. It would also avoid proliferation of multiple maven
plugins with essentially the same mission: post-processing bytecode.

Matt
On Nov 3, 2013 10:08 AM, "Gary Gregory" <ga...@gmail.com> wrote:

> On Sun, Nov 3, 2013 at 10:56 AM, Matt Benson <gu...@gmail.com> wrote:
>
>> Sounds like it would be trivial to frame what you've done here into the
>> context of a Weaver module, for which there exist both a maven plugin and
>> an Antlib, as well as a need for a near-term release. :-)
>>
>
> and eating our own dog food :)
>
> We may pushing the limits of Dajman's patience for a 1.0 which is long
> overdue.
>
> I suppose D needs to weigh using ASM or [weaver] vs. adjusting the current
> code with Java 5.
>
> Gary
>
>>
>> Matt
>> On Nov 3, 2013 2:30 AM, "Damjan Jovanovic" <da...@apache.org> wrote:
>>
>> > On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com>
>> wrote:
>> >
>> > > On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <da...@gmail.com>
>> > wrote:
>> > > >
>> > > > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
>> > > > <Jo...@scalaris.com> wrote:
>> > > > > Hi Damjan,
>> > > > >
>> > > > > Damjan Jovanovic wrote:
>> > > > >
>> > > > > [snip]
>> > > > >
>> > > > > Thanks for explanation.
>> > > > >
>> > > > >> We would be able to adapt that for Java < 1.7 by swallowing the
>> > close
>> > > > >> exception instead of calling addSuppressed() on the primary
>> > exception,
>> > > > >> but the show stopper is catching and rethrowing the primary
>> > exception
>> > > > >> (Throwable), which javac < 1.7 refuses to compile because it
>> doesn't
>> > > > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
>> > > > >> (
>> > >
>> > >
>> >
>> http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
>> > > ).
>> > > > >>
>> > > > >> But this would work and always sets succeeded correctly without
>> > > > >> catch/re-throw:
>> > > > >>
>> > > > >> final InputStream is = factoryMethodThatCanThrow();
>> > > > >> boolean succeeded = false;
>> > > > >> try {
>> > > > >>     try {
>> > > > >>         is.methodThatCanThrow();
>> > > > >>     } finally {
>> > > > >>     }
>> > > > >>     succeeded = true;
>> > > > >> } finally {
>> > > > >>     closeSafely(!succeeded, is);
>> > > > >> }
>> > > > >
>> > > > > I guess the nested try was unintentionally ;-)
>> > > > >
>> > > > > Cheers,
>> > > > > Jörg
>> > > >
>> > > > Well that actually won't work, because the "succeeded = true;" will
>> be
>> > > > skipped if there is a "return;" in the inner try.
>> > > >
>> > > > Other than a custom Java compiler, I guess there's no clean way of
>> > > > doing this in Java < 1.7. There's really only option 2 - with being
>> > > > careful to always set succeeded correctly on all paths out of the
>> try
>> > > > block. Almost like releasing memory in C.
>> > > >
>> > >
>> > > I haven't deeply followed this conversation, but would this be a
>> > candidate
>> > > for a [weaver] module?
>> > >
>> > > Matt
>> > >
>> > >
>> >
>> > [weaver] I am less sure about, but by playing with Objectweb's ASM I did
>> > manage to compile code with try-with-resources on Java 7, change the
>> class
>> > file version, do some bytecode manipulation to delete calls to
>> > Throwable.addSuppressed(), and get it to work on Java < 7.
>> >
>> > Made it into a Maven plugin and it works well :). Also verified other
>> Java
>> > 7 language features (binary literals, underscores in numeric literals,
>> > strings in switch, diamonds, exception multi-catch and re-throw) work on
>> > Java < 7 because they're just syntactic sugar.
>> >
>> > It's very small and simple - 143 lines in 1 file. Is it worth adding as
>> a
>> > new commons project? Or do we not host (projects that contain) Maven
>> > plugins?
>> >
>> > Damjan
>> >
>>
>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>

Re: Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

Posted by Gary Gregory <ga...@gmail.com>.
On Sun, Nov 3, 2013 at 10:56 AM, Matt Benson <gu...@gmail.com> wrote:

> Sounds like it would be trivial to frame what you've done here into the
> context of a Weaver module, for which there exist both a maven plugin and
> an Antlib, as well as a need for a near-term release. :-)
>

and eating our own dog food :)

We may pushing the limits of Dajman's patience for a 1.0 which is long
overdue.

I suppose D needs to weigh using ASM or [weaver] vs. adjusting the current
code with Java 5.

Gary

>
> Matt
> On Nov 3, 2013 2:30 AM, "Damjan Jovanovic" <da...@apache.org> wrote:
>
> > On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com>
> wrote:
> >
> > > On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <da...@gmail.com>
> > wrote:
> > > >
> > > > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
> > > > <Jo...@scalaris.com> wrote:
> > > > > Hi Damjan,
> > > > >
> > > > > Damjan Jovanovic wrote:
> > > > >
> > > > > [snip]
> > > > >
> > > > > Thanks for explanation.
> > > > >
> > > > >> We would be able to adapt that for Java < 1.7 by swallowing the
> > close
> > > > >> exception instead of calling addSuppressed() on the primary
> > exception,
> > > > >> but the show stopper is catching and rethrowing the primary
> > exception
> > > > >> (Throwable), which javac < 1.7 refuses to compile because it
> doesn't
> > > > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
> > > > >> (
> > >
> > >
> >
> http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
> > > ).
> > > > >>
> > > > >> But this would work and always sets succeeded correctly without
> > > > >> catch/re-throw:
> > > > >>
> > > > >> final InputStream is = factoryMethodThatCanThrow();
> > > > >> boolean succeeded = false;
> > > > >> try {
> > > > >>     try {
> > > > >>         is.methodThatCanThrow();
> > > > >>     } finally {
> > > > >>     }
> > > > >>     succeeded = true;
> > > > >> } finally {
> > > > >>     closeSafely(!succeeded, is);
> > > > >> }
> > > > >
> > > > > I guess the nested try was unintentionally ;-)
> > > > >
> > > > > Cheers,
> > > > > Jörg
> > > >
> > > > Well that actually won't work, because the "succeeded = true;" will
> be
> > > > skipped if there is a "return;" in the inner try.
> > > >
> > > > Other than a custom Java compiler, I guess there's no clean way of
> > > > doing this in Java < 1.7. There's really only option 2 - with being
> > > > careful to always set succeeded correctly on all paths out of the try
> > > > block. Almost like releasing memory in C.
> > > >
> > >
> > > I haven't deeply followed this conversation, but would this be a
> > candidate
> > > for a [weaver] module?
> > >
> > > Matt
> > >
> > >
> >
> > [weaver] I am less sure about, but by playing with Objectweb's ASM I did
> > manage to compile code with try-with-resources on Java 7, change the
> class
> > file version, do some bytecode manipulation to delete calls to
> > Throwable.addSuppressed(), and get it to work on Java < 7.
> >
> > Made it into a Maven plugin and it works well :). Also verified other
> Java
> > 7 language features (binary literals, underscores in numeric literals,
> > strings in switch, diamonds, exception multi-catch and re-throw) work on
> > Java < 7 because they're just syntactic sugar.
> >
> > It's very small and simple - 143 lines in 1 file. Is it worth adding as a
> > new commons project? Or do we not host (projects that contain) Maven
> > plugins?
> >
> > Damjan
> >
>



-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Backporting try-with-resources to Java < 7 (was: Re: [imaging] Closing stream)

Posted by Matt Benson <gu...@gmail.com>.
Sounds like it would be trivial to frame what you've done here into the
context of a Weaver module, for which there exist both a maven plugin and
an Antlib, as well as a need for a near-term release. :-)

Matt
On Nov 3, 2013 2:30 AM, "Damjan Jovanovic" <da...@apache.org> wrote:

> On Fri, Oct 25, 2013 at 1:52 PM, Matt Benson <gu...@gmail.com> wrote:
>
> > On Oct 25, 2013 6:30 AM, "Damjan Jovanovic" <da...@gmail.com>
> wrote:
> > >
> > > On Fri, Oct 25, 2013 at 12:36 PM, Jörg Schaible
> > > <Jo...@scalaris.com> wrote:
> > > > Hi Damjan,
> > > >
> > > > Damjan Jovanovic wrote:
> > > >
> > > > [snip]
> > > >
> > > > Thanks for explanation.
> > > >
> > > >> We would be able to adapt that for Java < 1.7 by swallowing the
> close
> > > >> exception instead of calling addSuppressed() on the primary
> exception,
> > > >> but the show stopper is catching and rethrowing the primary
> exception
> > > >> (Throwable), which javac < 1.7 refuses to compile because it doesn't
> > > >> do "Rethrowing Exceptions with More Inclusive Type Checking"
> > > >> (
> >
> >
> http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html
> > ).
> > > >>
> > > >> But this would work and always sets succeeded correctly without
> > > >> catch/re-throw:
> > > >>
> > > >> final InputStream is = factoryMethodThatCanThrow();
> > > >> boolean succeeded = false;
> > > >> try {
> > > >>     try {
> > > >>         is.methodThatCanThrow();
> > > >>     } finally {
> > > >>     }
> > > >>     succeeded = true;
> > > >> } finally {
> > > >>     closeSafely(!succeeded, is);
> > > >> }
> > > >
> > > > I guess the nested try was unintentionally ;-)
> > > >
> > > > Cheers,
> > > > Jörg
> > >
> > > Well that actually won't work, because the "succeeded = true;" will be
> > > skipped if there is a "return;" in the inner try.
> > >
> > > Other than a custom Java compiler, I guess there's no clean way of
> > > doing this in Java < 1.7. There's really only option 2 - with being
> > > careful to always set succeeded correctly on all paths out of the try
> > > block. Almost like releasing memory in C.
> > >
> >
> > I haven't deeply followed this conversation, but would this be a
> candidate
> > for a [weaver] module?
> >
> > Matt
> >
> >
>
> [weaver] I am less sure about, but by playing with Objectweb's ASM I did
> manage to compile code with try-with-resources on Java 7, change the class
> file version, do some bytecode manipulation to delete calls to
> Throwable.addSuppressed(), and get it to work on Java < 7.
>
> Made it into a Maven plugin and it works well :). Also verified other Java
> 7 language features (binary literals, underscores in numeric literals,
> strings in switch, diamonds, exception multi-catch and re-throw) work on
> Java < 7 because they're just syntactic sugar.
>
> It's very small and simple - 143 lines in 1 file. Is it worth adding as a
> new commons project? Or do we not host (projects that contain) Maven
> plugins?
>
> Damjan
>