Version 6 (modified by 17 months ago) ( diff ) | ,
---|
Java to Python (j2p)
This tool can translate java programs into python programs. This is a complex mechanism and still somewhat experimental. It can translate both single file java programs as multi-file projects.
There are a number of packages inside this project, because this translator has a modular setup:
package name= artifactid | description | current state |
---|---|---|
core | translation of java programs + library containing translators for a few basic java classes | general translation pretty complete; library translators very partial |
jackson-t | library with translators for jackson->pyson translation | quite complete |
tudutils-t | library with translators for tudelft utilities package. | very partial |
These modules offer translation support for external java libraries. For instance jackson is an external library to translate json to java; the jackson-t library translates jackson-based code and annotations into pyson-based python code.
You can easily extend the standard core translator with these plugin translators by just importing these plugins in your java project. Just make sure that they are all based on the same core version.
Internal mechanism
There are two main components in the core:
- The translator that parses java code and creates equivalent python code
- per-java-class translators that know how to translate a specific call to a java class function or field into equivalent python code.
The first is generic and used for all java programs. It is currently pretty complete but details will be filled in over time as the need arises to support more java syntax.
The second is currently very partial. The reason is that there are a huge number of java classes, and every field and function in it will need a specialized translator. This will grow slowly over time as needed.
Usage
Comments can contain python code if the block starts with {@link #CODE_IN_COMMENT_TXT}. This code replaces the entire object (if/case/while block; statement) that follows the comment.
Python has strict requirements regarding indentation. To make this possible, we need to be strict about indentation as well. In a single line python comment, the code must look exactly like
//#PY single_python_line
Note the single whitespace after the #PY. Your code starts after this single whitespace.
In a multi line comment the code must look exactly like
/*#PY * codeline1 * codeline2 * ... */
Your code lines each start with "* ", note the whitespace after the star. You are free to indent before the "*".
Your code is automatically indented to the level needed at the insertion
place in the coed.
Code MUST be placed in either a standard block comment or a single line comment. Starting a javadoc with the {@link #CODE_IN_COMMENT_TXT} is not allowed. This is to encourage proper use of javadoc.
A comment block overrides also annotations.
If the code block contains no code at all, it is translated as
pass
, to ensure that the code is a proper statement.