Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21f8779

Browse files
author
Troy Melhase
committed
Makes expression cast config point a callable.
1 parent 706839d commit 21f8779

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

java2python/compiler/visitor.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -699,21 +699,16 @@ def acceptCastExpr(self, node, memo):
699699
typeName = typeIdent.text
700700
if typeIdent.type == tokens.QUALIFIED_TYPE_IDENT:
701701
typeName = typeIdent.firstChild().text
702-
702+
703703
if typeName in tokens.primitiveTypeNames:
704704
# Cast using the primitive type constructor
705705
self.fs = typeName + '(' + FS.r + ')'
706706
else:
707-
mode = self.config.last('objCastMode')
708-
if mode == 'drop':
709-
# Use drop policy
710-
self.fs = FS.r
711-
elif mode == 'ctor':
712-
# Make constructor
713-
self.fs = FS.l + '(' + FS.r + ')'
707+
handler = self.configHandler('Cast')
708+
if handler:
709+
handler(self, node)
714710
else:
715-
warn('Couldn\'t perform cast operation. ' + typeName + \
716-
' is not a primitive and objCastMode in the config file has wrong value.')
711+
warn('No handler to perform cast of non-primitive type %s.', typeName)
717712
self.left, self.right = visitors = factory(parent=self), factory(parent=self)
718713
self.zipWalk(node.children, visitors, memo)
719714

java2python/config/default.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,17 @@
168168
# in method declarations. set to 0 to disable.
169169
#minIndentParams = 5
170170

171+
# Specifies handler for cast operations of non-primitive types are handled
172+
# (primitive types are automatically handled). Use basic.castDrop to leave
173+
# cast expressions out of generated source. Use basic.castCtor to transform
174+
# casts into constructor calls. Or you can specify a function of your own.
175+
expressionCastHandler = basic.castDrop
176+
171177

172178
# Values below are used by the handlers. They're here for
173179
# convenience.
174180

175181

176-
# Specifies how cast operations of non-primitive types are handled
177-
# (primitive types are automatically handled)
178-
# Valid values:
179-
# - 'drop': completely drops type and cast info
180-
# - 'ctor': converts the cast in a constructor call
181-
# E.g.: (Cast) x -> Cast(x)
182-
objCastMode = 'drop'
183-
184-
185182
# module output subs.
186183
moduleOutputSubs = [
187184
(r'System\.out\.println\((.*)\)', r'print \1'),

java2python/mod/basic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from os import path
88
from re import sub as rxsub
99

10+
from java2python.lib import FS
11+
1012

1113
def shebangLine(module):
1214
""" yields the canonical python shebang line. """
@@ -237,3 +239,9 @@ def moveStaticExpressions(cls):
237239
module.adopt(newExpr, index=len(module.children))
238240

239241

242+
def castCtor(expr, node):
243+
expr.fs = FS.l + '(' + FS.r + ')'
244+
245+
246+
def castDrop(expr, node):
247+
expr.fs = FS.r

test/Cast0.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
interface Something {
2+
public int foo();
3+
}
4+
5+
6+
interface SomethingElse {
7+
public int foo();
8+
}
9+
10+
11+
class Both implements Something {
12+
public int foo() {
13+
return 100;
14+
}
15+
}
16+
17+
class What extends Both {
18+
public int foo() {
19+
return 200;
20+
}
21+
}
22+
23+
24+
class Cast0 {
25+
public static void main(String[] args) {
26+
int x = 33;
27+
Integer ix = (Integer) x;
28+
System.out.println(x);
29+
System.out.println(ix);
30+
31+
What w = new What();
32+
33+
System.out.println( w.foo() );
34+
35+
Both b = (Both) w;
36+
37+
System.out.println( b.foo() );
38+
39+
}
40+
}

test/configs/Cast0.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from java2python.mod import basic
2+
3+
expressionCastHandler = basic.castDrop

0 commit comments

Comments
 (0)