JDK6 backwards compatible AutoCloseable from a librariesperspective

Rémi Forax forax at univ-mlv.fr
Mon Feb 7 06:32:17 PST 2011

  Le 07/02/2011 15:08, Michael Bien a écrit :
>    On 02/07/2011 02:40 PM, Rémi Forax wrote:
>>     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
>> jocl.lang.AutoClosable,
>> 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.
>> Rémi
>> [1] http://asm.ow2.org/
> Thank you Remi,
> do i really have to compile with 1.7 language level even if i don't use
> ARM internally?

yes, you have to.
source of the compiler near line 165:

public boolean allowTryWithResources() {
      return compareTo(JDK1_7)>= 0;

> -michael


More information about the coin-dev mailing list