The approach taken by java2python is to favor readability over correctness.
java2python copies identifiers from source to target, modifying the value only when:
- the identifier conflicts with a Python keyword or builtin, or
- the identifier has an explicit lexical transformation
Literals are copied from source to target with the following modifications:
nullis changed toNonefalseis changed toFalsetrueis changed toTrue- if necessary, floating point literals are changed to valid Python values
- string and character literals are changed to Python strings
Transformation of literal values happens at the AST level; see the
astTransforms configuration value for details.
Constant expressions are translated to their Python equivalents.
Ternary expressions are translated to their Python form (val if condition else other)
All of the Java prefix operators are supported:
++ -- ! ~ + -
In the case of ++ and --, java2python translates to += 1 and -= 1. If
necessary, those expressions are moved outside of statements.
All of the following assignment operators are translated into their Python equivalents:
= += -= *= /= &= |= ^= %= <<= >>=
The bit shift right (>>>)and bit shift assign right (>>>=) operators are
mapped to a function; if java2python detects code that uses either of these, it
replaces the operator with that function and includes the function within the
output. This behavior is controlled by the modulePrologueHandlers config
handler.
The following operators are translated to their Python equivalents:
|| && | ^ & == != < >
<= >= << >> >>> + - * / %
Refer to the note above regarding bit shift right.
The basic Java types are mapped to Python types as follows:
byte,short,int, andlongbecomeintcharbecomesstrfloatanddoublebecomefloatbooleanbecomesbool
Java arrays and array access expressions are translated to their Python equivalents.
Java classes, interfaces, and enums are translated into Python classes.
In the case of interfaces, the strategy is configurable. By default,
interfaces are translated to classes utilizing the ABCMeta class. The
package includes config handlers that can translate to simple classes
(inheriting from object), or from Zope Interfaces. Interface base types are
controlled via the interfaceBaseHandlers config item. The
interfaceHeadHandlers config item controls the metaclass.
Enums are also translated via a configurable strategy. By default, enumerated
values are created as class attributes with string values. The package
includes a config handler to create class attributes with integer values. The
config handler that controls enumeration value construction is
enumValueHandler.
Java assert statements are translated to equivalent Python assert
statements.
Java if statements are translated to equivalent Python if statements.
The processing import statements is delegated to the moduleImportDeclarationHandler.
Java for statements are translated to equivalent Python for statements.
Java while and do statements are translated to equivalent Python while
statements.
Java try and catch statements are translated to equivalent Python try and
except statements.
Java switch and case statements are translated to equivalent Python if
statements.
In the case of a synchronized method or static method, the compiler will
include a decorator, @synchronized in the method or static method preamble.
In the case of a synchronized block, the compiler will translate to this
form:
with lock_for_object(expr):
...
The lock_for_object callable is the default and can be controlled via the
methodLockFunctionName config item. Also of note, the default
modulePrologueHandlers uses a generator named maybeSyncHelpers to include
Python helper code for synchronization.
Java return statements are translated to equivalent Python return
statements.
Java throw statements are translated to equivalent Python raise statements.
Java break statements are translated to equivalent Python break statements.
However, a Java break statement with an identifier (e.g., break FOO) is not
supported. If the compiler detects such a statement, a warning will be printed
and the translated source will not contain the original label.
Java continue statements are translated to equivalent Python continue
statements. However, a Java continue statement with an identifier (e.g.,
continue FOO) is not supported. If the compiler detects such a statement, a
warning will be printed and the translated source will not contain the original
label.
The this Java keyword is translated to the Python pseudo keyword self.
The instanceof Java keyword is translated to the isinstance(…) Python
function call.
The Java keyword super is translated to the super(…) Python function call.
The compiler translates Java .class expressions to .__class__ attribute
references.
The Java keyword void is typically discarded by the compiler. In the case of
the void.class form, the compiler translates the expression to
None.__class__.
Annotations are typically dropped by the compiler. The following Java annotations have little or no meaning in Python and are discarded:
public protected private abstract final native transient
volatile strictfp
The static annotation is translated to a @classmethod decorator for the
corresponding method.
When used as a method or static method annotation, the synchronized keyword
is translated to a @synchronized method decorator. This behavior is
controllable via the methodPrologueHandlers config item.
See the note above regarding the use of synchronized within blocks.
Both Java end-of-line comments and multi-line comments are translated to Python
comments. The comment prefix is # (hash plus space) by default, and is
controllable via the commentPrefix config item.
JavaDoc comments are preserved as Python comments.
Java language specification: http://java.sun.com/docs/books/jls/third_edition/html/syntax.html