1- .. _ intro:
1+ ## Introduction
22
3- ************
4- Introduction
5- ************
3+ ### What it Does
64
7- What it Does
8- ============
5+ java2python reads the Java source files you give it and produces somewhat
6+ roughly equivalent Python source code. It tries to make the same decisions
7+ you would if you were porting the code manually. It can perform the
8+ translation faster and more accurately than you could (usually).
99
10- |j2py| reads the Java source files you give it and produces
11- somewhat-roughly-equivalent Python source code. It tries to make the
12- same decisions you would if you were porting the code manually. It
13- can perform the translation faster and more accurately than you could,
14- because it's a dumb machine that does what its told and you're a smart
15- person with lots of books you haven't read and a love of chocolate so
16- sometimes you're easily distracted and make mistakes. Like me and
17- this documentation.
10+ ### Where It's Useful
1811
19- Where It's Useful
20- =================
21-
22- |j2py| can help in two situations. First, if you're doing a one-time
12+ java2python can help in two situations. First, if you're doing a one-time
2313port of a Java project to Python, it can save you a lot of time and
2414effort by getting you really far really fast.
2515
2616Second, if you've got a Java project and you'd like to generate a
27- Python port and keep the port up to date, you'll find that |j2py| can
17+ Python port and keep the port up to date, you'll find that java2python can
2818help tremendously. The per-project and per-file configuration system
2919helps out a lot in this area.
3020
31- Where |j2py| is not useful is also important. It won't be useful to
32- you if you expect your newly translated Python code to run correctly
33- the first time. The platforms are too different and this tool is too
34- limited for that to happen. Also, you won't find |j2py| very useful
35- if you expect to convert Java sources at runtime, but that's really a
36- special case of the former.
21+ ### Where It's Not
3722
23+ Where java2python is not useful is also important. It won't be useful to you
24+ if you expect your newly translated Python code to run correctly the first
25+ time. The platforms are too different and this tool is too limited for that
26+ to happen. Also, you won't find java2python very useful if you expect to
27+ convert Java sources at runtime. I suppose you could try, but I wouldn't.
3828
39- How it Works
40- =============
29+ ### How it Works
4130
42- |j2py| first converts the source code you give it into an abstract
43- syntax tree. (That's a lie, really. |j2py| doesn't do this step,
44- ANTLR does this step, and ANTLR is a whole lot bigger and cooler than
45- |j2py| could ever be. Obviously, really smart people worked on ANTLR
46- and only one fairly dim one worked on |j2py| ).
31+ java2python first converts the source code you give it into an abstract syntax
32+ tree. (That's a lie, really. java2python doesn't do this step,
33+ [ ANTLR] ( www.antlr.org ) does this step, and ANTLR is a whole lot bigger and
34+ cooler than java2python could ever be. Obviously, really smart people worked
35+ on ANTLR and only one fairly dim one worked on java2python ).
4736
4837After the syntax tree is constructed, it's walked and its nodes are
4938converted to their Python equivalents. When the walking is complete,
50- |j2py| takes a few more swipes at it and prints it out. It's all very
51- boring, like geology or watching someone learn to play the flute .
39+ java2python takes a few more swipes at it and prints it out. It's all very
40+ boring, like geology or watching someone learn to play the xylophone .
5241
5342This is all well and good for most cases where there exists a very
5443similar Python construct for the given Java construct. Classes, for
@@ -62,45 +51,50 @@ saying that it can't. But what I am saying is that there are chunks
6251of Java source code that you can't make into nice and neat and obvious
6352Python equivalents.
6453
65- To get around these trouble spots, |j2py| takes one of two approaches
66- (and sometimes both if she's feeling especially feisty or if you
67- haven't paid her much attention lately). The first approach is to try
68- and make the problem go away. For example, in Java the ` if ` statement
69- can contain an assignment expression::
54+ To get around these trouble spots, java2python takes the approach of trying
55+ make the problem go away. For example, in Java the ` if ` statement can contain
56+ an assignment expression:
7057
71- if (++x == 0) { ... }
58+ ``` java
59+ if (++ x == 0 ) { ... }
60+ ```
7261
7362There isn't a single statement equivalent in Python because assignments
74- are statements there, not expressions. So |j2py| does what it can,
75- presumably what you would do::
63+ are statements there, not expressions. So java2python does what it can,
64+ presumably what you would do:
7665
77- x += 1
78- if x == 0:
79- ...
66+ ``` python
67+ x += 1
68+ if x == 0 :
69+ ...
70+ ```
8071
8172Careful readers will have spotted just how close we came to driving
8273over a cliff with that ` ++x ` expression. If the increment had been
8374done on the other side of the variable, the meaning of the statement
8475would have changed and the Python code would have been wrong.
8576Fortunately, I've driven by lots of cliffs and have been scared by all
8677of them so I thought of this ahead of time and decided to do something
87- about it::
78+ about it:
8879
89- if (x++ ==0) { ... }
80+ ``` java
81+ if (x++ == 0 ) { ... }
82+ ```
9083
91- will get translated to::
84+ will get translated to:
9285
93- mangled_name_for_x = x
94- x += 1
95- if mangled_name_for_x == 0:
96- ...
86+ ``` python
87+ mangled_name_for_x = x
88+ x += 1
89+ if mangled_name_for_x == 0 :
90+ ...
91+ ```
9792
98- See what |j2py| did there? It tried to do what you would do. For
99- further explanation and enumeration see the :ref: ` features ` chapter .
93+ See what java2python did there? It tried to do what you would do. For
94+ further explanation and enumeration see the [ features] ( features.md ) page .
10095
10196
102- Why Bother?
103- ===========
97+ ### Why Bother?
10498
10599I bothered to write this because I needed a Java package to run on the
106100CPython interpreter. I got tired of porting by hand, so I wrote this
0 commit comments