Skip to content

Commit 7a6f0fb

Browse files
author
Troy Melhase
committed
Blindly moves class-level expressions containing the class name
to the module scope. See issue natural#5.
1 parent 4ea8366 commit 7a6f0fb

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

java2python/config/default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
# name. It's commented out because its output differs so greatly
7070
# from its input, and because it's really not very useful.
7171
classPostWalkHandlers = [
72+
basic.moveStaticExpressions,
7273
## basic.classContentSort,
7374
]
7475

java2python/mod/basic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,15 @@ def zopeImplementsClassHead(obj):
225225
if implAny(obj):
226226
for cls in obj.bases:
227227
yield 'zope.interface.implements({})'.format(cls)
228+
229+
230+
def moveStaticExpressions(cls):
231+
name = '{}.'.format(cls.name) # notice the dot
232+
exprs = [child for child in cls.children if child.isExpression and name in str(child)]
233+
module = cls.parents(lambda x:x.isModule).next()
234+
for expr in exprs:
235+
cls.children.remove(expr)
236+
newExpr = module.factory.expr(fs=name + '{right}', right=expr)
237+
module.adopt(newExpr, index=len(module.children))
238+
239+

test/Class13.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Class13 {
2+
3+
static int x = 0;
4+
static int y = Class13.x;
5+
6+
Class klass = Class13.class;
7+
8+
public static void main(String[] args) {
9+
System.out.println(Class13.x == Class13.y ? 1 : 0);
10+
}
11+
12+
}

0 commit comments

Comments
 (0)