Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored code for generating bytecode of constant pool

  • Loading branch information...
commit 4d4403385285fdfb0ed197ce2048a363285c35e4 1 parent 5cbadc0
Simeon Visser authored
Showing with 30 additions and 35 deletions.
  1. +30 −35 java/codegenerator.py
65 java/codegenerator.py
View
@@ -2,6 +2,16 @@
from java.bytecode import generate_bytecode
+CONSTANT_TAGS = {
+ 'Utf8': "01",
+ 'Class': "07",
+ 'String': "08",
+ 'Fieldref': "09",
+ 'Methodref': "0A",
+ 'NameAndType': "0C",
+}
+
+
def _write_int(i, width=4):
return unhexlify(hex(i)[2:].rjust(width, '0'))
@@ -13,50 +23,35 @@ def _write_indices(*indices):
return result
-def write_CONSTANT_Utf8(s):
- return unhexlify("01") + _write_int(len(s)) + bytes(s.encode())
-
-
-def write_CONSTANT_Class(index):
- return unhexlify("07") + _write_indices(index)
-
-
-def write_CONSTANT_String(index):
- return unhexlify("08") + _write_indices(index)
-
-
-def write_CONSTANT_Fieldref(index1, index2):
- return unhexlify("09") + _write_indices(index1, index2)
-
-
-def write_CONSTANT_NameAndType(index1, index2):
- return unhexlify("0C") + _write_indices(index1, index2)
-
-
-def write_CONSTANT_Methodref(index1, index2):
- return unhexlify("0A") + _write_indices(index1, index2)
+def write_CONSTANT(tag, *data):
+ result = unhexlify(CONSTANT_TAGS[tag])
+ if tag == 'Utf8':
+ result += _write_int(len(data[0])) + bytes(data[0].encode())
+ else:
+ result += _write_indices(*data)
+ return result
def write_constant_pool(data):
pool = {}
# this_class
- pool[1] = write_CONSTANT_Class(2)
- pool[2] = write_CONSTANT_Utf8("PythonSample")
+ pool[1] = write_CONSTANT('Class', 2)
+ pool[2] = write_CONSTANT('Utf8', "PythonSample")
# super_class
- pool[3] = write_CONSTANT_Class(4)
- pool[4] = write_CONSTANT_Utf8("java/lang/Object")
+ pool[3] = write_CONSTANT('Class', 4)
+ pool[4] = write_CONSTANT('Utf8', "java/lang/Object")
# constructor from Object
- pool[5] = write_CONSTANT_Methodref(3, 6)
- pool[6] = write_CONSTANT_NameAndType(7, 8)
- pool[7] = write_CONSTANT_Utf8("<init>")
- pool[8] = write_CONSTANT_Utf8("()V")
+ pool[5] = write_CONSTANT('Methodref', 3, 6)
+ pool[6] = write_CONSTANT('NameAndType', 7, 8)
+ pool[7] = write_CONSTANT('Utf8', "<init>")
+ pool[8] = write_CONSTANT('Utf8', "()V")
# Code attribute
- pool[9] = write_CONSTANT_Utf8("Code")
+ pool[9] = write_CONSTANT('Utf8', "Code")
# public static void main(String[] args)
- pool[10] = write_CONSTANT_Methodref(1, 11)
- pool[11] = write_CONSTANT_NameAndType(12, 13)
- pool[12] = write_CONSTANT_Utf8("main")
- pool[13] = write_CONSTANT_Utf8("([Ljava/lang/String;)V")
+ pool[10] = write_CONSTANT('Methodref', 1, 11)
+ pool[11] = write_CONSTANT('NameAndType', 12, 13)
+ pool[12] = write_CONSTANT('Utf8', "main")
+ pool[13] = write_CONSTANT('Utf8', "([Ljava/lang/String;)V")
return pool
Please sign in to comment.
Something went wrong with that request. Please try again.