Created
November 17, 2012 03:56
-
-
Save TkTech/4093163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
""" | |
An example showing how to create a "Hello World" class from scratch. | |
""" | |
from jawa import ClassFile | |
from jawa.assemble import assemble | |
if __name__ == '__main__': | |
cf = ClassFile.create('HelloWorld') | |
main = cf.methods.create('main', '([Ljava/lang/String;)V', code=True) | |
main.access_flags.acc_static = True | |
main.code.max_locals = 1 | |
main.code.max_stack = 2 | |
main.code.assemble(assemble([ | |
('getstatic', cf.constants.create_field_ref( | |
'java/lang/System', | |
'out', | |
'Ljava/io/PrintStream;' | |
)), | |
('ldc', cf.constants.create_string('Hello World!')), | |
('invokevirtual', cf.constants.create_method_ref( | |
'java/io/PrintStream', | |
'println', | |
'(Ljava/lang/String;)V' | |
)), | |
('return',) | |
])) | |
with open('HelloWorld.class', 'wb') as fout: | |
cf.save(fout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment