diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py index 40b6ea7..8e8a54d 100644 --- a/java2python/compiler/visitor.py +++ b/java2python/compiler/visitor.py @@ -678,7 +678,6 @@ def acceptPreformatted(self, node, memo): return acceptPreformatted acceptArrayElementAccess = makeNodePreformattedExpr(FS.l + '[' + FS.r + ']') - acceptCastExpr = makeNodePreformattedExpr(FS.l + '(' + FS.r + ')' ) # problem? acceptDiv = makeNodePreformattedExpr(FS.l + ' / ' + FS.r) acceptLogicalAnd = makeNodePreformattedExpr(FS.l + ' and ' + FS.r) acceptLogicalNot = makeNodePreformattedExpr('not ' + FS.l) @@ -690,6 +689,34 @@ def acceptPreformatted(self, node, memo): acceptUnaryMinus = makeNodePreformattedExpr('-' + FS.l) acceptUnaryPlus = makeNodePreformattedExpr('+' + FS.l) + def acceptCastExpr(self, node, memo): + """ Accept and process a cast expression. """ + # If the type of casting is a primitive type, + # then do the cast, else drop it. + factory = self.factory.expr + typeTok = node.firstChildOfType(tokens.TYPE) + typeIdent = typeTok.firstChild() + typeName = typeIdent.text + if typeIdent.type == tokens.QUALIFIED_TYPE_IDENT: + typeName = typeIdent.firstChild().text + + if typeName in tokens.primitiveTypeNames: + # Cast using the primitive type constructor + self.fs = typeName + '(' + FS.r + ')' + else: + mode = self.config.last('objCastMode') + if mode == 'drop': + # Use drop policy + self.fs = FS.r + elif mode == 'ctor': + # Make constructor + self.fs = FS.l + '(' + FS.r + ')' + else: + warn('Couldn\'t perform cast operation. ' + typeName + \ + ' is not a primitive and objCastMode in the config file has wrong value.') + self.left, self.right = visitors = factory(parent=self), factory(parent=self) + self.zipWalk(node.children, visitors, memo) + def makeAcceptPrePost(suffix, pre): """ Make an accept method for pre- and post- assignment expressions. """ def acceptPrePost(self, node, memo): diff --git a/java2python/config/default.py b/java2python/config/default.py index 45bb503..bbe6d31 100644 --- a/java2python/config/default.py +++ b/java2python/config/default.py @@ -173,6 +173,15 @@ # convenience. +# Specifies how cast operations of non-primitive types are handled +# (primitive types are automatically handled) +# Valid values: +# - 'drop': completely drops type and cast info +# - 'ctor': converts the cast in a constructor call +# E.g.: (Cast) x -> Cast(x) +objCastMode = 'drop' + + # module output subs. moduleOutputSubs = [ (r'System\.out\.println\((.*)\)', r'print \1'), diff --git a/java2python/lang/base.py b/java2python/lang/base.py index 4c10110..0633b8e 100644 --- a/java2python/lang/base.py +++ b/java2python/lang/base.py @@ -88,6 +88,11 @@ def methodTypes(self): mod = self.module return (mod.VOID_METHOD_DECL, mod.FUNCTION_METHOD_DECL, ) + @property + def primitiveTypeNames(self): + """ Type name of well-known primitive types """ + return ('bool', 'str', 'int', 'long', 'float', ) + @property def module(self): """ Provides lazy import to the parser module. """