views:

876

answers:

5

I'm looking for ways/tools/projects to translate Java bytecode into other programming languages or, failing that, at least into a structured representation (like XML). Ideally open source, naturally.

I've looked at ASM, the "bytecode manipulation and analysis framework". It doesn't support translation to other representations, but looks like a good foundation for such a project. Sadly, none of the projects listed on their users page comes close.

+1  A: 

Looks like you are looking for a cross-compiler. I know of the project xmlvm but have never used it. Maybe this suits your needs.

+1  A: 

You could try gcj to convert byte-code into native code.

If you want to convert into languages like python/groovy/ruby perhaps considering writing in those languages directly on the JVM (I realize this isn't exactly what you're looking for).

Perhaps scavenge some examples from something like this Eclipse Byte Code outline viewer.

Lastly, converting from one low-level language (byte-code) to another is gonna be pretty tricky and correctness will be an issue.

basszero
+5  A: 

ASM has the tree api which can basically give you the full structure of the bytecode. Seems like it's pretty easy to use that or even the visitor api to print that out in XML or some other format. Not sure what use that is though.

Translating back into Java is a decompiler's job and ones like Jad do ok. But that's hard because a) there is information lost during the source to bytecode translation and b) there is ambiguity in that multiple source can yield the same byte code.

If you actually want to go from bytecode to another high level language directly, that's going to be tough to do both comprehensively and meaningfully, for all the same reasons decompiling is hard, except even worse into another language.

If you want to go from Java source to another language's source, this has been done before, as in this Java-to-Python converter. That's somewhat easier as you can convert Java to an AST with something like Antlr, or the built-in Java compiler tools, Project Jackpot, etc. Even then, I presume you won't get very idiomatic code in the target language.

Alex Miller
A: 

I am pretty sure that the answer to this question depends strongly on what you need this for. Please elaborate.

Thorbjørn Ravn Andersen
+1  A: 

Try BCEL (byte code engineering library). You can implement a Visitor interface, which you can use to generate what ever you want (source code in another language, a custom AST , xml, what ever).

I used it for a project in college where I (mostly) modified a program analysis framework that worked over Java source code into one that could also work against Java byte code. That task involved generating nodes for the analysis framework's internal AST representation.

Also,

I briefly skimmed the documentation for the "ASM" project you mentioned. It looks like it defines several vistior classes. You should be able to use those to do what you want as well.

If you are not familiar with the visitor pattern, check out the description here. Also, if you haven't read it yet, the "Gang of Four" book is a good read.

Scott Wisniewski