GeezerVison is showing github.com — Makes expression cast config point a callable. · hellcoderz/java2python@21f8779
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: java2python/compiler/visitor.py +5-10Lines changed: 5 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -699,21 +699,16 @@ def acceptCastExpr(self, node, memo): |
699 | 699 | typeName = typeIdent.text |
700 | 700 | if typeIdent.type == tokens.QUALIFIED_TYPE_IDENT: |
701 | 701 | typeName = typeIdent.firstChild().text |
702 | | - |
| 702 | + |
703 | 703 | if typeName in tokens.primitiveTypeNames: |
704 | 704 | # Cast using the primitive type constructor |
705 | 705 | self.fs = typeName + '(' + FS.r + ')' |
706 | 706 | 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) |
714 | 710 | 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) |
717 | 712 | self.left, self.right = visitors = factory(parent=self), factory(parent=self) |
718 | 713 | self.zipWalk(node.children, visitors, memo) |
719 | 714 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: java2python/config/default.py +6-9Lines changed: 6 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
168 | 168 | # in method declarations. set to 0 to disable. |
169 | 169 | #minIndentParams = 5 |
170 | 170 |
|
| 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 | + |
171 | 177 |
|
172 | 178 | # Values below are used by the handlers. They're here for |
173 | 179 | # convenience. |
174 | 180 |
|
175 | 181 |
|
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 | | - |
185 | 182 | # module output subs. |
186 | 183 | moduleOutputSubs = [ |
187 | 184 | (r'System\.out\.println\((.*)\)', r'print \1'), |
|
Collapse file
Copy file name to clipboardExpand all lines: java2python/mod/basic.py +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
7 | 7 | from os import path |
8 | 8 | from re import sub as rxsub |
9 | 9 |
|
| 10 | +from java2python.lib import FS |
| 11 | + |
10 | 12 |
|
11 | 13 | def shebangLine(module): |
12 | 14 | """ yields the canonical python shebang line. """ |
@@ -237,3 +239,9 @@ def moveStaticExpressions(cls): |
237 | 239 | module.adopt(newExpr, index=len(module.children)) |
238 | 240 |
|
239 | 241 |
|
| 242 | +def castCtor(expr, node): |
| 243 | + expr.fs = FS.l + '(' + FS.r + ')' |
| 244 | + |
| 245 | + |
| 246 | +def castDrop(expr, node): |
| 247 | + expr.fs = FS.r |
Collapse file
Copy file name to clipboard +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +} |
Collapse file
Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +from java2python.mod import basic |
| 2 | + |
| 3 | +expressionCastHandler = basic.castDrop |