views:

1158

answers:

7

Is there a tool that, given a Java 5 level source code, will backport it to Java 1.4-compliant source code, by removing Generics declarations, transforming for eachs in simple fors or iteration fors, etc.?

Please note that I am looking for a tool that translates source code to source code, not class binaries.

+3  A: 

You are looking for Retroweaver, it sounds like.

Ascalonian
I am looking for _source code_ translation. Will Retroweaver do that? For what I glimpsed, it doesn't look like it handle that. Am I wrong?
kolrie
+2  A: 

Similar question: http://stackoverflow.com/questions/547872/converting-java-1-5-source-into-1-1-source/547923#547923

Essentially run retroweaver over your code and then run that through a decompiler... not sure of any source-to-source translators.

TofuBeer
Decompilers (jad for instance) can make a mess out of threaded code, especially the synchronized blocks. I think fixing those errors may be harder than fixing the sources themselves.
extraneon
Yes, if I need to choose between doing it manually and decompile binaries, I have no doubt I would go with the first option.
kolrie
+6  A: 

The previous post on a similar subject works on class files. I have not heard of a tool which does the same for source files.

You can, however, develop and compile in Java 5 and deploy on a Java 4 runtime with retroweavor or retrotranslator.

If you must convert java 5 to 4, the easiest way to do it, as far as I see, is by setting your IDE java version to 1.4 and fix all errors.

In eclipse, for instance, you can set a per-project java version by right-clicking on the project, select Properties, select Java Compiler and set the Compiler compliance level. It may be smart to use the Java 1.4 JRE system library instead of the 1.5 JRE (Java Build Path, Libraries).

extraneon
+1 manual way is probably correct. Possibly with a sed-preprocessing step to strip out all generic declarations (or better yet: convert them to comments).
Joachim Sauer
I would go with manual myself.
TofuBeer
I wish I had Eclipse installed to test this, but do the generic-inference refactoring tools provide a capability to "go backwards" and replace generics and add casts to accesses? It seems like they have all the static analysis info available to be able to do that..
Matt J
Netbeans can also do this.
Malfist
+1. Go manual, automating specific steps as you can. Other solutions will force you to take on large amounts of technical debt.
Jared
+4  A: 

Declawer should do what you want. There's some brief info about it here, but basically what it does is run through a directory of Java 1.5 source and output Java 1.4 equivalent source. It doesn't support all 1.5 features. For example, the enhanced for loop and auto(un)boxing aren't there. It will strip out generics though, and that should get you a substantial portion of the way there. The source it generates is a little funky, but not too bad. It's built on top of javac, it basically just tells it to spit out the AST as Java source instead of bytecode.

Hope that helps.

Matt J
Does that work for backporting Java 1.6 code back to Java 1.5?
DyreSchlock
In general no, but this also isn't as big of an issue because 1.6 didn't add a lot of new language features like 1.5 did. You can always generate bytecode from your 1.6 source code that will run on a 1.4 or 1.5 JVM, you just can't use Declawer to transform your 1.6 source code into source code that only uses 1.5 language and API features that is still readable.
Matt J
+1  A: 

We have been using retrotranslator in our project and it has been working absolutley fine. You can find it here Retrotranslator @ Sourceforge

Vinnie
+1  A: 

RetroJ does the waving at Source Level.. it is commercial though

Raja Nagendra Kumar
+1  A: 

We have written our own tool "Decaf" to port back db4o from current Java dialects all the way back to JDK 1.1.

Decaf is based on the Eclipse compiler.

It can deal with constructs like generics, for loops and varargs.

Sources for Decaf can be found in our SVN repository: http://source.db4o.com/db4o/trunk/decaf/

Carl Rosenberger