You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The java2python package can translate any syntactically valid Java source
4
-
code file. The generated Python code is not guaranteed to run, nor is
5
-
guaranteed to be syntactically valid Python. However, java2python works
6
-
well many cases, and in some of those, it creates perfectly usable and
7
-
workable Python code.
3
+
The java2python package can translate any syntactically valid Java source code
4
+
file. The generated Python code is not guaranteed to run, nor is guaranteed to
5
+
be syntactically valid Python. However, java2python works well many cases, and
6
+
in some of those, it creates perfectly usable and workable Python code.
8
7
9
8
10
-
## General Approach
9
+
###General Approach
11
10
12
11
The approach taken by java2python is to favor readability over correctness.
13
12
14
-
## Identifiers and Qualified Identifiers
13
+
###Identifiers and Qualified Identifiers
15
14
16
-
java2python copies identifiers from source to target, modifying the value only when:
15
+
java2python copies identifiers from source to target, modifying the value only
16
+
when:
17
17
18
18
* the identifier conflicts with a Python keyword or builtin, or
19
19
* the identifier has an explicit lexical transformation
20
20
21
21
22
-
## Literals: Integer, Floating Point, Character, String, Boolean and Null
22
+
###Literals: Integer, Floating Point, Character, String, Boolean and Null
23
23
24
24
Literals are copied from source to target with the following modifications:
25
25
@@ -32,34 +32,41 @@ Literals are copied from source to target with the following modifications:
32
32
Transformation of literal values happens at the AST level; see the
33
33
`astTransforms` configuration value for details.
34
34
35
-
## Expressions
35
+
###Expressions
36
36
37
-
### Constant Expression
37
+
####Constant Expression
38
38
39
39
Constant expressions are translated to their Python equivalents.
40
40
41
-
### Ternary Expressions
41
+
####Ternary Expressions
42
42
43
-
Ternary expressions are translated to their Python form (`val if condition else other`)
43
+
Ternary expressions are translated to their Python form (`val if condition else
44
+
other`)
44
45
45
46
46
-
## Prefix Operators
47
+
###Prefix Operators
47
48
48
49
All of the Java prefix operators are supported:
49
50
50
51
++ -- ! ~ + -
51
52
52
-
In the case of `++` and `--`, java2python translates to `+= 1` and `-= 1`. If necessary, those expressions are moved outside of statements.
53
+
In the case of `++` and `--`, java2python translates to `+= 1` and `-= 1`. If
54
+
necessary, those expressions are moved outside of statements.
53
55
54
-
## Assignment Operators
56
+
###Assignment Operators
55
57
56
-
All of the following assignment operators are translated into their Python equivalents:
58
+
All of the following assignment operators are translated into their Python
59
+
equivalents:
57
60
58
61
= += -= *= /= &= |= ^= %= <<= >>=
59
62
60
-
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.
63
+
The bit shift right (`>>>`)and bit shift assign right (`>>>=`) operators are
64
+
mapped to a function; if java2python detects code that uses either of these, it
65
+
replaces the operator with that function and includes the function within the
66
+
output. This behavior is controlled by the `modulePrologueHandlers` config
67
+
handler.
61
68
62
-
## Infix Operators
69
+
###Infix Operators
63
70
64
71
The following operators are translated to their Python equivalents:
65
72
@@ -68,7 +75,7 @@ The following operators are translated to their Python equivalents:
68
75
69
76
Refer to the note above regarding bit shift right.
70
77
71
-
## Basic Types
78
+
###Basic Types
72
79
73
80
The basic Java types are mapped to Python types as follows:
74
81
@@ -77,125 +84,161 @@ The basic Java types are mapped to Python types as follows:
77
84
*`float` and `double` become `float`
78
85
*`boolean` becomes `bool`
79
86
80
-
### Arrays
87
+
####Arrays
81
88
82
-
Java arrays and array access expressions are translated to their Python equivalents.
89
+
Java arrays and array access expressions are translated to their Python
90
+
equivalents.
83
91
84
92
85
-
## Types, Interfaces, Enums
93
+
###Types, Interfaces, Enums
86
94
87
95
Java classes, interfaces, and enums are translated into Python classes.
88
96
89
97
In the case of interfaces, the strategy is configurable. By default,
90
-
interfaces are translated to classes utilizing the `ABCMeta` class. The package
91
-
includes config handlers that can translate to simple classes (inheriting from
92
-
`object`), or from Zope Interfaces. Interface base types are controlled via the `interfaceBaseHandlers` config item. The `interfaceHeadHandlers` config item controls the
93
-
metaclass.
98
+
interfaces are translated to classes utilizing the `ABCMeta` class. The
99
+
package includes config handlers that can translate to simple classes
100
+
(inheriting from `object`), or from Zope Interfaces. Interface base types are
101
+
controlled via the `interfaceBaseHandlers` config item. The
102
+
`interfaceHeadHandlers` config item controls the metaclass.
94
103
95
104
Enums are also translated via a configurable strategy. By default, enumerated
96
105
values are created as class attributes with string values. The package
97
-
includes a config handler to create class attributes with integer values. The config handler
98
-
that controls enumeration value construction is `enumValueHandler`.
106
+
includes a config handler to create class attributes with integer values. The
107
+
config handler that controls enumeration value construction is
108
+
`enumValueHandler`.
99
109
100
110
101
-
## Statements
111
+
###Statements
102
112
103
-
### assert
104
-
Java `assert` statements are translated to equivalent Python `assert` statements.
113
+
#### assert
114
+
115
+
Java `assert` statements are translated to equivalent Python `assert`
116
+
statements.
117
+
118
+
#### if
105
119
106
-
### if
107
120
Java `if` statements are translated to equivalent Python `if` statements.
108
121
109
122
110
-
### for
123
+
#### for
124
+
111
125
Java `for` statements are translated to equivalent Python `for` statements.
112
126
113
-
### while and do
114
-
Java `while` and `do` statements are translated to equivalent Python `while` statements.
127
+
#### while and do
128
+
129
+
Java `while` and `do` statements are translated to equivalent Python `while`
130
+
statements.
131
+
132
+
#### try and catch
115
133
116
-
### try and catch
117
-
Java `try` and `catch` statements are translated to equivalent Python `try` and `except` statements.
134
+
Java `try` and `catch` statements are translated to equivalent Python `try` and
135
+
`except` statements.
118
136
119
-
### switch and case
120
-
Java `switch` and `case` statements are translated to equivalent Python `if` statements.
137
+
#### switch and case
121
138
122
-
### synchronized
139
+
Java `switch` and `case` statements are translated to equivalent Python `if`
140
+
statements.
123
141
124
-
In the case of a `synchronized` method or static method, the compiler will include a decorator, `@synchronized` in
125
-
the method or static method preamble. In the case of a `synchronized` block, the compiler will translate to this form:
142
+
#### synchronized
143
+
144
+
In the case of a `synchronized` method or static method, the compiler will
145
+
include a decorator, `@synchronized` in the method or static method preamble.
146
+
In the case of a `synchronized` block, the compiler will translate to this
147
+
form:
126
148
127
149
with lock_for_object(expr):
128
150
...
129
151
130
-
The `lock_for_object` callable is the default and can be controlled via the `methodLockFunctionName` config item.
131
-
Also of note, the default `modulePrologueHandlers` uses a generator named `maybeSyncHelpers` to include
152
+
The `lock_for_object` callable is the default and can be controlled via the
153
+
`methodLockFunctionName` config item. Also of note, the default
154
+
`modulePrologueHandlers` uses a generator named `maybeSyncHelpers` to include
132
155
Python helper code for synchronization.
133
156
134
-
### return
135
-
Java `return` statements are translated to equivalent Python `return` statements.
157
+
#### return
158
+
159
+
Java `return` statements are translated to equivalent Python `return`
160
+
statements.
161
+
136
162
163
+
#### throw
137
164
138
-
### throw
139
165
Java `throw` statements are translated to equivalent Python `throw` statements.
140
166
141
-
### break
142
-
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
143
-
contain the original label.
167
+
#### break
168
+
169
+
Java `break` statements are translated to equivalent Python `break` statements.
170
+
However, a Java `break` statement with an identifier (e.g., `break FOO`) is not
171
+
supported. If the compiler detects such a statement, a warning will be printed
172
+
and the translated source will not contain the original label.
173
+
174
+
#### continue
144
175
145
-
### continue
146
-
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
147
-
contain the original label.
176
+
Java `continue` statements are translated to equivalent Python `continue`
177
+
statements. However, a Java `continue` statement with an identifier (e.g.,
178
+
`continue FOO`) is not supported. If the compiler detects such a statement, a
179
+
warning will be printed and the translated source will not contain the original
180
+
label.
148
181
149
182
150
-
## Other Keywords
183
+
###Other Keywords
151
184
152
-
### this
185
+
####this
153
186
154
187
The `this` Java keyword is translated to the Python pseudo keyword `self`.
155
188
156
-
### instanceof
189
+
####instanceof
157
190
158
-
The `instance of` Java keyword is translated to the `isinstance(…)` Python function call.
191
+
The `instance of` Java keyword is translated to the `isinstance(…)` Python
192
+
function call.
159
193
160
-
### super
194
+
####super
161
195
162
196
The Java keyword `super` is translated to the `super(…)` Python function call.
163
197
164
-
### .class
198
+
####.class
165
199
166
-
The compiler translates Java `.class` expressions to `.__class__` attribute references.
200
+
The compiler translates Java `.class` expressions to `.__class__` attribute
201
+
references.
167
202
168
-
### void
203
+
####void
169
204
170
-
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__`.
205
+
The Java keyword `void` is typically discarded by the compiler. In the case of
206
+
the `void.class` form, the compiler translates the expression to
207
+
`None.__class__`.
171
208
172
209
173
-
## Annotations
210
+
###Annotations
174
211
175
-
Annotations are typically dropped by the compiler. The following Java annotations have little or no meaning in Python, and are discarded:
212
+
Annotations are typically dropped by the compiler. The following Java
213
+
annotations have little or no meaning in Python, and are discarded:
176
214
177
215
public protected private abstract final native transient
178
216
volatile strictfp
179
217
180
-
### static
218
+
####static
181
219
182
-
The `static` method keyword is translated to a `@classmethod` decorator for the corresponding method.
220
+
The `static` annotation is translated to a `@classmethod` decorator for the
221
+
corresponding method.
183
222
184
-
### synchronized
223
+
####synchronized
185
224
186
-
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.
225
+
When used as a method or static method annotation, the `synchronized` keyword
226
+
is translated to a `@synchronized` method decorator. This behavior is
227
+
controllable via the `methodPrologueHandlers` config item.
187
228
188
229
See the note above regarding the use of `synchronized` within blocks.
189
230
190
-
## Comments
231
+
###Comments
191
232
192
-
Both Java end-of-line comments and multi-line comments are translated to Python comments. The comment prefix is `# ` by default, and is controllable via the `commentPrefix` config item.
233
+
Both Java end-of-line comments and multi-line comments are translated to Python
234
+
comments. The comment prefix is `# ` (hash plus space) by default, and is
235
+
controllable via the `commentPrefix` config item.
193
236
194
-
### JavaDoc
237
+
####JavaDoc
195
238
196
239
JavaDoc comments are preserved as Python comments.
197
240
198
241
199
-
## References
242
+
###References
200
243
201
244
* Java language specification: http://java.sun.com/docs/books/jls/third_edition/html/syntax.html
0 commit comments