Skip to content

Commit 1c4d18f

Browse files
committed
+ Iterator iter.remove(), test: ForEachExamples
1 parent 2412a6b commit 1c4d18f

11 files changed

+153
-28
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ As demonstrated by the third example above, python scripts can step in, to chang
8080
- do switch statement right
8181
- make install
8282
- do java.typed right for VarArityParam (see LoopStyles)
83-
83+
- find a way to translate statements to produce no output
8484

8585

8686
### Pitfalls

j2py/addinit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def add_init(ast):
5757
add import of System,Object
5858
"""
5959

60-
imp = decode('Id("from java import Object,System")')
60+
imp = decode('Id("from java import *")')
6161
ast[1].insert(0,imp)
6262

6363
imp = decode('Id("import java")')

j2py/j2py.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
import sys
66

77

8-
from renameids import rename_ids
8+
import rename
99
from fixnames import fix_names
1010
from translatepackages import translate_packages
1111
import fixstradd
1212
import addinit
1313
from fixcomments import convert_comments
1414
from fixenum import fix_enum
15-
1615
import fix_values
16+
import rename_methods
1717

1818
if __name__ == '__main__':
1919
ast = aterm.decode(sys.stdin.read())
20-
rename_ids(ast)
20+
rename.run(ast)
2121
fix_values.run(ast)
2222
convert_comments(ast)
2323
fix_names(ast)
24+
rename_methods.run(ast)
2425
translate_packages(ast)
2526
fixstradd.run(ast)
2627

j2py/java/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from decorators import init,innerclass,typed,staticclass,overloaded
77
from decorators import implements,extends,use_class_init
88

9-
from objects import Array,Object,String,System, Interface
9+
from objects import Array,Object,String,System, Interface, Number, Integer, Class
1010

1111
from enum import Enum,enum,EnumItem
1212

j2py/java/math/BigDecimal.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from java.math import Number
2+
3+
class BigDecimal(Number,float):
4+
pass
5+
6+

j2py/java/math/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from java import Object
2+
3+
class Number(Object):
4+
pass
5+

j2py/java/objects.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def __getattr__(self,key):
88
raise Exception('key not found') #TODO raise right exception, read docs ...
99

1010

11-
class TypeWrapper(object):
11+
class Class(object):
1212
def __init__(self,typ):
1313
self.typ = typ
1414

@@ -26,7 +26,7 @@ class Object(object):
2626
__interfaces = []
2727

2828
def getClass(self):
29-
return TypeWrapper(self.__class__)
29+
return Class(self.__class__)
3030

3131

3232
String = "".__class__
@@ -49,5 +49,29 @@ class Interface(object):
4949
@classmethod
5050
def getName(self):
5151
return self.__name__
52-
52+
53+
class Number(object):
54+
pass
55+
56+
class Integer(int,Number):
57+
pass
58+
59+
60+
class Iterator(object):
61+
62+
def __init__(self,collection):
63+
self.pos = 0
64+
self.collection = collection
65+
66+
def hasNext(self):
67+
return self.pos < len(self.collection)
68+
69+
def next(self):
70+
self.pos += 1
71+
return self.collection[self.pos-1]
72+
73+
def remove(self):
74+
self.collection.pop(self.pos-1)
75+
self.pos -= 1
76+
5377

j2py/java/util.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1+
from objects import Iterator, Object
2+
from helpers import str
3+
4+
15
class Collection(list):
26
def add(self,o):
37
self.append(o)
48

59
def iterator(self):
610
return Iterator(self)
711

12+
def __str__(self):
13+
return "[%s]" % (', '.join([str(i) for i in self]))
14+
815

916
class List(Collection):
1017
pass
1118

12-
13-
class Iterator(object):
14-
15-
def __init__(self,collection):
16-
self.pos = 0
17-
self.collection = collection
18-
19-
def hasNext(self):
20-
return self.pos < len(self.collection)
21-
22-
def next(self):
23-
self.pos += 1
24-
return self.collection[self.pos-1]
2519

26-
2720
class ArrayList(Collection):
28-
def __init__(self,typ):
21+
def __init__(self,typ = (Object,)):
2922
self._typ = typ
3023

3124

j2py/renameids.py renamed to j2py/rename.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
DEBUG = False
1212

13+
1314
def rename_ids(ast):
1415
"""
1516
"""
1617

1718
ren = {
18-
"String" : AString("java.String")
19+
# "String" : AString("String")
1920
}
2021

2122

@@ -24,7 +25,7 @@ def rename_ids(ast):
2425
tn[0][0]=ren[tn[0][0]]
2526

2627
ren = {
27-
"class" : "__class__"
28+
# "class" : "__class__"
2829
}
2930

3031
kw = "and,del,elif,from,in,is,not,or,print,str,None".split(',')
@@ -34,10 +35,15 @@ def rename_ids(ast):
3435
id[0]=AString(ren[id[0]])
3536
elif id[0] in kw:
3637
id[0]=AString(id[0]+'_')
37-
38+
39+
40+
41+
def run(ast):
42+
rename_ids(ast)
43+
3844
if __name__ == '__main__':
3945
ast = decode(sys.stdin.read())
40-
rename_ids(ast)
46+
run(ast)
4147
if not DEBUG:
4248
print ast
4349

j2py/rename_methods.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
4+
from aterm import *
5+
import sys
6+
7+
"""
8+
TODO write docstring
9+
"""
10+
11+
DEBUG = False
12+
13+
14+
def rename_methods(ast):
15+
16+
ren = {
17+
"length" : "__len__"
18+
, "substring" : "__slice__"
19+
}
20+
21+
#TODO check also for type
22+
for id in ast.findall("Id"):
23+
if id.up.name in ["Method"]:
24+
if id[0] in ren.keys():
25+
id[0]=ren[id[0]]
26+
27+
def run(ast):
28+
rename_methods(ast)
29+
30+
if __name__ == '__main__':
31+
ast = decode(sys.stdin.read())
32+
run(ast)
33+
if not DEBUG:
34+
print ast
35+
36+

j2py/test/j2py/ForEachExamples.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.*;
2+
import java.math.BigDecimal;
3+
4+
public final class ForEachExamples {
5+
6+
public static void main(String... aArgs){
7+
List<Number> numbers = new ArrayList<Number>();
8+
numbers.add(new Integer(42));
9+
numbers.add(new Integer(-30));
10+
numbers.add(new BigDecimal("654.2"));
11+
12+
//typical for-each loop
13+
//processes each item, without changing the collection or array.
14+
for ( Number number : numbers ){
15+
log(number);
16+
}
17+
18+
//use with an array
19+
String[] names = {"Ethan Hawke", "Julie Delpy"};
20+
for( String name : names ){
21+
log("Name : " + name);
22+
}
23+
24+
//removal of items requires an explicit iterator :
25+
Collection<String> words = new ArrayList<String>();
26+
words.add("Il ne lui faut que deux choses: ");
27+
words.add("le");
28+
words.add("pain");
29+
words.add("et");
30+
words.add("le");
31+
words.add("temps.");
32+
words.add("- Alfred de Vigny.");
33+
for(Iterator<String> iter = words.iterator(); iter.hasNext();){
34+
if (iter.next().length() == 4){
35+
iter.remove();
36+
}
37+
}
38+
log("Edited words: " + words.toString());
39+
40+
//if used with a non-parameterized type, then Object must be used
41+
Collection stuff = new ArrayList();
42+
stuff.add("blah");
43+
for (Object thing : stuff){
44+
String item = (String)thing;
45+
log("Thing : " + item);
46+
}
47+
}
48+
49+
// PRIVATE //
50+
private static void log(Object aThing){
51+
System.out.println(aThing);
52+
}
53+
}
54+

0 commit comments

Comments
 (0)