JDK6 backwards compatible AutoCloseable from a libraries perspective

Rémi Forax forax at univ-mlv.fr
Mon Feb 7 05:40:46 PST 2011

  Le 07/02/2011 14:10, Michael Bien a écrit :
>    Hello everyone,
> I would like to support the new try-with-resource feature in jocl while
> staying backwards compatible with JDK5/6. The problem is that
> AutoCloseable resides in java.lang which means i have no chance to load
> the class if i would ship it for jdk5 backwards compatibility.
> Providing two builds for JOCL is not what i would like to do just to be
> able to be upwards compatible.
> any ideas what i could do in this situation?
> best regards,
> michael

You need to:
Create a jocl internal equivalent to java.lang.AutoClosable, let say 
this class should extends java.lang.AutoClosable.
modify you managed classes to implements  jocl.lang.AutoClosable.
compile with javac -source 7 -target 7
Now you have a 1.7 compatible version.

The trick is to downgrade the classfiles to have a 1.5 compatible version.
For this use a bytecode rewriter tools like ASM [1] to change a just 
some bits
in the classfiles.
   modify the classfile of jocl.lang.AutoClosable to don't inherits from 
java.lang.AutoClosable anymore.
   downgrade the classfiles major version number from 51 (V1_7) to 49 (V1_5)

In fact, there is a way to avoid to create jocl.lang.AutoClosable, you can
analyze the bytecode to detect call to AutoClosable.close() and replace it
by the type of the variable infered using the same algorithm as the 
verifier does.
ASM package org.objectweb.asm.analysis already provides this analysis.


[1] http://asm.ow2.org/

More information about the coin-dev mailing list