def vmo5_normalize__Ljava_lang_String__Ljava_lang_String_(frame, args): '''Normalize according api rules''' s_ref = args[1] value = str_to_string(frame.vm, s_ref) norm = os.path.normpath(value) if value != norm: s_ref = frame.vm.make_heap_string(norm) frame.stack.append(s_ref)
def java_lang_Class_forName0__Ljava_lang_String_ZLjava_lang_ClassLoader__Ljava_lang_Class_(frame, args): ref = args[0] assert type(ref) is tuple and ref[0] == "ref" name = str_to_string(frame.vm, ref) name = name.replace(".", "/") klass = frame.vm.get_class(name) ref = frame.vm.get_class_class(klass) frame.stack.append(ref)
def java_lang_Class_forName0__Ljava_lang_String_ZLjava_lang_ClassLoader__Ljava_lang_Class_( frame, args): ref = args[0] assert type(ref) is tuple and ref[0] == "ref" name = str_to_string(frame.vm, ref) name = name.replace(".", "/") klass = frame.vm.get_class(name) ref = frame.vm.get_class_class(klass) frame.stack.append(ref)
def vmo5_getBooleanAttributes__Ljava_io_File__I(frame, args): '''See javadoc for details. Subset of all attributes is supported''' ref = args[1] assert ref is not None # NPE o = frame.vm.heap[ref[1]] path_ref = o.fields['path'] path = str_to_string(frame.vm, path_ref) result = 0 if os.path.exists(path): result |= 0x01 if not os.path.isfile(path): result |= 0x04 frame.stack.append(result)
def java_io_FileInputStream_open__Ljava_lang_String__V(frame, args): if args[1] is None: frame.vm.raise_exception(frame, "java/lang/NullPointerException") return fis = frame.vm.heap[args[0][1]] ref = args[1] file_name = str_to_string(frame.vm, ref) if not os.path.isfile(file_name): frame.vm.raise_exception(frame, "java/io/FileNotFoundException") return size = os.path.getsize(file_name) f = open(file_name, 'rb') fis.fields["@file"] = f fis.fields["@available_bytes"] = size
def sun_misc_Unsafe_objectFieldOffset__Ljava_lang_reflect_Field__J(frame, args): ref = args[1] assert type(ref) is tuple and ref[0] == "ref" field = frame.vm.heap[ref[1]] name = str_to_string(frame.vm, field.fields["name"]) k_ref = field.fields["clazz"] klass_object = frame.vm.heap[k_ref[1]] klass = frame.vm.get_class(klass_object.fields["@CLASS_NAME"]) o = klass.get_instance(frame.vm) index = 0 for key in o.fields: if key == name: frame.stack.append(("long", index)) return index += 1 assert False # should never get here
def java_io_FileOutputStream_open__Ljava_lang_String_Z_V(frame, args): if args[1] is None: frame.vm.raise_exception(frame, "java/lang/NullPointerException") return fis = frame.vm.heap[args[0][1]] ref = args[1] file_name = str_to_string(frame.vm, ref) append_flag = args[2] assert append_flag == 0, "File append is not yet here" if not os.path.isfile(file_name): frame.vm.raise_exception(frame, "java/io/FileNotFoundException") return size = os.path.getsize(file_name) f = open(file_name, 'wb') fis.fields["@file"] = f fis.fields["@available_bytes"] = size
def sun_misc_Unsafe_objectFieldOffset__Ljava_lang_reflect_Field__J( frame, args): ref = args[1] assert type(ref) is tuple and ref[0] == "ref" field = frame.vm.heap[ref[1]] name = str_to_string(frame.vm, field.fields["name"]) k_ref = field.fields["clazz"] klass_object = frame.vm.heap[k_ref[1]] klass = frame.vm.get_class(klass_object.fields["@CLASS_NAME"]) o = klass.get_instance(frame.vm) index = 0 for key in o.fields: if key == name: frame.stack.append(("long", index)) return index += 1 assert False # should never get here
def vmo2_getProperty__Ljava_lang_String__Ljava_lang_String_(frame, args): '''java.lang.System public static String getProperty(String key) This is call to java.util.Properties object ''' s_ref = args[1] value = str_to_string(frame.vm, s_ref) # refactor this code someday # ok for now, as all refs are cached props = {} props["file.encoding"] = frame.vm.make_heap_string("utf8") props["line.separator"] = frame.vm.make_heap_string("\n") if value in props: ref = props[value] assert type(ref) is tuple and ref[0] == "ref" frame.stack.append(ref) return frame.stack.append(None)
def sun_reflect_NativeConstructorAccessorImpl_newInstance0__Ljava_lang_reflect_Constructor__Ljava_lang_Object__Ljava_lang_Object_(frame, args): '''Create instance of a class, with constructor call''' ref = args[0] params = args[1] assert type(ref) is tuple and ref[0] == "ref" assert params is None or len(params) == 0 o = frame.vm.heap[ref[1]] klass_klass = frame.vm.heap[o.fields["clazz"][1]] clazz = frame.vm.get_class(klass_klass.fields["@CLASS_NAME"]) signature = str_to_string(frame.vm, o.fields["signature"]) assert signature == "()V" instance = clazz.get_instance(frame.vm) iref = frame.vm.add_to_heap(instance) frame.stack.append(iref) method = clazz.find_method("<init>", signature) # actully running constructor in exclusive mode pvm_thread = Thread(frame.vm, frame.vm.top_thread_ref) pvm_thread.is_alive = True m_args = [None]*method[1] m_args[0] = iref sub = Frame(pvm_thread, clazz, method, m_args, "nativ instance0") pvm_thread.frame_stack.append(sub) frame.vm.run_thread(pvm_thread)