Пример #1
0
    def __init__(self, source: IO = None):
        # Default to J2SE_7
        self._version = ClassVersion(0x32, 0)
        self._constants = ConstantPool()
        self.access_flags = Flags(
            '>H', {
                'acc_public': 0x0001,
                'acc_final': 0x0010,
                'acc_super': 0x0020,
                'acc_interface': 0x0200,
                'acc_abstract': 0x0400,
                'acc_synthetic': 0x1000,
                'acc_annotation': 0x2000,
                'acc_enum': 0x4000
            })
        self._this = 0
        self._super = 0
        self._interfaces = []
        self.fields = FieldTable(self)
        self.methods = MethodTable(self)
        self.attributes = AttributeTable(self)
        #: The ClassLoader bound to this ClassFile, if any.
        self.classloader = None

        if source:
            self._from_io(source)
Пример #2
0
def test_ldc():
    value = 5.0
    constants = ConstantPool()
    constant = constants.create_double(value)

    assert_incrementing_instruction(
        instruction=constant_instruction('ldc', constant),
        constants=constants,
        expected=[Push(Double.create_instance(value))])
Пример #3
0
def complex_machine():
    machine = Machine(FixedClassLoader({
        COMPLEX_CLASS_NAME: COMPLEX_CLASS,
        EXCEPTION_NAME: JvmClass(
            name=EXCEPTION_NAME,
            name_of_base=RootObjectType.refers_to,
            constants=ConstantPool()
        ),
        RootObjectType.refers_to: JvmClass(
            name=RootObjectType.refers_to,
            name_of_base=None,
            constants=ConstantPool()
        )
    }))
    for _ in range(2):
        frame = Frame.from_class_and_method(COMPLEX_CLASS, METHOD)
        machine.frames.push(frame)
    return machine
Пример #4
0
def test_printable_constants():
    # Ensure we can successfully repr valid constants without crashing.
    pool = ConstantPool()
    repr(pool.create_utf8('HelloWorld'))
    repr(pool.create_class('HelloWorld'))
    repr(pool.create_double(1))
    repr(pool.create_float(1))
    repr(pool.create_integer(1))
    repr(pool.create_long(1))
    repr(pool.create_name_and_type('HelloWorld', 'I'))
    repr(pool.create_field_ref('HelloWorld', 'test', 'I'))
    repr(pool.create_method_ref('HelloWorld', 'test', 'I)V'))
    repr(pool.create_interface_method_ref('HelloWorld', 'test', 'I)V'))
    repr(pool.create_string('HelloWorld'))
Пример #5
0
def test_invoke_v():
    method_name = 'method_name'
    class_name = 'class_name'

    consts = ConstantPool()
    descriptor = '(II)V'
    key = MethodKey(method_name, descriptor)
    no_op = Instruction.create('nop')

    method = BytecodeMethod(
        name='method_name',
        descriptor='(II)V',
        max_locals=5,
        max_stack=5,
        instructions=[no_op, no_op],
        args=[Integer, Integer],
    )

    jvm_class = JvmClass(
        class_name,
        RootObjectType.refers_to,
        consts,
        methods={
            key: method
        }
    )

    method_ref = consts.create_method_ref(class_name, method_name, descriptor)
    instruction = constant_instruction('invokevirtual', method_ref)
    loader = FixedClassLoader({
        class_name: jvm_class
    })

    instance = loader.default_instance(class_name)
    arg_value = SOME_INT
    arguments = [instance, arg_value, arg_value]
    reversed_arguments = list(reversed(arguments))
    assert_instruction(
        constants=consts,
        loader=loader,
        instruction=instruction,
        op_stack=reversed_arguments,
        expected=[
            Pop(3),
            Invoke(class_name, key, arguments)
        ]
    )
Пример #6
0
def test_string_constant():
    text = 'some_text'
    consts = ConstantPool()
    const = consts.create_string(text)

    chars = [ord(c) for c in text]
    char_array = ArrayReferenceType(Integer).create_instance(chars)
    hash_value = hash(text) % (2**32)
    hash_ = Integer.create_instance(hash_value)

    reference_type = ObjectReferenceType('java/lang/String')
    jvm_object = JvmObject({'hash': hash_, 'value': char_array})

    assert_incrementing_instruction(
        instruction=constant_instruction('ldc', const),
        constants=consts,
        expected=[Push(reference_type.create_instance(jvm_object))])
Пример #7
0
METHOD = BytecodeMethod(
    name='method_name',
    descriptor='(II)V',
    instructions=[
        named_tuple_replace(Instruction.create('nop'), pos=i) for i in range(5)
    ],
    max_locals=5,
    max_stack=15,
    args=[Integer, Integer],
    exception_handlers=Handlers([HANDLER])
)

COMPLEX_CLASS = JvmClass(
    name=COMPLEX_CLASS_NAME,
    name_of_base=RootObjectType.refers_to,
    constants=ConstantPool(),
    fields={
        FIELD_NAME: Integer
    },
    static_fields={
        FIELD_NAME: Integer
    },
    methods={
        METHOD_KEY: METHOD
    }
)


def complex_machine():
    machine = Machine(FixedClassLoader({
        COMPLEX_CLASS_NAME: COMPLEX_CLASS,