views:

517

answers:

4

I am trying to convert java 1.5 source code into equivalent 1.1 source. My strategy so far has been to try to cross-compile 1.5 source into 1.1 byte code, and then decompile the 1.1 byte into 1.1 source code.

I see that there are issues with cross-compiling using the -target option to javac. Can anyone explain the hang-up here? Is there a better way to accomplish what I am trying to do?

+2  A: 

The problem with this is that the Java 1.5 JRE contains additions to the libraries not found in the Java 1.1 JRE. So, even if you were somehow about to decompile the bytecode back into Java source code, the Java 1.1. machine in question still wouldn't be able to run it.

Additionally, when you decompile the byte code, it's not going to be as readable as it originally was because you lose the names of local variables and parameters.

I don't really see a completely automated of doing this. Depending on the size of the project, it might be better to set your IDE to use source version 1.1, then manually fix all of the errors you come across. Some stuff, like replacing foreach loops with regular for loops, is going to be pretty easy and your IDE might be able to refactor automatically. Other things, like replacing Java 1.5 library calls to their 1.1 counterparts, will require some thinking on your part.

Outlaw Programmer
W.r.t. libraries it depends, there may be extensions that give you most of the collections in 1.4 (I've had such a case).You don't loose names if you compile with debug support. The output of Jad is quite nice, if you don't use inner classes and such.
starblue
I am using Jad now to go from 1.5 back to 1.5, but have generics stripped away.
dim fish
+5  A: 

Without a bit more context I don't know what kind of answers you will get.

Why can you not just compile the source with the 1.1 compiler? Some things, say java.util.List will not exist in 1.1 so being able to compile the code to an older version won't help you much.

If you are talking about language features such as generics, the enhanced for loop. etc... then those are just syntactic sugar for the most part and going back to 1.1 would be doable.

There is a tool called retroweaver that will go back to 1.4 from 1.5 source.

Do you have some examples of the code (classes or language constructs) that won't compile with the 1.1 compiler?

TofuBeer
I am working with a home-brew Java compiler which uses 1.4 grammar but does not support all language features, even simple things like the switch statement, because I would have to add support myself. I want to automatically simplify a program to make it easier to convert it for my compiler.
dim fish
You might want to look at Antlr for doing a conversion tool.
TofuBeer
+2  A: 

The list of minor differences and incompatibilities grows longer and longer the further back you go, they are often real edge cases but they might mount up.

If you are running on a 1.1 circa VM/runtime (the only reason I can think of to stick to 1.1) then note that it almost certainly has no patches or security fixes for well known security flaws fixed since then.

ShuggyCoUk
This is no problem for me. I want to make it easier to take open source Java programs and convert them so they will compile in my simple home-brew compiler. Essentially I need benchmarks for my experimental compiler analyses.
dim fish
ah cool - it might be worth including this rationale in your question
ShuggyCoUk
A: 

Supporting older versions of Java isn't as useful as it used to be. Java 1.3 or 1.4 maybe, but anything older isn't used much any more, nor should it be.

One way you can achieve this is to use the ASM library to read a class and build the same class as any version you wish. even 1.0. However it won't stop you using features which were not available in that version. You would have to test the code very thoughly to be sure it really works on a Java 1.1 JVM.

Peter Lawrey