Skip to content

Commit db60b70

Browse files
committed
Cast fully handled. For objects and primitives
1 parent 2922fbd commit db60b70

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

java2python/compiler/visitor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def acceptPreformatted(self, node, memo):
689689
acceptUnaryMinus = makeNodePreformattedExpr('-' + FS.l)
690690
acceptUnaryPlus = makeNodePreformattedExpr('+' + FS.l)
691691

692-
def acceptCastExpr(self, node, memo): # problem?
692+
def acceptCastExpr(self, node, memo):
693693
""" Accept and process a cast expression. """
694694
# If the type of casting is a primitive type,
695695
# then do the cast, else drop it.
@@ -699,12 +699,21 @@ def acceptCastExpr(self, node, memo): # problem?
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-
self.fs = FS.r
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 + ')'
714+
else:
715+
warn('Couldn\'t perform cast operation. ' + typeName + \
716+
' is not a primitive and objCastMode in the config file has wrong value.')
708717
self.left, self.right = visitors = factory(parent=self), factory(parent=self)
709718
self.zipWalk(node.children, visitors, memo)
710719

java2python/config/default.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@
173173
# convenience.
174174

175175

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+
176185
# module output subs.
177186
moduleOutputSubs = [
178187
(r'System\.out\.println\((.*)\)', r'print \1'),

0 commit comments

Comments
 (0)