Skip to content

Commit 38153c4

Browse files
author
Troy Melhase
committed
Added support for base class members via config modules. Closes natural#10.
Added support for mapping exception types. Closes natural#11. Fixed dotted type identifiers. Replaced "x == None" expressions with "x is None" in output code. Fixed class/instance member formatting strings to account for classmethods. Added various samples to defaultconfig.py. Other minor fixes; also trimmed extraneous whitespace.
1 parent 251f6ba commit 38153c4

File tree

4 files changed

+175
-72
lines changed

4 files changed

+175
-72
lines changed

java2python/bin/j2py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def transform(options):
4242
print '*** error: no AST generated.'
4343
return 2
4444

45-
filein = fileout = filedefault = '-'
45+
filein = fileout = filedefault = '-'
4646
if options.inputfile:
4747
filein = os.path.basename(options.inputfile)
4848
if options.outputfile:
4949
fileout = os.path.basename(options.outputfile)
5050
elif fileout != filedefault:
51-
fileout = '%s.py' % (os.path.splitext(filein)[0])
51+
fileout = '%s.py' % (os.path.splitext(filein)[0])
5252

5353
if fileout == filedefault:
5454
output = sys.stdout
@@ -60,16 +60,16 @@ def transform(options):
6060
M = Module(filein, fileout)
6161
W.walk(ast, M)
6262
source = str(M)
63-
print >> output, source
63+
print >> output, source
6464
walk_finish = time()
6565

6666
if options.syntaxcheck:
6767
try:
6868
compiler.parse(source)
69-
except (SyntaxError, ):
70-
msg = '## ERR: invalid syntax in generated source.'
69+
except (SyntaxError, ), ex:
70+
msg = "## ERR: %s" % (ex, )
7171
else:
72-
msg = '## INFO: generated source has valid syntax.'
72+
msg = '## INFO: generated source has valid syntax.'
7373
print >> sys.stderr, msg
7474

7575
finish = time()
@@ -101,12 +101,11 @@ def cli_options(argv):
101101
default=False, action='store_true')
102102
parser.add_option('-t', '--time', dest='timerun',
103103
help='Write processing time to stderr',
104-
default=False, action='store_true')
104+
default=False, action='store_true')
105105
options, args = parser.parse_args(argv)
106106
return options, args
107107

108108
if __name__ == '__main__':
109109
options, args = cli_options(sys.argv)
110110
ret = transform(options)
111111
sys.exit(ret)
112-

java2python/lib/defaultconfig.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
(r'(.*?)\.equalsIgnoreCase\((.*?)\)', r'\1.lower() == \2.lower()'),
8282
(r'([\w.]+)\.size\(\)', r'len(\1)'),
8383
(r'(\w+)\.get\((.*?)\)', r'\1[\2]'),
84+
(r'(\s)(\S*?)(\.toString\(\))', r'\1str(\2)'),
85+
(r'(\s)(\S*?)(\.length\(\))', r'\1len(\2)'),
8486
]
8587

8688
## mapping of java type names to python type names. user-defined
@@ -107,7 +109,8 @@
107109
## method name mapping. user-defined configuration modules can
108110
## replace and/or augment this with their own.
109111
renameMethodMap = {
110-
'equals':'__eq__'
112+
'equals':'__eq__',
113+
'is':'is_',
111114
}
112115

113116
## generic name mapping. user-defined configuration modules can
@@ -117,13 +120,42 @@
117120
'null':'None',
118121
'false':'False',
119122
'true':'True',
123+
'is':'is_',
124+
'str':'strval',
120125
}
121126

122127
## method type modifier mapping. when input methods have modifiers
123128
## with matching names, those names are replaced with their
124129
## corresponding statements from this mapping. this value can be
125130
## replaced and/or augmented via user-defined configuration modules.
126131
modifierDecoratorMap = {
127-
'synchronized':'## original method synchronized',
132+
'synchronized':'@synchronized(mlock)',
128133
'static':'@classmethod',
129134
}
135+
136+
137+
baseClassMembers = {
138+
'Thread':['MAX_PRIORITY', 'MIN_PRIORITY', 'NORM_PRIORITY',
139+
'activeCount', 'checkAccess', 'countStackFrames',
140+
'currentThread', 'destroy', 'dumpStack',
141+
'enumerate', 'getContextClassLoader', 'getName',
142+
'getPriority', 'getThreadGroup', 'holdsLock',
143+
'interrupt', 'interrupted', 'isAlive',
144+
'isDaemon', 'isInterrupted', 'join',
145+
'resume', 'run', 'setContextClassLoader',
146+
'setDaemon', 'setName', 'setPriority',
147+
'sleep', 'start', 'stop', 'suspend', 'toString', 'yield',
148+
],
149+
}
150+
151+
152+
variableNameMapping = {
153+
'str':'strval',
154+
'is':'is_',
155+
}
156+
157+
158+
exceptionTypeMapping = {
159+
'NoSuchFieldError':'AttributeError',
160+
'NoSuchMethodException':'AttributeError',
161+
}

0 commit comments

Comments
 (0)