def create_args_array(loader, args): string_class = loader.load_class("java/lang/String") args_array = string_class.array_class().new_array(len(args)) j_args = args_array.refs() for i, arg in enumerate(args): j_args[i] = j_string(loader, arg) return args_array
def init_static_final_var(clazz: Class, field: Field): from rtda.heap.StringPool import j_string static_vars = clazz.static_vars constant_pool = clazz.constant_pool cp_index = field.const_value_index slot_id = field.slot_id if cp_index > 0: if field.descriptor in {"Z", "B", "C", "S", "I", "J", "F", "D"}: val = constant_pool.get_constant(cp_index) static_vars.set_numeric(slot_id, val) elif field.descriptor == "Ljava/lang/String": python_str = constant_pool.get_constant(cp_index) j_str = j_string(clazz.loader, python_str) static_vars.set_ref(slot_id, j_str)
def _ldc(frame: Frame, index): stack = frame.operand_stack clazz = frame.method.get_class() c = clazz.constant_pool.get_constant(index) if isinstance(c, int): stack.push_numeric(c) elif isinstance(c, float): stack.push_numeric(c) elif isinstance(c, str): # 从运行时常量池中加载字符串常量,先通过常量拿到python字符串,然后把它转成Java字符串实例 interned_str = j_string(clazz.loader, c) # 把引用推入操作数栈顶 stack.push_ref(interned_str) else: raise RuntimeError("todo: ldc!")
def get_name_0(frame: Frame): """ private native String getName0() :param frame: :return: """ # 从局部变量表中拿到this引用,这是一个类对象引用 this = frame.local_vars.get_this() # 通过extra获得与之对应的Class对象 clazz = this.extra # 获取类名 name = clazz.java_name # 转成Java字符串 name_obj = j_string(clazz.loader, name) # 将其推入操作数栈顶 frame.operand_stack.push_ref(name_obj)