Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating Java bytecode into other representations and programming languages

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.

like image 474
Tim Bunce Avatar asked Mar 12 '09 12:03

Tim Bunce


2 Answers

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.

like image 181
Alex Miller Avatar answered Sep 23 '22 08:09

Alex Miller


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.

like image 36
Scott Wisniewski Avatar answered Sep 24 '22 08:09

Scott Wisniewski