Skip to content

Commit 251f6ba

Browse files
author
Troy Melhase
committed
Added support for "public static void main" method to become script
call. Closes natural#4. Also added tests directory and HelloWorldApp.java sample.
1 parent 80b1190 commit 251f6ba

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

java2python/lib/defaultconfig.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
## if True, a default docstring is generated for method statements.
3434
writeMethodDocString = False
3535

36+
## if True, and if input source contains a "public static void main"
37+
## method, a block is written to the end of the file to call the class
38+
## method with sys.argv.
39+
writeMainMethodScript = True
40+
3641
## if True, classes without base classes will instead inherit from
3742
## object.
3843
classesInheritObject = True
@@ -119,5 +124,6 @@
119124
## corresponding statements from this mapping. this value can be
120125
## replaced and/or augmented via user-defined configuration modules.
121126
modifierDecoratorMap = {
122-
'synchronized':'## original method synchronized'
127+
'synchronized':'## original method synchronized',
128+
'static':'@classmethod',
123129
}

java2python/lib/sourcetypes.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,11 @@ def writeTo(self, output, indent):
390390
output.write('\n')
391391
Source.writeTo(self, output, indent)
392392
self.writeExtraLines('moduleEpilogue', output)
393-
393+
if self.config.last('writeMainMethodScript'):
394+
self.writeMainBlock(output)
395+
394396
def writeExtraLines(self, name, output):
395-
""" writes an sequence of lines given a config attribute name
397+
""" writes a sequence of lines given a config attribute name
396398
397399
Lines may be callable. Refer to the defaultconfig module for
398400
details.
@@ -409,7 +411,29 @@ def writeExtraLines(self, name, output):
409411
pass
410412
output.write('%s\n' % (line, ))
411413

414+
def writeMainBlock(self, output):
415+
""" writes a block to call the module as a script
412416
417+
@param output writable file-like object
418+
@return None
419+
"""
420+
try:
421+
cls = [c for c in self.lines if getattr(c, 'isClass', None)][0]
422+
methods = [m for m in cls.lines if getattr(m, 'isMethod', None)]
423+
main = [m for m in methods if m.name == 'main'][0]
424+
except (Exception, ):
425+
pass
426+
else:
427+
mods = main.modifiers
428+
typ = main.type
429+
if ('public' in mods) and ('static' in mods) and (typ == 'void'):
430+
name = cls.name
431+
offset = self.I(1)
432+
output.write("if __name__ == '__main__':\n")
433+
output.write("%simport sys\n" % offset)
434+
output.write("%s%s.main(sys.argv)\n" % (offset, name))
435+
436+
413437
class Class(Source):
414438
""" Class -> specialized block type
415439
@@ -550,9 +574,12 @@ class Method(Source):
550574
""" Method -> specialized block type
551575
552576
"""
577+
instanceFirstParam = ('object', 'self')
578+
classFirstParam = ('type', 'cls')
579+
553580
def __init__(self, parent, name):
554581
Source.__init__(self, parent=parent, name=name)
555-
self.parameters = [('object', 'self'), ]
582+
self.parameters = [self.instanceFirstParam, ]
556583

557584
def addModifier(self, name):
558585
""" adds named modifier to method
@@ -570,6 +597,9 @@ def addModifier(self, name):
570597
else:
571598
if deco not in self.preamble:
572599
self.preamble.append(deco)
600+
if (deco == '@classmethod') and (self.parameters) \
601+
and (self.parameters[0] == self.instanceFirstParam):
602+
self.parameters[0] = self.classFirstParam
573603
Source.addModifier(self, name)
574604

575605
def addParameter(self, typ, name):

java2python/lib/walker.g

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ returns [typ]
102102

103103

104104
modifiers [block, mod=None]
105-
: #(MODIFIERS (mod = modifier[block])*)
105+
: #(MODIFIERS (mod = modifier[block]
106106
{
107107
if mod:
108108
block.addModifier(mod)
109-
}
109+
})*
110+
)
110111
;
111112

112113

@@ -214,6 +215,9 @@ method_def [block]
214215
method_head[meth]
215216
(statement_list[meth])?
216217
)
218+
{
219+
meth.type = typ
220+
}
217221
;
218222

219223

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class HelloWorldApp {
2+
public void foo() {
3+
System.out.println("FOO");
4+
}
5+
public static void main(String[] args) {
6+
System.out.println("Hello, world.");
7+
System.out.println(args);
8+
}
9+
}

0 commit comments

Comments
 (0)