def init_native_classes(self): pc = primitive_classes for type in 'Ddouble Bbyte Cchar Ffloat Iint Jlong Sshort Zboolean'.split(): name = type[1:] klass = Class(name) klass.java_instance = self.load_class('java/lang/Class').instantiate() klass.java_instance._values['class_name'] = name klass.primitive = True pc[type[0]] = pc[name] = klass
def doPrivileged(klass, vm, method, frame): action = frame.get_local(0) run_method_name = Class.method_name('run', '()Ljava/lang/Object;') method = action._klass.methods[run_method_name] # instance to call on result = vm.wrap_run_method(action, method) return result
def invokevirtual_special(vm, frame, offset, bytecode): method_index = vm.constant_pool_index(bytecode, frame.pc) new_klass, method = vm.resolve_field(frame.klass, method_index) # fetch the instance and then extract the class from that. # we do this so that in the case of an abstract class we get # the implemented method if bytecode[frame.pc] == 182: instance = frame.stack[-len(method.parameters) - 1] if instance is null: # remove all our method parameters for i in xrange(len(method.parameters) + 1): frame.pop() raise Exception vm.throw_exception(frame, 'java/lang/NullPointerException') return assert new_klass.is_subclass(instance),\ '%s not a subclass of %s' % (str(new_klass), str(instance)) new_klass = instance._klass if bytecode[frame.pc] == 184: assert (method.access_flags & ACC_STATIC) != 0 # find the superclass that contains this method (or we have it) method_name = Class.method_name(method.name, method.descriptor) while method_name not in new_klass.methods: new_klass = new_klass.super_class if new_klass.super_class == new_klass: break assert method_name in new_klass.methods new_method = new_klass.methods[method_name] vm.run_method(new_klass, new_method) frame.pc += 2
def invokevirtual_special(vm, frame, offset, bytecode): method_index = vm.constant_pool_index(bytecode, frame.pc) new_klass, method = vm.resolve_field(frame.klass, method_index) # fetch the instance and then extract the class from that. # we do this so that in the case of an abstract class we get # the implemented method if bytecode[frame.pc] == 182: instance = frame.stack[-len(method.parameters)-1] if instance is null: # remove all our method parameters for i in xrange(len(method.parameters)+1): frame.pop() raise Exception vm.throw_exception(frame, 'java/lang/NullPointerException') return assert new_klass.is_subclass(instance),\ '%s not a subclass of %s' % (str(new_klass), str(instance)) new_klass = instance._klass if bytecode[frame.pc] == 184: assert (method.access_flags & ACC_STATIC) != 0 # find the superclass that contains this method (or we have it) method_name = Class.method_name(method.name, method.descriptor) while method_name not in new_klass.methods: new_klass = new_klass.super_class if new_klass.super_class == new_klass: break assert method_name in new_klass.methods new_method = new_klass.methods[method_name] vm.run_method(new_klass, new_method) frame.pc += 2
def registerStreams(klass, vm): OutputStream = vm.load_class('java/io/OutputStream') PrintOutputStream = Class('java/io/PrintOutputStream', super_class=OutputStream) @PrintOutputStream.override_native_method('write') def write(*args): print 'write', args PrintStream = vm.load_class('java/io/PrintStream') system_out = PrintStream.instantiate() system_out_stream = PrintOutputStream.instantiate() vm.stack.push(system_out) vm.stack.push(system_out_stream) klass.field_overrides['out'] = system_out print PrintStream.get_method('<init>', '(Ljava/io/OutputStream;)V') klass, method = PrintStream.get_method('<init>', '(Ljava/io/OutputStream;)V') vm.wrap_run_method(system_out, method, system_out_stream) return void
def parse(self): # read the first magic bytes for magic in MAGIC: byte = self._read_byte() if byte != magic: raise MalformedClassException() klass = self.klass # class file version klass.minor_version = self._read_byte2() klass.major_version = self._read_byte2() constant_pool_length = self._read_byte2() klass.constant_pool = ConstantPool(constant_pool_length) while klass.constant_pool.size < constant_pool_length - 1: klass.constant_pool.add_pool(self.parse_constant_pool_item()) klass.access_flags = self._read_byte2() klass.this_class = klass.constant_pool.get_class(self._read_byte2()) super_class_index = self._read_byte2() klass.super_class = 'java/lang/Object' if super_class_index != 0: klass.super_class = klass.constant_pool.get_class( super_class_index) interfaces_count = self._read_byte2() for i in xrange(interfaces_count): klass.interfaces.append( klass.constant_pool.get_class(self._read_byte2())) field_length = self._read_byte2() for i in xrange(field_length): field = self.parse_field() klass.fields[field.name] = field method_count = self._read_byte2() for i in xrange(method_count): method = self.parse_method() klass.methods[Class.method_name(method)] = method klass.attributes = self.parse_attributes()
def parse(self): # read the first magic bytes for magic in MAGIC: byte = self._read_byte() if byte != magic: raise MalformedClassException() klass = self.klass # class file version klass.minor_version = self._read_byte2() klass.major_version = self._read_byte2() constant_pool_length = self._read_byte2() klass.constant_pool = ConstantPool(constant_pool_length) while klass.constant_pool.size < constant_pool_length-1: klass.constant_pool.add_pool(self.parse_constant_pool_item()) klass.access_flags = self._read_byte2() klass.this_class = klass.constant_pool.get_class(self._read_byte2()) super_class_index = self._read_byte2() klass.super_class = 'java/lang/Object' if super_class_index != 0: klass.super_class = klass.constant_pool.get_class(super_class_index) interfaces_count = self._read_byte2() for i in xrange(interfaces_count): klass.interfaces.append(klass.constant_pool.get_class(self._read_byte2())) field_length = self._read_byte2() for i in xrange(field_length): field = self.parse_field() klass.fields[field.name] = field method_count = self._read_byte2() for i in xrange(method_count): method = self.parse_method() klass.methods[Class.method_name(method)] = method klass.attributes = self.parse_attributes()
def load_class(self, class_name): java_Class = None if class_name[0] == 'L' and class_name[-1] == ';': class_name = class_name[1:-1] if class_name != 'java/lang/Class': java_Class = self.load_class('java/lang/Class') if class_name in self.class_cache: return self.class_cache[class_name] if class_name in primitive_classes: return primitive_classes[class_name] if class_name[0] == '[': self.class_cache[class_name] = klass = Class.array_factory(class_name) klass.super_class = java_Class klass.java_instance = java_Class.instantiate() klass.java_instance._values['class_name'] = class_name return klass klass = self.class_loader.load(class_name) self.class_cache[class_name] = klass if not java_Class: java_Class = klass klass._klass = java_Class klass.java_instance = java_Class.instantiate() klass.java_instance._values['class_name'] = class_name # load all supers and interfaces if klass.super_class: klass.super_class = self.load_class(klass.super_class) klass.interfaces = map(self.load_class, klass.interfaces) # run <clinit> method of class try: _, method = klass.get_method('<clinit>', '()V') self.wrap_run_method(klass, method) except NoSuchMethodException: pass return klass
def __init__(self, player_num, main_game_state, weapon, stats=DEFAULT_STATS): Class.__init__(self, 'wizard', player_num, weapon, main_game_state, stats)
char_stream = antlr3.ANTLRFileStream(f) lexer = fjLexer(char_stream) tokens = antlr3.CommonTokenStream(lexer) parser = fjParser(tokens) r = parser.program() root = r.tree children = r.tree.getChildren() cds = children[:-1] e = children[-1] ct = {} for cd in cds: klass = Class.fromTree(cd) ct[klass.name] = klass # DEBUG print "EXEC:" debug_node(e) exp = exp_from_tree(e) # DEBUG print "CT", ct print "e", exp ct["Object"] = CObject var_dict = {} print exp.execute(ct, var_dict)
def __init__(self, classname, filereader): self.offset = 0 self.classname = classname self.klass = Class(classname) self.file_reader = filereader self.parse()