Skip to content

Commit edf0a51

Browse files
author
Troy Melhase
committed
Applied patches from lilingv for anonymous class detection and generation.
Closes natural#15.
1 parent 588c146 commit edf0a51

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

java2python/lib/sourcetypes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Source:
9393
config = Config()
9494
staticmethodLiteral = '@staticmethod'
9595
classmethodLiteral = '@classmethod'
96+
anonymousClassCount = 0
9697

9798
def __init__(self, parent=None, name=None):
9899
self.parent = parent

java2python/lib/walker.g

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,15 +780,22 @@ returns [value = block.missingValue]
780780
exp = ()
781781
arrexp = None
782782
arrdecl = None
783+
klass = block.newClass()
783784
}
784785
: #("new"
785-
typ = type[block]
786+
typ = type[block]
786787
(arrdecl = new_array_declarator[block]
787788
( arrexp = array_initializer[block])?
788-
| exp = expr_list[block] (obj_block[block])?
789+
| exp = expr_list[block] (obj_block[klass])?
789790
)
790791
)
791792
{
793+
if len(klass.lines):
794+
block.anonymousClassCount += 1
795+
klass.addBaseClass(typ)
796+
klass.setName("%s%s" % (typ, block.anonymousClassCount))
797+
else:
798+
block.lines.remove(klass)
792799
alltypes = block.config.combined("typeValueMap")
793800
if arrdecl:
794801
value = ("[%s() for __idx0 in range(%s)]", (("%s", typ), ("%s", arrdecl)))

java2python/tests/AnonTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class AnonTest {
2+
public static void main(String[] args) {
3+
4+
int y = 0;
5+
6+
test(
7+
new Frobinator() {
8+
public void haveFrobinated( FrobEvent e ) {
9+
otherAction();
10+
}
11+
}
12+
);
13+
14+
15+
16+
}
17+
18+
public static void test(Object o) {
19+
System.out.println(o);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)